Blocking (synchronous) calls in Goliath
Posting for my future self. A generic function to run blocking code in a deferred thread and resume the fiber on completion, so as not to block the reactor loop.
1 2 3 4 5 6 7 8 9 10 |
def blocking(&f) fiber = Fiber.current result = nil EM.defer(f, ->(x){ result = x fiber.resume }) Fiber.yield result end |
Usage
1 2 3 4 5 6 |
class MyServer < Goliath::API def response(env) blocking { sleep 1 } [200, {}, 'Woken up'] end end |