async-limiter 1.1.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cdf40ab309d5d7b7ca00fb0dcaedb33b70a1fca2d64beda3372315e15c010cb
4
- data.tar.gz: 11d3b4dfb522dd797fceccf3584d1a8cf09754242243dd9691fde4e21f3135d5
3
+ metadata.gz: 89371390667823819a23be2b9b32fa67835de748ed46e456e3bef72af1086f9c
4
+ data.tar.gz: 2cb73676f8f6d2f78d9eb7dfe5f7842076fc2121be674ce13f61ba26075633a3
5
5
  SHA512:
6
- metadata.gz: b24f90a04704a5bcb01147d801b00ec58b96f22d853a7edbde6357f115fc3e903b57602609c9c2be8e622908eb75e1f6cfd21919e555616b6f6178d2380f55e7
7
- data.tar.gz: 29c23c6e2868214742540be7e9aeb4678eb9f9ff9dd4625e8eab41f129db05b459f1e4a053245015876fc9086b6cadfc78159890f2493e06ca6824ab70f708c9
6
+ metadata.gz: 1eb18ea438a0aa21647a7630f11cdfc0318e2b1e5b9395f9476e1c05a1865e37a4a994e0fd877bca06aa62de1ce1429662fec1b8d450b8e88bd1eb1ab90ba105
7
+ data.tar.gz: d0fb87462427dfef33c2967a594b6df029579f3bfd8afaf1f9fe6445abff60f9d147d4838c3edf9ded900488203669f3e53c04f2bde3c09edfafcd1181078148
@@ -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 acquire
34
- wait
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 << fiber
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
@@ -1,5 +1,5 @@
1
1
  module Async
2
2
  module Limiter
3
- VERSION = "1.1.0"
3
+ VERSION = "1.5.0"
4
4
  end
5
5
  end
@@ -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 acquire
60
- wait
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
@@ -78,6 +84,14 @@ module Async
78
84
  end
79
85
 
80
86
  @window_frame_start_time = current_time
87
+
88
+ return unless block_given?
89
+
90
+ begin
91
+ yield
92
+ ensure
93
+ release
94
+ end
81
95
  end
82
96
 
83
97
  def release
@@ -137,13 +151,13 @@ module Async
137
151
  @window_frame_start_time + window_frame <= Clock.now
138
152
  end
139
153
 
140
- def wait
154
+ def wait(*queue_args)
141
155
  fiber = Fiber.current
142
156
 
143
157
  # @waiting.any? check prevents fibers resumed via scheduler from
144
158
  # slipping in operations before other waiting fibers get resumed.
145
159
  if blocking? || @waiting.any?
146
- @waiting << fiber
160
+ @waiting.push(fiber, *queue_args) # queue_args used for custom queues
147
161
  schedule if schedule?
148
162
  loop do
149
163
  Task.yield # run this line at least once
@@ -165,6 +179,8 @@ module Async
165
179
  def schedule(parent: @parent || Task.current)
166
180
  @scheduler_task ||=
167
181
  parent.async { |task|
182
+ task.annotate("scheduling tasks for #{self.class}.")
183
+
168
184
  while @waiting.any? && !limit_blocking?
169
185
  delay = [next_acquire_time - Async::Clock.now, 0].max
170
186
  task.sleep(delay) if delay.positive?
@@ -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.1.0
4
+ version: 1.5.0
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-10-11 00:00:00.000000000 Z
11
+ date: 2020-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async