Class: Fizzy::Bulkhead
- Inherits:
-
Object
- Object
- Fizzy::Bulkhead
- Defined in:
- lib/fizzy/bulkhead.rb
Overview
Semaphore-based concurrency limiter (bulkhead pattern).
Limits the number of concurrent operations to prevent resource exhaustion. When the limit is reached, callers block until a slot becomes available or the timeout expires.
Instance Attribute Summary collapse
-
#current ⇒ Integer
readonly
Number of currently active operations.
Instance Method Summary collapse
-
#call { ... } ⇒ Object
Executes the block within the concurrency limit.
-
#initialize(max_concurrent: 10, timeout: 5) ⇒ Bulkhead
constructor
A new instance of Bulkhead.
Constructor Details
#initialize(max_concurrent: 10, timeout: 5) ⇒ Bulkhead
Returns a new instance of Bulkhead.
16 17 18 19 20 21 22 |
# File 'lib/fizzy/bulkhead.rb', line 16 def initialize(max_concurrent: 10, timeout: 5) @max_concurrent = max_concurrent @timeout = timeout @semaphore = Mutex.new @condition = ConditionVariable.new @current = 0 end |
Instance Attribute Details
#current ⇒ Integer (readonly)
Returns number of currently active operations.
25 26 27 |
# File 'lib/fizzy/bulkhead.rb', line 25 def current @current end |
Instance Method Details
#call { ... } ⇒ Object
Executes the block within the concurrency limit.
32 33 34 35 36 37 38 39 |
# File 'lib/fizzy/bulkhead.rb', line 32 def call acquire_slot begin yield ensure release_slot end end |