Class: RackRequestBlocker

Inherits:
Object
  • Object
show all
Defined in:
lib/intransient_capybara/rack_request_blocker.rb

Overview

Rack middleware that keeps track of the number of active requests and can block new requests.

Constant Summary collapse

@@num_active_requests =
Atomic.new(0)
@@block_requests =
Atomic.new(false)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackRequestBlocker

Returns a new instance of RackRequestBlocker.



25
26
27
# File 'lib/intransient_capybara/rack_request_blocker.rb', line 25

def initialize(app)
  @app = app
end

Class Method Details

.allow_requests!Object

Allows the server to accept requests again.



21
22
23
# File 'lib/intransient_capybara/rack_request_blocker.rb', line 21

def self.allow_requests!
  @@block_requests.value = false
end

.block_requests!Object

Prevents the server from accepting new requests. Any new requests will return an HTTP 503 status.



16
17
18
# File 'lib/intransient_capybara/rack_request_blocker.rb', line 16

def self.block_requests!
  @@block_requests.value = true
end

.num_active_requestsObject

Returns the number of requests the server is currently processing.



10
11
12
# File 'lib/intransient_capybara/rack_request_blocker.rb', line 10

def self.num_active_requests
  @@num_active_requests.value
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/intransient_capybara/rack_request_blocker.rb', line 29

def call(env)
  increment_active_requests
  if block_requests?
    block_request(env)
  else
    @app.call(env)
  end
ensure
  decrement_active_requests
end