Created
December 14, 2013 21:55
-
-
Save ztellman/7965425 to your computer and use it in GitHub Desktop.
This file contains 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
(defn timeout-line-seq | |
"Takes a reader, and returns a line-seq that can time out on reading the next line." | |
[^Reader reader timeout] | |
(let [q (LinkedBlockingQueue. 10) | |
q-seq (fn this [] | |
(lazy-seq | |
(if-let [l (.poll q timeout TimeUnit/MILLISECONDS)] | |
(when-not (= ::end l) | |
(cons l (this))) | |
(do | |
(.close reader) | |
(throw (TimeoutException. "next-line timeout elapsed"))))))] | |
(future | |
(try | |
(doseq [l (line-seq reader)] | |
(.put q l)) | |
(finally | |
(.put q ::end)))) | |
(q-seq))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment