Skip to content

Instantly share code, notes, and snippets.

@agocs
Created August 18, 2014 15:18
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 agocs/8916127a42312a65a990 to your computer and use it in GitHub Desktop.
Save agocs/8916127a42312a65a990 to your computer and use it in GitHub Desktop.
(defmacro time-limited
"Use this macro to cause a function to time out after a little while"
[seconds & body]
`(let [f# (future ~@body)]
(try
(.get f# ~seconds java.util.concurrent.TimeUnit/SECONDS)
(catch java.util.concurrent.TimeoutException x#
(do (future-cancel f#)
(throw x#)))
(catch Exception e#
(throw (.getCause e#))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment