Class: Async::Limiter::Window
- Inherits:
-
Object
- Object
- Async::Limiter::Window
show all
- Defined in:
- lib/async/limiter/window.rb,
lib/async/limiter/window/fixed.rb,
lib/async/limiter/window/sliding.rb,
lib/async/limiter/window/continuous.rb
Defined Under Namespace
Classes: Continuous, Fixed, Sliding
Constant Summary
collapse
- TYPES =
%i[fixed sliding].freeze
- NULL_TIME =
-1
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(limit = 1, type: :fixed, window: 1, parent: nil, burstable: true, lock: true, queue: []) ⇒ Window
Returns a new instance of Window.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/async/limiter/window.rb', line 18
def initialize(limit = 1, type: :fixed, window: 1, parent: nil,
burstable: true, lock: true, queue: [])
@count = 0
@input_limit = @limit = limit
@type = type
@input_window = @window = window
@parent = parent
@burstable = burstable
@lock = lock
@waiting = queue
@scheduler = nil
@yield_wait = false
@yield_notification = Notification.new
@window_frame_start_time = NULL_TIME
@window_start_time = NULL_TIME
@window_count = 0
update_concurrency
validate!
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
12
13
14
|
# File 'lib/async/limiter/window.rb', line 12
def count
@count
end
|
#lock ⇒ Object
Returns the value of attribute lock.
16
17
18
|
# File 'lib/async/limiter/window.rb', line 16
def lock
@lock
end
|
#type ⇒ Object
Returns the value of attribute type.
14
15
16
|
# File 'lib/async/limiter/window.rb', line 14
def type
@type
end
|
Instance Method Details
#acquire(*queue_args) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/async/limiter/window.rb', line 68
def acquire(*queue_args)
wait(*queue_args)
@count += 1
current_time = Clock.now
if window_changed?(current_time)
@window_start_time =
if @type == :sliding
current_time
elsif @type == :fixed
(current_time / @window).to_i * @window
else
raise "invalid type #{@type}"
end
@window_count = 1
else
@window_count += 1
end
@window_frame_start_time = current_time
return unless block_given?
begin
yield
ensure
release
end
end
|
#async(*queue_args, parent: (@parent || Task.current), **options) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/async/limiter/window.rb', line 53
def async(*queue_args, parent: (@parent || Task.current), **options)
acquire(*queue_args)
parent.async(**options) do |task|
yield task
ensure
release
end
end
|
#blocking? ⇒ Boolean
49
50
51
|
# File 'lib/async/limiter/window.rb', line 49
def blocking?
limit_blocking? || window_blocking? || window_frame_blocking?
end
|
#limit ⇒ Object
41
42
43
|
# File 'lib/async/limiter/window.rb', line 41
def limit
@input_limit
end
|
#limit=(new_limit) ⇒ Object
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/async/limiter/window.rb', line 107
def limit=(new_limit)
validate_limit!(new_limit)
@input_limit = @limit = new_limit
update_concurrency
resume_waiting
reschedule if reschedule?
limit
end
|
#release ⇒ Object
100
101
102
103
104
105
|
# File 'lib/async/limiter/window.rb', line 100
def release
@count -= 1
resume_waiting if @lock
end
|
#sync(*queue_args) ⇒ Object
62
63
64
65
66
|
# File 'lib/async/limiter/window.rb', line 62
def sync(*queue_args)
acquire(*queue_args) do
yield Task.current
end
end
|
#window ⇒ Object
45
46
47
|
# File 'lib/async/limiter/window.rb', line 45
def window
@input_window
end
|
#window=(new_window) ⇒ Object
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/async/limiter/window.rb', line 118
def window=(new_window)
validate_window!(new_window)
@input_window = @window = new_window
update_concurrency
resume_waiting
reschedule if reschedule?
window
end
|