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
#Registry-like object for utilization metrics., #Tags associated with this limiter for identification or categorization., #tags, #utilization
Instance Method Summary collapse
- #acquired_count ⇒ Object
- #available_count ⇒ Object
-
#initialize(limit = 1, **options) ⇒ 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, **options) ⇒ Limited
Initialize a limited concurrency limiter.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/async/limiter/limited.rb', line 25 def initialize(limit = 1, **) super(**) @limit = limit @count = 0 @available = ConditionVariable.new @acquired_count_metric = @utilization.metric(:acquired_count) @available_count_metric = @utilization.metric(:available_count) @waiting_count_metric = @utilization.metric(:waiting_count) @reacquire_waiting_count_metric = @utilization.metric(:reacquire_waiting_count) update_utilization_metrics end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
45 46 47 |
# File 'lib/async/limiter/limited.rb', line 45 def count @count end |
#Current count of active tasks.(countofactivetasks.) ⇒ Object (readonly)
45 |
# File 'lib/async/limiter/limited.rb', line 45 attr_reader :count |
#limit ⇒ Object
Returns the value of attribute limit.
42 43 44 |
# File 'lib/async/limiter/limited.rb', line 42 def limit @limit end |
#The maximum number of concurrent tasks.(maximumnumberofconcurrenttasks.) ⇒ Object (readonly)
42 |
# File 'lib/async/limiter/limited.rb', line 42 attr_reader :limit |
Instance Method Details
#acquired_count ⇒ Object
48 49 50 |
# File 'lib/async/limiter/limited.rb', line 48 def acquired_count @mutex.synchronize{@count} end |
#available_count ⇒ Object
53 54 55 |
# File 'lib/async/limiter/limited.rb', line 53 def available_count @mutex.synchronize{@limit - @count} end |
#limited? ⇒ Boolean
Check if a new task can be acquired.
69 70 71 |
# File 'lib/async/limiter/limited.rb', line 69 def limited? @mutex.synchronize{@count >= @limit} end |
#reacquire_waiting_count ⇒ Object
63 64 65 |
# File 'lib/async/limiter/limited.rb', line 63 def reacquire_waiting_count @reacquire_waiting_count_metric.value end |
#statistics ⇒ Object
Get current limiter statistics.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/async/limiter/limited.rb', line 89 def statistics @mutex.synchronize do { limit: @limit, count: @count, acquired_count: @count, available_count: @limit - @count, waiting_count: @waiting_count_metric.value, reacquire_waiting_count: @reacquire_waiting_count_metric.value, timing: @timing.statistics } end end |
#waiting_count ⇒ Object
58 59 60 |
# File 'lib/async/limiter/limited.rb', line 58 def waiting_count @waiting_count_metric.value end |