module Environment
A flat environment module for falcon-limiter services.
Provides simple, declarative configuration for concurrency limiting. Override these methods in your service to customize behavior.
Definitions
def limiter_maximum_long_tasks
Maximum number of concurrent long tasks (default: 10). If this is nil or non-positive, long task support will be disabled.
Signature
-
returns
Integer
The maximum number of concurrent long tasks.
Implementation
def limiter_maximum_long_tasks
10
end
def limiter_maximum_connections
Signature
-
returns
Integer
The maximum number of concurrent connection accepts.
Implementation
def limiter_maximum_connections
1
end
def limiter_start_delay
Signature
-
returns
Float
The delay before starting long task in seconds.
Implementation
def limiter_start_delay
0.1
end
def connection_limiter
Signature
-
returns
Async::Limiter::Queued
The limiter for coordinating long tasks and connection accepts.
Implementation
def connection_limiter
# Create priority queue and pre-populate with tokens:
queue = Async::PriorityQueue.new
limiter_maximum_connections.times{queue.push(true)}
Async::Limiter::Queued.new(queue)
end
def limiter_middleware_class
Signature
-
returns
Class
The middleware class to use for long task support.
Implementation
def limiter_middleware_class
Middleware
end
def limiter_middleware(middleware)
Signature
-
returns
Protocol::HTTP::Middleware
The middleware with long task support, if enabled.
Implementation
def limiter_middleware(middleware)
# Create middleware with long task support if enabled:
if limiter_maximum_long_tasks&.positive?
limiter_middleware_class.new(
middleware,
connection_limiter: connection_limiter,
maximum_long_tasks: limiter_maximum_long_tasks,
start_delay: limiter_start_delay
)
else
middleware
end
end
def middleware
Signature
-
returns
Protocol::HTTP::Middleware
The middleware with long task support, if enabled.
Implementation
def middleware
limiter_middleware(super)
end
def limiter_wrapper
Signature
-
returns
Falcon::Limiter::Wrapper
The wrapper for the connection limiter.
Implementation
def limiter_wrapper
Wrapper.new(connection_limiter)
end
def endpoint
Signature
-
returns
IO::Endpoint::Wrapper
The endpoint with connection limiting.
Implementation
def endpoint
super.with(wrapper: limiter_wrapper)
end