Class: FiberJob::ConcurrencyManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#semaphoreObject (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

#acquirevoid

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

Returns:

  • (Boolean)

    true if acquire would block



43
44
45
# File 'lib/fiber_job/concurrency.rb', line 43

def blocking?
  @semaphore.blocking?
end

#countInteger

Current number of acquired permits

Returns:

  • (Integer)

    Number of active 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

Yields:

  • (void)

    Block to execute within semaphore protection

Returns:

  • (Object)

    Result of the block execution



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

#limitInteger

Maximum number of permits

Returns:

  • (Integer)

    Semaphore limit



55
56
57
# File 'lib/fiber_job/concurrency.rb', line 55

def limit
  @semaphore.limit
end

#releasevoid

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