(defn timeout
"returns a channel that will close after msecs"
[^long msecs]
(let [timeout (+ (System/currentTimeMillis) msecs)
me (.ceilingEntry timeouts-map timeout)]
(or (when (and me (< (.getKey me) (+ timeout TIMEOUT_RESOLUTION_MS)))
(.channel ^TimeoutQueueEntry (.getValue me)))
(let [timeout-channel (channels/chan nil)
timeout-entry (TimeoutQueueEntry. timeout-channel timeout)]
(.put timeouts-map timeout timeout-entry)
(.put timeouts-queue timeout-entry)
timeout-channel))))