Skip to content
Newer
Older
100644 23 lines (20 sloc) 545 Bytes
53844d7 @josh rack lock should require thread
josh authored
1 require 'thread'
dec966d @rkh fix Rack::Lock, use same logic for Rack::CommonLogger
rkh authored
2 require 'rack/body_proxy'
53844d7 @josh rack lock should require thread
josh authored
3
7409b0c @josh Add Locking middleware
josh authored
4 module Rack
f467626 @oscardelben Document Rack::Lock
oscardelben authored
5 # Rack::Lock locks every request inside a mutex, so that every request
6 # will effectively be executed synchronously.
7409b0c @josh Add Locking middleware
josh authored
7 class Lock
3bf8655 @tenderlove use a proxy object to call close on the mutex when the body is closed…
tenderlove authored
8 def initialize(app, mutex = Mutex.new)
9 @app, @mutex = app, mutex
7409b0c @josh Add Locking middleware
josh authored
10 end
11
12 def call(env)
3bf8655 @tenderlove use a proxy object to call close on the mutex when the body is closed…
tenderlove authored
13 @mutex.lock
d520111 @dkubb Fix Rack::Lock mutex usage
dkubb authored
14 begin
fd1fbab @deepj Constantize all rack environment variables and make them frozen
deepj authored
15 response = @app.call(env.merge(RACK_MULTITHREAD => false))
41b2b37 @dkubb Fix Rack::Lock to handle unexpected exceptions
dkubb authored
16 returned = response << BodyProxy.new(response.pop) { @mutex.unlock }
d520111 @dkubb Fix Rack::Lock mutex usage
dkubb authored
17 ensure
41b2b37 @dkubb Fix Rack::Lock to handle unexpected exceptions
dkubb authored
18 @mutex.unlock unless returned
d520111 @dkubb Fix Rack::Lock mutex usage
dkubb authored
19 end
7409b0c @josh Add Locking middleware
josh authored
20 end
21 end
22 end
Something went wrong with that request. Please try again.