Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move gaint lock into middleware

  • Loading branch information...
commit 2eb2ec9e635c740684673495ed547d1c0769038d 1 parent 3ff6b00
@josh josh authored
View
1  actionpack/lib/action_controller.rb
@@ -56,6 +56,7 @@ def self.load_all!
autoload :Integration, 'action_controller/integration'
autoload :IntegrationTest, 'action_controller/integration'
autoload :Layout, 'action_controller/layout'
+ autoload :Lock, 'action_controller/lock'
autoload :MiddlewareStack, 'action_controller/middleware_stack'
autoload :MimeResponds, 'action_controller/mime_responds'
autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes'
View
15 actionpack/lib/action_controller/dispatcher.rb
@@ -2,8 +2,6 @@ module ActionController
# Dispatches requests to the appropriate controller and takes care of
# reloading the app after each request when Dependencies.load? is true.
class Dispatcher
- @@guard = Mutex.new
-
class << self
def define_dispatcher_callbacks(cache_classes)
unless cache_classes
@@ -46,6 +44,7 @@ def to_prepare(identifier = nil, &block)
cattr_accessor :middleware
self.middleware = MiddlewareStack.new do |middleware|
+ middleware.use "ActionController::Lock", :if => lambda { !ActionController::Base.allow_concurrency }
middleware.use "ActionController::Failsafe"
middleware.use "ActionController::SessionManagement::Middleware"
end
@@ -59,7 +58,7 @@ def initialize(output = $stdout, request = nil, response = nil)
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
end
- def dispatch_unlocked
+ def dispatch
begin
run_callbacks :before_dispatch
handle_request
@@ -70,16 +69,6 @@ def dispatch_unlocked
end
end
- def dispatch
- if ActionController::Base.allow_concurrency
- dispatch_unlocked
- else
- @@guard.synchronize do
- dispatch_unlocked
- end
- end
- end
-
# DEPRECATE: Remove CGI support
def dispatch_cgi(cgi, session_options)
CGIHandler.dispatch_cgi(self, cgi, @output)
View
18 actionpack/lib/action_controller/lock.rb
@@ -0,0 +1,18 @@
+module ActionController
+ class Lock
+ def initialize(app)
+ @app = app
+ @lock = Mutex.new
+ end
+
+ def call(env)
+ old_multithread = env["rack.multithread"]
+ env["rack.multithread"] = false
+ response = @lock.synchronize do
+ @app.call(env)
+ end
+ env["rack.multithread"] = old_multithread
+ response
+ end
+ end
+end
View
22 actionpack/lib/action_controller/middleware_stack.rb
@@ -10,10 +10,26 @@ def initialize(klass, *args, &block)
@klass = klass.to_s.constantize
end
+ options = args.extract_options!
+ if options.has_key?(:if)
+ @conditional = options.delete(:if)
+ else
+ @conditional = true
+ end
+ args << options unless options.empty?
+
@args = args
@block = block
end
+ def active?
+ if @conditional.respond_to?(:call)
+ @conditional.call
+ else
+ @conditional
+ end
+ end
+
def ==(middleware)
case middleware
when Middleware
@@ -50,8 +66,12 @@ def use(*args, &block)
push(middleware)
end
+ def active
+ find_all { |middleware| middleware.active? }
+ end
+
def build(app)
- reverse.inject(app) { |a, e| e.build(a) }
+ active.reverse.inject(app) { |a, e| e.build(a) }
end
end
end
View
2  actionpack/test/controller/dispatcher_test.rb
@@ -50,7 +50,7 @@ def log_failsafe_exception(status, exception); end
end
def test_failsafe_response
- Dispatcher.any_instance.expects(:dispatch_unlocked).raises('b00m')
+ Dispatcher.any_instance.expects(:dispatch).raises('b00m')
ActionController::Failsafe.any_instance.expects(:log_failsafe_exception)
assert_nothing_raised do
View
2  railties/lib/tasks/middleware.rake
@@ -1,6 +1,6 @@
desc 'Prints out your Rack middleware stack'
task :middleware => :environment do
- ActionController::Dispatcher.middleware.each do |middleware|
+ ActionController::Dispatcher.middleware.active.each do |middleware|
puts "use #{middleware.inspect}"
end
puts "run ActionController::Dispatcher.new"

0 comments on commit 2eb2ec9

Please sign in to comment.
Something went wrong with that request. Please try again.