Class: Async::Limiter::Limited

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

Attributes inherited from Generic

#Tags associated with this limiter for identification or categorization., #tags

Instance Method Summary collapse

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

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

#limitObject

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_countObject



44
45
46
# File 'lib/async/limiter/limited.rb', line 44

def acquired_count
	@mutex.synchronize{@count}
end

#available_countObject



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.

Returns:

  • (Boolean)


65
66
67
# File 'lib/async/limiter/limited.rb', line 65

def limited?
	@mutex.synchronize{@count >= @limit}
end

#reacquire_waiting_countObject



59
60
61
# File 'lib/async/limiter/limited.rb', line 59

def reacquire_waiting_count
	@mutex.synchronize{@reacquire_waiting_count}
end

#statisticsObject

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_countObject



54
55
56
# File 'lib/async/limiter/limited.rb', line 54

def waiting_count
	@mutex.synchronize{@waiting_count}
end