Skip to content

Instantly share code, notes, and snippets.

@ztellman
Created December 14, 2013 21:55
Show Gist options
  • Save ztellman/7965425 to your computer and use it in GitHub Desktop.
Save ztellman/7965425 to your computer and use it in GitHub Desktop.
(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