Skip to content

Instantly share code, notes, and snippets.

@bfollington
Last active September 12, 2020 10:03
Show Gist options
  • Save bfollington/2099f96ea4c3e0be08e38a52053fd60b to your computer and use it in GitHub Desktop.
Save bfollington/2099f96ea4c3e0be08e38a52053fd60b to your computer and use it in GitHub Desktop.
Check response time from a list of URLs every second
(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