async-limiter 1.2.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/async/limiter/concurrent.rb +20 -6
- data/lib/async/limiter/unlimited.rb +14 -0
- data/lib/async/limiter/version.rb +1 -1
- data/lib/async/limiter/window.rb +23 -15
- data/lib/async/limiter/window/continuous.rb +3 -2
- data/lib/async/limiter/window/fixed.rb +3 -2
- data/lib/async/limiter/window/sliding.rb +3 -2
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb03fc864de09582936ff16e60cd9a214020f66606ae489b09c673f5076d37e6
|
4
|
+
data.tar.gz: 6ec875d4dd7afa21bdde41d12b61f51b4898e93b644d2d90abea339c64810ae7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cde5a1bf23dadac86d333e8a43d6379df264ddd45684ea16aeba0b59f0c3141f9170bb70b37139a3868391c28fcb8a8e48f63399356a9fbacabd89fc369d39c7
|
7
|
+
data.tar.gz: 7b5b82b3141d057099b2f8fee90ce1d15ef56492106f44a751df64ad2a881098a4f43437d381d8f8753bbc4d9386c2a044d97a0542c4090320f9d91a7068c6e0
|
@@ -21,8 +21,8 @@ module Async
|
|
21
21
|
limit_blocking?
|
22
22
|
end
|
23
23
|
|
24
|
-
def async(parent: (@parent || Task.current), **options)
|
25
|
-
acquire
|
24
|
+
def async(*queue_args, parent: (@parent || Task.current), **options)
|
25
|
+
acquire(*queue_args)
|
26
26
|
parent.async(**options) do |task|
|
27
27
|
yield task
|
28
28
|
ensure
|
@@ -30,9 +30,23 @@ module Async
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
34
|
-
|
33
|
+
def sync(*queue_args)
|
34
|
+
acquire(*queue_args) do
|
35
|
+
yield(@parent || Task.current)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def acquire(*queue_args)
|
40
|
+
wait(*queue_args)
|
35
41
|
@count += 1
|
42
|
+
|
43
|
+
return unless block_given?
|
44
|
+
|
45
|
+
begin
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
release
|
49
|
+
end
|
36
50
|
end
|
37
51
|
|
38
52
|
def release
|
@@ -53,11 +67,11 @@ module Async
|
|
53
67
|
@count >= @limit
|
54
68
|
end
|
55
69
|
|
56
|
-
def wait
|
70
|
+
def wait(*queue_args)
|
57
71
|
fiber = Fiber.current
|
58
72
|
|
59
73
|
if blocking?
|
60
|
-
@waiting
|
74
|
+
@waiting.push(fiber, *queue_args) # queue_args used for custom queues
|
61
75
|
Task.yield while blocking?
|
62
76
|
end
|
63
77
|
rescue Exception # rubocop:disable Lint/RescueException
|
@@ -27,8 +27,22 @@ module Async
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
def sync(*queue_args)
|
31
|
+
acquire(*queue_args) do
|
32
|
+
yield(@parent || Task.current)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
30
36
|
def acquire
|
31
37
|
@count += 1
|
38
|
+
|
39
|
+
return unless block_given?
|
40
|
+
|
41
|
+
begin
|
42
|
+
yield
|
43
|
+
ensure
|
44
|
+
release
|
45
|
+
end
|
32
46
|
end
|
33
47
|
|
34
48
|
def release
|
data/lib/async/limiter/window.rb
CHANGED
@@ -25,7 +25,7 @@ module Async
|
|
25
25
|
@lock = lock
|
26
26
|
|
27
27
|
@waiting = queue
|
28
|
-
@
|
28
|
+
@scheduler = nil
|
29
29
|
|
30
30
|
@window_frame_start_time = NULL_TIME
|
31
31
|
@window_start_time = NULL_TIME
|
@@ -47,8 +47,8 @@ module Async
|
|
47
47
|
limit_blocking? || window_blocking? || window_frame_blocking?
|
48
48
|
end
|
49
49
|
|
50
|
-
def async(parent: (@parent || Task.current), **options)
|
51
|
-
acquire
|
50
|
+
def async(*queue_args, parent: (@parent || Task.current), **options)
|
51
|
+
acquire(*queue_args)
|
52
52
|
parent.async(**options) do |task|
|
53
53
|
yield task
|
54
54
|
ensure
|
@@ -56,8 +56,14 @@ module Async
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
def
|
60
|
-
|
59
|
+
def sync(*queue_args)
|
60
|
+
acquire(*queue_args) do
|
61
|
+
yield(@parent || Task.current)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def acquire(*queue_args)
|
66
|
+
wait(*queue_args)
|
61
67
|
@count += 1
|
62
68
|
|
63
69
|
current_time = Clock.now
|
@@ -145,13 +151,13 @@ module Async
|
|
145
151
|
@window_frame_start_time + window_frame <= Clock.now
|
146
152
|
end
|
147
153
|
|
148
|
-
def wait
|
154
|
+
def wait(*queue_args)
|
149
155
|
fiber = Fiber.current
|
150
156
|
|
151
157
|
# @waiting.any? check prevents fibers resumed via scheduler from
|
152
158
|
# slipping in operations before other waiting fibers get resumed.
|
153
159
|
if blocking? || @waiting.any?
|
154
|
-
@waiting
|
160
|
+
@waiting.push(fiber, *queue_args) # queue_args used for custom queues
|
155
161
|
schedule if schedule?
|
156
162
|
loop do
|
157
163
|
Task.yield # run this line at least once
|
@@ -164,34 +170,36 @@ module Async
|
|
164
170
|
end
|
165
171
|
|
166
172
|
def schedule?
|
167
|
-
@
|
173
|
+
@scheduler.nil? &&
|
168
174
|
@waiting.any? &&
|
169
175
|
!limit_blocking?
|
170
176
|
end
|
171
177
|
|
172
178
|
# Schedule resuming waiting tasks.
|
173
179
|
def schedule(parent: @parent || Task.current)
|
174
|
-
@
|
175
|
-
parent.async { |task|
|
180
|
+
@scheduler ||=
|
181
|
+
parent.async(transient: true) { |task|
|
182
|
+
task.annotate("scheduling tasks for #{self.class}.")
|
183
|
+
|
176
184
|
while @waiting.any? && !limit_blocking?
|
177
|
-
delay = [next_acquire_time -
|
185
|
+
delay = [next_acquire_time - Clock.now, 0].max
|
178
186
|
task.sleep(delay) if delay.positive?
|
179
187
|
resume_waiting
|
180
188
|
end
|
181
189
|
|
182
|
-
@
|
190
|
+
@scheduler = nil
|
183
191
|
}
|
184
192
|
end
|
185
193
|
|
186
194
|
def reschedule?
|
187
|
-
@
|
195
|
+
@scheduler &&
|
188
196
|
@waiting.any? &&
|
189
197
|
!limit_blocking?
|
190
198
|
end
|
191
199
|
|
192
200
|
def reschedule
|
193
|
-
@
|
194
|
-
@
|
201
|
+
@scheduler.stop
|
202
|
+
@scheduler = nil
|
195
203
|
|
196
204
|
schedule
|
197
205
|
end
|
@@ -4,14 +4,15 @@ module Async
|
|
4
4
|
module Limiter
|
5
5
|
class Window
|
6
6
|
class Continuous < Window
|
7
|
-
def initialize(limit = 1, window: 1, parent: nil, lock: true)
|
7
|
+
def initialize(limit = 1, window: 1, parent: nil, lock: true, queue: [])
|
8
8
|
super(
|
9
9
|
limit,
|
10
10
|
type: :sliding, # type doesn't matter, but sliding is less work
|
11
11
|
burstable: false,
|
12
12
|
window: window,
|
13
13
|
parent: parent,
|
14
|
-
lock: lock
|
14
|
+
lock: lock,
|
15
|
+
queue: queue
|
15
16
|
)
|
16
17
|
end
|
17
18
|
end
|
@@ -4,14 +4,15 @@ module Async
|
|
4
4
|
module Limiter
|
5
5
|
class Window
|
6
6
|
class Fixed < Window
|
7
|
-
def initialize(limit = 1, window: 1, parent: nil, lock: true)
|
7
|
+
def initialize(limit = 1, window: 1, parent: nil, lock: true, queue: [])
|
8
8
|
super(
|
9
9
|
limit,
|
10
10
|
type: :fixed,
|
11
11
|
burstable: true,
|
12
12
|
window: window,
|
13
13
|
parent: parent,
|
14
|
-
lock: lock
|
14
|
+
lock: lock,
|
15
|
+
queue: queue
|
15
16
|
)
|
16
17
|
end
|
17
18
|
end
|
@@ -4,14 +4,15 @@ module Async
|
|
4
4
|
module Limiter
|
5
5
|
class Window
|
6
6
|
class Sliding < Window
|
7
|
-
def initialize(limit = 1, window: 1, parent: nil, lock: true)
|
7
|
+
def initialize(limit = 1, window: 1, parent: nil, lock: true, queue: [])
|
8
8
|
super(
|
9
9
|
limit,
|
10
10
|
type: :sliding,
|
11
11
|
burstable: true,
|
12
12
|
window: window,
|
13
13
|
parent: parent,
|
14
|
-
lock: lock
|
14
|
+
lock: lock,
|
15
|
+
queue: queue
|
15
16
|
)
|
16
17
|
end
|
17
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-limiter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Sutic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async
|
@@ -16,70 +16,70 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.27'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.27'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: async-rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.15'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.15'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
47
|
+
version: '3.10'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
54
|
+
version: '3.10'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rubocop-rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '2.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '2.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: standard
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
75
|
+
version: '0.9'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
82
|
+
version: '0.9'
|
83
83
|
description:
|
84
84
|
email: code@brunosutic.com
|
85
85
|
executables: []
|