Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created June 7, 2011 05:15
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 cowboyd/1011718 to your computer and use it in GitHub Desktop.
Save cowboyd/1011718 to your computer and use it in GitHub Desktop.
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