async-pool 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df755b3a1ec23182af37b80f498c301e42b0e333c97e663b4f065453c66d36cd
4
- data.tar.gz: 1af8d6d7b3f67fcaad73527dbf278133813564ff152c0733cdf4e7da88704334
3
+ metadata.gz: 6ad379b03305a3175cb279cdb733802af5d18c8dd4286aa405af0266326ded2e
4
+ data.tar.gz: a14e523cbf72c0be0153c5281a66e4b15b97bbf46f3ebc1f4f2903b642d84d32
5
5
  SHA512:
6
- metadata.gz: 941097d316256c77a43a700468ad8dbd2549a3c433c1a8393913fbeb72d06886b6f0f59d1689902b3ea923e02a2e90ee3dd9269cb14a6b619160aa025d864a80
7
- data.tar.gz: a8d87b81f9ec2942d9bb7547b712d239df6fe072f39eb9c6d3d6bc81b16c8a76a79f3ff3fe0746ab7bc8428228fcf391d8a05d7606b13729de878dae80185c96
6
+ metadata.gz: e6977d59abe105dc69cf481500307f1105d5fb3aaecf44804c71202892fa59869b23672b059d396ae1d538d9859a372a4ddb3d21d75a000f172f0ec6a83df9b6
7
+ data.tar.gz: df123b18dd484354fd59e356f0957e2b95965cf28889592191b92b6f2280cbd96f5d035c3229734382e5d306882b3acfc96d614dff60cbb48f546871b7914f26
@@ -32,7 +32,9 @@ module Async
32
32
 
33
33
  def initialize(constructor, limit: nil)
34
34
  @resources = {}
35
- @available = Async::Notification.new
35
+
36
+ @available = []
37
+ @notification = Async::Notification.new
36
38
 
37
39
  @limit = limit
38
40
 
@@ -43,6 +45,10 @@ module Async
43
45
  # @attr [Hash<Resource, Integer>] all allocated resources, and their associated usage.
44
46
  attr :resources
45
47
 
48
+ def size
49
+ @resources.size
50
+ end
51
+
46
52
  # Whether the pool has any active resources.
47
53
  def active?
48
54
  !@resources.empty?
@@ -59,7 +65,7 @@ module Async
59
65
 
60
66
  # Wait until a pool resource has been freed.
61
67
  def wait
62
- @available.wait
68
+ @notification.wait
63
69
  end
64
70
 
65
71
  def empty?
@@ -131,7 +137,7 @@ module Async
131
137
 
132
138
  resource.close
133
139
 
134
- @available.signal
140
+ @notification.signal
135
141
  end
136
142
 
137
143
  protected
@@ -150,20 +156,21 @@ module Async
150
156
  Async.logger.debug(self) {"Reuse #{resource}"}
151
157
 
152
158
  @resources[resource] -= 1
159
+ @available.push(resource)
153
160
 
154
- @available.signal
161
+ @notification.signal
155
162
  end
156
163
 
157
164
  def wait_for_resource
158
165
  # If we fail to create a resource (below), we will end up waiting for one to become resources.
159
166
  until resource = available_resource
160
- @available.wait
167
+ @notification.wait
161
168
  end
162
169
 
163
- Async.logger.debug(self) {"Wait for resource #{resource}"}
170
+ Async.logger.debug(self) {"Wait for resource -> #{resource}"}
164
171
 
165
172
  # if resource.concurrency > 1
166
- # @available.signal
173
+ # @notification.signal
167
174
  # end
168
175
 
169
176
  return resource
@@ -173,27 +180,30 @@ module Async
173
180
  # This might return nil, which means creating the resource failed.
174
181
  if resource = @constructor.call
175
182
  @resources[resource] = 1
183
+
184
+ @available.push(resource) if resource.concurrency > 1
176
185
  end
177
186
 
178
187
  return resource
179
188
  end
180
189
 
181
190
  def available_resource
182
- # TODO This is a linear search... not ideal, but simple for now.
183
- @resources.each do |resource, count|
184
- if count < resource.concurrency
185
- # We want to use this resource... but is it connected?
186
- if resource.viable?
187
- @resources[resource] += 1
188
-
189
- return resource
191
+ @guard.acquire do
192
+ while resource = @available.last
193
+ if usage = @resources[resource] and usage < resource.concurrency
194
+ if resource.viable?
195
+ @resources[resource] += 1
196
+
197
+ return resource
198
+ else
199
+ retire(resource)
200
+ @available.pop
201
+ end
190
202
  else
191
- retire(resource)
203
+ @available.pop
192
204
  end
193
205
  end
194
- end
195
-
196
- @guard.acquire do
206
+
197
207
  if @limit.nil? or @resources.size < @limit
198
208
  Async.logger.debug(self) {"No resources resources, allocating new one..."}
199
209
 
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module Pool
23
- VERSION = "0.1.0"
23
+ VERSION = "0.2.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams