Sha256: e8b5255846b736922c48bdf1c406a740a580e00c20f5bf09cec98d91b8877c4d

Contents?: true

Size: 711 Bytes

Versions: 39

Compression:

Stored size: 711 Bytes

Contents

require 'thread'
require 'rack/body_proxy'

module Rack
  # Rack::Lock locks every request inside a mutex, so that every request
  # will effectively be executed synchronously.
  class Lock
    def initialize(app, mutex = Mutex.new)
      @app, @mutex = app, mutex
    end

    def call(env)
      @mutex.lock
      @env = env
      @old_rack_multithread = env[RACK_MULTITHREAD]
      begin
        response = @app.call(env.merge!(RACK_MULTITHREAD => false))
        returned = response << BodyProxy.new(response.pop) { unlock }
      ensure
        unlock unless returned
      end
    end

    private

    def unlock
      @mutex.unlock
      @env[RACK_MULTITHREAD] = @old_rack_multithread
    end
  end
end

Version data entries

39 entries across 37 versions & 9 rubygems

Version Path
rack-2.0.9.4 lib/rack/lock.rb
rack-2.0.9.3 lib/rack/lock.rb
rack-2.0.9.2 lib/rack/lock.rb
rack-2.0.9.1 lib/rack/lock.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-2.0.5/lib/rack/lock.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rack-2.0.8/lib/rack/lock.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/lock.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
rack-2.0.9 lib/rack/lock.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rack-2.0.8/lib/rack/lock.rb
rack-2.0.8 lib/rack/lock.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
tdiary-5.1.0 vendor/bundle/gems/rack-2.0.7/lib/rack/lock.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/lock.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/lock.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/lock.rb