Last active
September 12, 2020 10:03
-
-
Save bfollington/2099f96ea4c3e0be08e38a52053fd60b to your computer and use it in GitHub Desktop.
Check response time from a list of URLs every second
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cursive-test.core | |
(:require [clj-http.client :as client]) | |
(:require [clojure.core.async :as async])) | |
; setup async pipelines | |
(def publisher (async/chan)) | |
(def publication | |
(async/pub publisher #(:topic %))) | |
(def subscriber-one (async/chan)) | |
; pinging | |
(def urls | |
[ | |
"https://twopm.studio" | |
"https://twopm.itch.io" | |
"https://bf.wtf" | |
]) | |
(defn ping | |
[url chan] | |
(async/go | |
(async/>! chan {:topic :ping | |
:url url | |
:res (client/head url)}))) | |
(defn ping-many | |
[urls chan] | |
(run! #(ping % chan) urls)) | |
; read results | |
(defn pluck-results | |
[res] | |
{:url (:url res) | |
:time (get-in res [:request-time :res])}) | |
(defn read-results | |
[chan] | |
(async/go-loop [] | |
(->>(async/<! chan) | |
(pluck-results) | |
(println)) | |
(recur))) | |
; kick off | |
(async/sub publication :ping subscriber-one) | |
(read-results subscriber-one) | |
(async/go-loop [] | |
(ping-many urls publisher) | |
(async/<! (async/timeout 1000)) | |
(recur)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment