Class: Falcon::Limiter::Middleware
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Falcon::Limiter::Middleware
- Defined in:
- lib/falcon/limiter/middleware.rb
Overview
Protocol::HTTP middleware that provides long task management for requests. This allows applications to manage I/O vs CPU bound workloads effectively.
Instance Attribute Summary collapse
-
#connection_limiter ⇒ Object
readonly
Returns the value of attribute connection_limiter.
-
#long_task_limiter ⇒ Object
readonly
Returns the value of attribute long_task_limiter.
-
#maximum_long_tasks ⇒ Object
readonly
Returns the value of attribute maximum_long_tasks.
-
#start_delay ⇒ Object
readonly
Returns the value of attribute start_delay.
-
#utilization ⇒ Object
readonly
Returns the value of attribute utilization.
Instance Method Summary collapse
-
#call(request) ⇒ Object
Process an HTTP request with long task management support.
-
#initialize(delegate, connection_limiter:, maximum_long_tasks: 10, start_delay: 0.1, utilization: Async::Utilization::Registry.new) ⇒ Middleware
constructor
Initialize the middleware with limiting configuration.
-
#statistics ⇒ Object
Get semaphore statistics.
Constructor Details
#initialize(delegate, connection_limiter:, maximum_long_tasks: 10, start_delay: 0.1, utilization: Async::Utilization::Registry.new) ⇒ Middleware
Initialize the middleware with limiting configuration.
23 24 25 26 27 28 29 30 31 |
# File 'lib/falcon/limiter/middleware.rb', line 23 def initialize(delegate, connection_limiter:, maximum_long_tasks: 10, start_delay: 0.1, utilization: Async::Utilization::Registry.new) super(delegate) @maximum_long_tasks = maximum_long_tasks @start_delay = start_delay @connection_limiter = connection_limiter @utilization = utilization @long_task_limiter = Semaphore.new(maximum_long_tasks, utilization: utilization.namespace(:long_task)) end |
Instance Attribute Details
#connection_limiter ⇒ Object (readonly)
Returns the value of attribute connection_limiter.
33 34 35 |
# File 'lib/falcon/limiter/middleware.rb', line 33 def connection_limiter @connection_limiter end |
#long_task_limiter ⇒ Object (readonly)
Returns the value of attribute long_task_limiter.
33 34 35 |
# File 'lib/falcon/limiter/middleware.rb', line 33 def long_task_limiter @long_task_limiter end |
#maximum_long_tasks ⇒ Object (readonly)
Returns the value of attribute maximum_long_tasks.
33 34 35 |
# File 'lib/falcon/limiter/middleware.rb', line 33 def maximum_long_tasks @maximum_long_tasks end |
#start_delay ⇒ Object (readonly)
Returns the value of attribute start_delay.
33 34 35 |
# File 'lib/falcon/limiter/middleware.rb', line 33 def start_delay @start_delay end |
#utilization ⇒ Object (readonly)
Returns the value of attribute utilization.
33 34 35 |
# File 'lib/falcon/limiter/middleware.rb', line 33 def utilization @utilization end |
Instance Method Details
#call(request) ⇒ Object
Process an HTTP request with long task management support. Creates a long task context that applications can use to manage I/O operations.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/falcon/limiter/middleware.rb', line 39 def call(request) # Create LongTask instance for this request if enabled long_task = LongTask.for(request, @long_task_limiter, start_delay: @start_delay) # Use scoped context for clean access long_task.with do response = super(request) if long_task.started? Protocol::HTTP::Body::Completable.wrap(response) do long_task.stop(force: true) end end response end end |
#statistics ⇒ Object
Get semaphore statistics
58 59 60 61 62 63 |
# File 'lib/falcon/limiter/middleware.rb', line 58 def statistics { long_task_limiter: @long_task_limiter.statistics, connection_limiter: @connection_limiter.statistics } end |