Class: Falcon::Body::RequestFinished

Inherits:
Protocol::HTTP::Body::Wrapper
  • Object
show all
Defined in:
lib/falcon/body/request_finished.rb

Overview

Wraps a response body and decrements a metric after the body is closed.

Runs close on the underlying body first (which invokes rack.response_finished), then decrements the metric, even if closing raises. Use this so requests_active stays elevated until the request is fully finished (including response_finished callbacks), without leaking if close reports an error.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, metric) ⇒ RequestFinished

Initialize the wrapper.



39
40
41
42
43
# File 'lib/falcon/body/request_finished.rb', line 39

def initialize(body, metric)
	super(body)
	
	@metric = metric
end

Class Method Details

.wrap(message, metric) ⇒ Object

Wrap a response body with a metric.

Expects the caller to have incremented ‘requests_active` beforehand. If the body is not wrapped (nil or empty message/body, or an error while assigning the wrapper), the `ensure` block decrements once. When wrapped, the metric is decremented in #close.



25
26
27
28
29
30
31
32
33
# File 'lib/falcon/body/request_finished.rb', line 25

def self.wrap(message, metric)
	if body = message&.body and !body.empty?
		message.body = new(body, metric)
	else
		metric.decrement
	end
	
	message
end

Instance Method Details

#close(error = nil) ⇒ Object

Closes the underlying body (invoking rack.response_finished), then decrements the metric.



58
59
60
61
62
63
# File 'lib/falcon/body/request_finished.rb', line 58

def close(error = nil)
	super
ensure
	@metric&.decrement
	@metric = nil
end

#rewindObject



51
52
53
# File 'lib/falcon/body/request_finished.rb', line 51

def rewind
	false
end

#rewindable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/falcon/body/request_finished.rb', line 46

def rewindable?
	false
end