Class: Async::Limiter::Limited
- Defined in:
- lib/async/limiter/limited.rb
Overview
Limited concurrency limiter that enforces a strict task limit.
This implements a counting semaphore that limits the number of concurrent operations. It coordinates with timing strategies to provide both concurrency and rate limiting.
The Limited limiter uses a mutex and condition variable for thread-safe coordination, with support for deadline-aware timeout handling.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
- #Current count of active tasks.(countofactivetasks.) ⇒ Object readonly
-
#limit ⇒ Object
Returns the value of attribute limit.
- #The maximum number of concurrent tasks.(maximumnumberofconcurrenttasks.) ⇒ Object readonly
Attributes inherited from Generic
#Tags associated with this limiter for identification or categorization., #tags
Instance Method Summary collapse
- #acquired_count ⇒ Object
- #available_count ⇒ Object
-
#initialize(limit = 1, timing: Timing::None, parent: nil) ⇒ Limited
constructor
Initialize a limited concurrency limiter.
-
#limited? ⇒ Boolean
Check if a new task can be acquired.
- #reacquire_waiting_count ⇒ Object
-
#statistics ⇒ Object
Get current limiter statistics.
- #waiting_count ⇒ Object
Methods inherited from Generic
#acquire, #as_json, #async, #release, #sync, #to_json
Constructor Details
#initialize(limit = 1, timing: Timing::None, parent: nil) ⇒ Limited
Initialize a limited concurrency limiter.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/async/limiter/limited.rb', line 26 def initialize(limit = 1, timing: Timing::None, parent: nil) super(timing: timing, parent: parent) @limit = limit @count = 0 @waiting_count = 0 @reacquire_waiting_count = 0 @available = ConditionVariable.new end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
41 42 43 |
# File 'lib/async/limiter/limited.rb', line 41 def count @count end |
#Current count of active tasks.(countofactivetasks.) ⇒ Object (readonly)
41 |
# File 'lib/async/limiter/limited.rb', line 41 attr_reader :count |
#limit ⇒ Object
Returns the value of attribute limit.
38 39 40 |
# File 'lib/async/limiter/limited.rb', line 38 def limit @limit end |
#The maximum number of concurrent tasks.(maximumnumberofconcurrenttasks.) ⇒ Object (readonly)
38 |
# File 'lib/async/limiter/limited.rb', line 38 attr_reader :limit |
Instance Method Details
#acquired_count ⇒ Object
44 45 46 |
# File 'lib/async/limiter/limited.rb', line 44 def acquired_count @mutex.synchronize{@count} end |
#available_count ⇒ Object
49 50 51 |
# File 'lib/async/limiter/limited.rb', line 49 def available_count @mutex.synchronize{@limit - @count} end |
#limited? ⇒ Boolean
Check if a new task can be acquired.
65 66 67 |
# File 'lib/async/limiter/limited.rb', line 65 def limited? @mutex.synchronize{@count >= @limit} end |
#reacquire_waiting_count ⇒ Object
59 60 61 |
# File 'lib/async/limiter/limited.rb', line 59 def reacquire_waiting_count @mutex.synchronize{@reacquire_waiting_count} end |
#statistics ⇒ Object
Get current limiter statistics.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/async/limiter/limited.rb', line 84 def statistics @mutex.synchronize do { limit: @limit, count: @count, acquired_count: @count, available_count: @limit - @count, waiting_count: @waiting_count, reacquire_waiting_count: @reacquire_waiting_count, timing: @timing.statistics } end end |
#waiting_count ⇒ Object
54 55 56 |
# File 'lib/async/limiter/limited.rb', line 54 def waiting_count @mutex.synchronize{@waiting_count} end |