Skip to content

Instantly share code, notes, and snippets.

@retrogradeorbit
Last active July 8, 2020 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retrogradeorbit/5edb20ac9559061dfd3c0ca74336fdae to your computer and use it in GitHub Desktop.
Save retrogradeorbit/5edb20ac9559061dfd3c0ca74336fdae to your computer and use it in GitHub Desktop.
wait-for-port spire code. extracted from babashka
(ns net-utils
(:import [java.net Socket ConnectException]))
(defn wait-for-port
"Waits for TCP connection to be available on host and port. Options map
supports `:timeout` and `:pause`. If `:timeout` is provided and reached,
`:default`'s value (if any) is returned. The `:pause` option determines
the time waited between retries."
([host port]
(wait-for-port host port nil))
([^String host ^long port {:keys [:default :timeout :pause] :as opts}]
(let [opts (merge {:host host
:port port}
opts)
t0 (System/currentTimeMillis)]
(loop []
(let [v (try (.close (Socket. host port))
(- (System/currentTimeMillis) t0)
(catch ConnectException _e
(let [took (- (System/currentTimeMillis) t0)]
(if (and timeout (>= took timeout))
:wait-for-port.impl/timed-out
:wait-for-port.impl/try-again))))]
(cond (identical? :wait-for-port.impl/try-again v)
(do (Thread/sleep (or pause 100))
(recur))
(identical? :wait-for-port.impl/timed-out v)
default
:else
(assoc opts :took v)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment