Skip to content
Newer
Older
100644 34 lines (28 sloc) 628 Bytes
53844d7 @josh rack lock should require thread
josh authored Jun 16, 2010
1 require 'thread'
2
7409b0c @josh Add Locking middleware
josh authored Jan 15, 2009
3 module Rack
4 class Lock
3bf8655 @tenderlove use a proxy object to call close on the mutex when the body is closed…
tenderlove authored Dec 17, 2010
5 class Proxy < Struct.new(:target, :mutex) # :nodoc:
6 def each
7 target.each { |x| yield x }
8 end
9
10 def close
11 target.close if target.respond_to?(:close)
12 ensure
13 mutex.unlock
14 end
15 end
16
7409b0c @josh Add Locking middleware
josh authored Jan 15, 2009
17 FLAG = 'rack.multithread'.freeze
18
3bf8655 @tenderlove use a proxy object to call close on the mutex when the body is closed…
tenderlove authored Dec 17, 2010
19 def initialize(app, mutex = Mutex.new)
20 @app, @mutex = app, mutex
7409b0c @josh Add Locking middleware
josh authored Jan 15, 2009
21 end
22
23 def call(env)
24 old, env[FLAG] = env[FLAG], false
3bf8655 @tenderlove use a proxy object to call close on the mutex when the body is closed…
tenderlove authored Dec 17, 2010
25 @mutex.lock
26 response = @app.call(env)
27 response[2] = Proxy.new(response[2], @mutex)
28 response
7409b0c @josh Add Locking middleware
josh authored Jan 15, 2009
29 ensure
30 env[FLAG] = old
31 end
32 end
33 end
Something went wrong with that request. Please try again.