Class: FiberJob::ConcurrencyManager
- Inherits:
-
Object
- Object
- FiberJob::ConcurrencyManager
- Defined in:
- lib/fiber_job/concurrency.rb
Overview
The ConcurrencyManager class provides efficient fiber reuse through direct semaphore acquire/release operations instead of spawning new fibers
Instance Attribute Summary collapse
-
#semaphore ⇒ Object
readonly
Returns the value of attribute semaphore.
Instance Method Summary collapse
-
#acquire ⇒ void
Acquire a semaphore permit, blocking if none available.
-
#blocking? ⇒ Boolean
Check if the semaphore would block on acquire.
-
#count ⇒ Integer
Current number of acquired permits.
-
#execute {|void| ... } ⇒ Object
Execute a block with automatic acquire/release handling This method reuses the current fiber instead of spawning new ones.
-
#initialize(max_concurrency: 5) ⇒ ConcurrencyManager
constructor
A new instance of ConcurrencyManager.
-
#limit ⇒ Integer
Maximum number of permits.
-
#release ⇒ void
Release a semaphore permit, allowing waiting fibers to proceed.
Constructor Details
#initialize(max_concurrency: 5) ⇒ ConcurrencyManager
Returns a new instance of ConcurrencyManager.
12 13 14 |
# File 'lib/fiber_job/concurrency.rb', line 12 def initialize(max_concurrency: 5) @semaphore = Async::Semaphore.new(max_concurrency) end |
Instance Attribute Details
#semaphore ⇒ Object (readonly)
Returns the value of attribute semaphore.
10 11 12 |
# File 'lib/fiber_job/concurrency.rb', line 10 def semaphore @semaphore end |
Instance Method Details
#acquire ⇒ void
This method returns an undefined value.
Acquire a semaphore permit, blocking if none available
18 19 20 |
# File 'lib/fiber_job/concurrency.rb', line 18 def acquire @semaphore.acquire end |
#blocking? ⇒ Boolean
Check if the semaphore would block on acquire
43 44 45 |
# File 'lib/fiber_job/concurrency.rb', line 43 def blocking? @semaphore.blocking? end |
#count ⇒ Integer
Current number of acquired permits
49 50 51 |
# File 'lib/fiber_job/concurrency.rb', line 49 def count @semaphore.count end |
#execute {|void| ... } ⇒ Object
Execute a block with automatic acquire/release handling This method reuses the current fiber instead of spawning new ones
32 33 34 35 36 37 38 39 |
# File 'lib/fiber_job/concurrency.rb', line 32 def execute(&) acquire begin yield ensure release end end |
#limit ⇒ Integer
Maximum number of permits
55 56 57 |
# File 'lib/fiber_job/concurrency.rb', line 55 def limit @semaphore.limit end |
#release ⇒ void
This method returns an undefined value.
Release a semaphore permit, allowing waiting fibers to proceed
24 25 26 |
# File 'lib/fiber_job/concurrency.rb', line 24 def release @semaphore.release end |