Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This correctly doesn't release the lock until after the body is closed. Restore the related tests as well. Just make changes to avoid use of rack.multithread.
- Loading branch information
1 parent
38baae6
commit a8a0459
Showing
2 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# Rack::Lock locks every request inside a mutex, so that every request | ||
# will effectively be executed synchronously. | ||
class Lock | ||
def initialize(app) | ||
@app = app | ||
@mutex = ::Thread::Mutex.new | ||
def initialize(app, mutex = Mutex.new) | ||
@app, @mutex = app, mutex | ||
end | ||
|
||
def call(env) | ||
@mutex.synchronize do | ||
@app.call(env) | ||
@mutex.lock | ||
begin | ||
response = @app.call(env) | ||
returned = response << BodyProxy.new(response.pop) { unlock } | ||
ensure | ||
unlock unless returned | ||
end | ||
end | ||
|
||
def self.rackup(config, app) | ||
if config.multithread? || (config.concurrent? && RUBY_VERSION >= '3') | ||
new(app) | ||
else | ||
app | ||
end | ||
private | ||
|
||
def unlock | ||
@mutex.unlock | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters