Create a gist now

Instantly share code, notes, and snippets.

Rack middleware to lock v8
##
# Rack middleware which synchronizes access to an
# in-process v8 engine embedded with The Ruby Racer
#
class LockV8
def initialize(app)
@app = app
end
# Grabs the V8 GIL for the duration of this request.
#
# The current implentation of Locker() is naive and
# does not catch any exceptions inside the passed in
# block. Instead, manually manage the exceptions and
# return value outside of the critical section.
#
# Locking will be made more robust in future versions.
def call(*args)
result = nil
err = nil
V8::C::Locker() do
begin
result = @app.call(*args)
rescue Exception => e
err = e
end
end
raise err if err
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment