async-limiter 1.0.0 → 1.4.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: 6d203b60d1b006ce7dc199fd37dbfeac43468324a083a02a233dbf7fe1ebc61f
4
- data.tar.gz: f7cd17453e047205743648458058f30365d795514fba956dca1aa910ab34816e
3
+ metadata.gz: fb0c5c05d9810ad2fa9552b2ef47a8de1a3f7a6a0d3d7b5ed06966123d3aec4d
4
+ data.tar.gz: 30515013abce6931c53c67cbec22aab86407bd540169bcf66d39be1c97b3e6e3
5
5
  SHA512:
6
- metadata.gz: 2721ba28df1d8aca742ba974ace3146d136734e9482c39429957dd0ff9022843eef5dc6bdbda9c9aa60017c019ab2011907a31a6d30d8a16f7392b2c70daf04a
7
- data.tar.gz: 7ea2cbb70d1f192d947d42594711fdf37da8f86a56d624925ec3b8fdb31957248ecbb1b945f62f7c4dfc755c947ede0ec65f8035c2806fd942ebd60ab430194d
6
+ metadata.gz: cc2899bf484bbdc536ec30198d306357799e9f877c731b90ca47547690f95795b6ee757b3186ba73479a3b02dfb2ef75f7b6dc51ff1d24e2689284c6581aaf77
7
+ data.tar.gz: 1fca09443942de54eeec4c16c2b0b6612be2fd7fc1be2efba01543cd720f4c4e055cf75cb37746ffc806fd8f7fbfe68cdf35592a65d9499ec6b7beb003305cd9
@@ -8,10 +8,10 @@ module Async
8
8
 
9
9
  attr_reader :limit
10
10
 
11
- def initialize(limit = 1, parent: nil)
11
+ def initialize(limit = 1, parent: nil, queue: [])
12
12
  @count = 0
13
13
  @limit = limit
14
- @waiting = []
14
+ @waiting = queue
15
15
  @parent = parent
16
16
 
17
17
  validate!
@@ -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.0.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -15,7 +15,7 @@ module Async
15
15
  attr_reader :lock
16
16
 
17
17
  def initialize(limit = 1, type: :fixed, window: 1, parent: nil,
18
- burstable: true, lock: true)
18
+ burstable: true, lock: true, queue: [])
19
19
  @count = 0
20
20
  @input_limit = @limit = limit
21
21
  @type = type
@@ -24,7 +24,7 @@ module Async
24
24
  @burstable = burstable
25
25
  @lock = lock
26
26
 
27
- @waiting = []
27
+ @waiting = queue
28
28
  @scheduler_task = nil
29
29
 
30
30
  @window_frame_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 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
@@ -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.0.0
4
+ version: 1.4.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-10 00:00:00.000000000 Z
11
+ date: 2020-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async