Created
June 7, 2011 05:15
-
-
Save cowboyd/1011718 to your computer and use it in GitHub Desktop.
Rack middleware to lock v8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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