Sha256: 77936ab002dc068e665345aedf6b15d8d93de74d40755514a09dd723e84569f5

Contents?: true

Size: 742 Bytes

Versions: 9

Compression:

Stored size: 742 Bytes

Contents

# frozen_string_literal: true

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

9 entries across 9 versions & 1 rubygems

Version Path
rack-2.1.4.4 lib/rack/lock.rb
rack-2.1.4.3 lib/rack/lock.rb
rack-2.1.4.2 lib/rack/lock.rb
rack-2.1.4.1 lib/rack/lock.rb
rack-2.1.4 lib/rack/lock.rb
rack-2.1.3 lib/rack/lock.rb
rack-2.1.2 lib/rack/lock.rb
rack-2.1.1 lib/rack/lock.rb
rack-2.1.0 lib/rack/lock.rb