Skip to content

Instantly share code, notes, and snippets.

@janherich
Last active August 29, 2015 13:55
Show Gist options
  • Save janherich/8738585 to your computer and use it in GitHub Desktop.
Save janherich/8738585 to your computer and use it in GitHub Desktop.
time bound channel
(defn create-cancel-channel
[channel-to-notify timeout-ms]
(let [cancel-channel (async/chan)
timeout-channel (async/timeout timeout-ms)]
(async/go
(let [[_ c] (async/alts! [cancel-channel timeout-channel])]
(when (= c timeout-channel)
(async/close! cancel-channel)
(async/close! channel-to-notify))))
cancel-channel))
;; transport channel
(def transport-channel (async/chan))
;; cancel-channel
(def cancel-channel (create-cancel-channel transport-channel 2000))
;; to cancel the transport channel timeout, just close the cancel-channel
(async/close! cancel-channel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment