async-pool 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/async/pool/controller.rb +29 -19
- data/lib/async/pool/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ad379b03305a3175cb279cdb733802af5d18c8dd4286aa405af0266326ded2e
|
4
|
+
data.tar.gz: a14e523cbf72c0be0153c5281a66e4b15b97bbf46f3ebc1f4f2903b642d84d32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
@
|
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
|
-
@
|
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
|
-
@
|
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
|
-
@
|
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
|
-
# @
|
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
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
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
|
-
|
203
|
+
@available.pop
|
192
204
|
end
|
193
205
|
end
|
194
|
-
|
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
|
|
data/lib/async/pool/version.rb
CHANGED