async-pool 0.6.1 → 0.7.1

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: f1ad63ef9b3e6a6e062277b976adefe9e254b9717aa47a33bf9ede37f5009e86
4
- data.tar.gz: 362cd05e427993817b230ff97ae1844f2299cb0fc5544dbdfafa690e501cbaab
3
+ metadata.gz: cfc00b6087965cc5fc3f58ae151a0048fc8cf351aab9c06bdaf46b0eb65bb877
4
+ data.tar.gz: 7f9e3067d5bc75de995735ac4a025cb6cea4ee72e0e63201ec4f28c000301cc2
5
5
  SHA512:
6
- metadata.gz: 2484b795cff61e71ff9c1fd2207c7600f809ed7b8352e86e084079dedf0b8451c6c5f096aedd25714146410db3287685e6aa863c057923a36e7d30380bc25eee
7
- data.tar.gz: 053a39d997a23028079fc17517f5d45bf370dae42e834b55c3956cc5811912993a0f64ddd91d2d4f50e1a2fbea85d1083fd6181871c8cec64f67ccbd0b3a915f
6
+ metadata.gz: e6933852df812446375faf0c0c921c137bdd972beec9f1e392f6bdfc0c803ddf1cf62151323fbd18f8431c9ab1021eaac2c90ab461f03679d3e93cebfbc039b0
7
+ data.tar.gz: 76a83cc5cc6a5dc2230ae6ad3eedc12a0e4fc24170d7391d5a1c032553d4a5653fc89b12060e331bd64687fefb5308d64785bbdc9cc70701b3f86e394de718f2
checksums.yaml.gz.sig CHANGED
Binary file
@@ -13,11 +13,19 @@ require 'async/semaphore'
13
13
 
14
14
  module Async
15
15
  module Pool
16
+ # A resource pool controller.
16
17
  class Controller
18
+ # Create a new resource pool, using the given block to create new resources.
17
19
  def self.wrap(**options, &block)
18
20
  self.new(block, **options)
19
21
  end
20
22
 
23
+ # Create a new resource pool.
24
+ #
25
+ # @parameter constructor [Proc] A block which creates a new resource.
26
+ # @parameter limit [Integer | Nil] The maximum number of resources that this pool can have at any given time. If nil, the pool can have an unlimited number of resources.
27
+ # @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource.
28
+ # @parameter policy [Policy] The pool policy.
21
29
  def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
22
30
  @constructor = constructor
23
31
  @limit = limit
@@ -45,6 +53,7 @@ module Async
45
53
  # @attribute [Integer] The maximum number of resources that this pool can have at any given time.
46
54
  attr_accessor :limit
47
55
 
56
+ # Generate a human-readable representation of the pool.
48
57
  def to_s
49
58
  if @resources.empty?
50
59
  "\#<#{self.class}(#{usage_string})>"
@@ -53,6 +62,7 @@ module Async
53
62
  end
54
63
  end
55
64
 
65
+ # Generate a JSON representation of the pool.
56
66
  def as_json(...)
57
67
  {
58
68
  limit: @limit,
@@ -62,6 +72,7 @@ module Async
62
72
  }
63
73
  end
64
74
 
75
+ # Generate a JSON representation of the pool.
65
76
  def to_json(...)
66
77
  as_json.to_json(...)
67
78
  end
@@ -71,6 +82,7 @@ module Async
71
82
  @guard.limit
72
83
  end
73
84
 
85
+ # Set the maximum number of concurrent tasks that can be creating a new resource.
74
86
  def concurrency= value
75
87
  @guard.limit = value
76
88
  end
@@ -81,6 +93,7 @@ module Async
81
93
  # @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
82
94
  attr :resources
83
95
 
96
+ # The number of resources in the pool.
84
97
  def size
85
98
  @resources.size
86
99
  end
@@ -109,10 +122,12 @@ module Async
109
122
  @notification.wait
110
123
  end
111
124
 
125
+ # Whether the pool is empty.
112
126
  def empty?
113
127
  @resources.empty?
114
128
  end
115
129
 
130
+ # Acquire a resource from the pool. If a block is provided, the resource will be released after the block has been executed.
116
131
  def acquire
117
132
  resource = wait_for_resource
118
133
 
@@ -139,6 +154,7 @@ module Async
139
154
  retire(resource) unless processed
140
155
  end
141
156
 
157
+ # Close all resources in the pool.
142
158
  def close
143
159
  @available.clear
144
160
 
@@ -146,7 +162,7 @@ module Async
146
162
  resource, usage = pair
147
163
 
148
164
  if usage > 0
149
- Console.logger.warn(self, resource: resource, usage: usage) {"Closing resource while still in use!"}
165
+ Console.warn(self, resource: resource, usage: usage) {"Closing resource while still in use!"}
150
166
  end
151
167
 
152
168
  resource.close
@@ -156,8 +172,8 @@ module Async
156
172
  end
157
173
 
158
174
  # Retire (and close) all unused resources. If a block is provided, it should implement the desired functionality for unused resources.
159
- # @param retain [Integer] the minimum number of resources to retain.
160
- # @yield resource [Resource] unused resources.
175
+ # @parameter retain [Integer] the minimum number of resources to retain.
176
+ # @yields {|resource| ...} Any unused resource.
161
177
  def prune(retain = 0)
162
178
  unused = []
163
179
 
@@ -190,8 +206,9 @@ module Async
190
206
  return unused.size
191
207
  end
192
208
 
209
+ # Retire a specific resource.
193
210
  def retire(resource)
194
- Console.logger.debug(self) {"Retire #{resource}"}
211
+ Console.debug(self) {"Retire #{resource}"}
195
212
 
196
213
  @resources.delete(resource)
197
214
 
@@ -244,10 +261,10 @@ module Async
244
261
  # end
245
262
 
246
263
  def reuse(resource)
247
- Console.logger.debug(self) {"Reuse #{resource}"}
264
+ Console.debug(self) {"Reuse #{resource}"}
248
265
  usage = @resources[resource]
249
266
 
250
- if usage.zero?
267
+ if usage.nil? || usage.zero?
251
268
  raise "Trying to reuse unacquired resource: #{resource}!"
252
269
  end
253
270
 
@@ -269,7 +286,7 @@ module Async
269
286
  @notification.wait
270
287
  end
271
288
 
272
- Console.logger.debug(self) {"Wait for resource -> #{resource}"}
289
+ Console.debug(self) {"Wait for resource -> #{resource}"}
273
290
 
274
291
  # if resource.concurrency > 1
275
292
  # @notification.signal
@@ -336,7 +353,7 @@ module Async
336
353
  end
337
354
 
338
355
  if @limit.nil? or @resources.size < @limit
339
- Console.logger.debug(self) {"No available resources, allocating new one..."}
356
+ Console.debug(self) {"No available resources, allocating new one..."}
340
357
 
341
358
  return create_resource
342
359
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2019-2022, by Samuel Williams.
4
+ # Copyright, 2019-2024, by Samuel Williams.
5
5
 
6
6
  require 'console/logger'
7
7
 
@@ -17,6 +17,9 @@ module Async
17
17
  self.new
18
18
  end
19
19
 
20
+ # Create a new resource.
21
+ #
22
+ # @parameter concurrency [Integer] The concurrency of this resource.
20
23
  def initialize(concurrency = 1)
21
24
  @concurrency = concurrency
22
25
  @closed = false
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Async
7
7
  module Pool
8
- VERSION = "0.6.1"
8
+ VERSION = "0.7.1"
9
9
  end
10
10
  end
data/lib/async/pool.rb CHANGED
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2019-2022, by Samuel Williams.
4
+ # Copyright, 2019-2024, by Samuel Williams.
5
5
 
6
6
  require_relative 'pool/version'
7
7
  require_relative 'pool/controller'
8
+
9
+ # @namespace
10
+ module Async
11
+ # @namespace
12
+ module Pool
13
+ end
14
+ end
data/readme.md CHANGED
@@ -4,44 +4,11 @@ Provides support for connection pooling both singleplex and multiplex resources.
4
4
 
5
5
  [![Development Status](https://github.com/socketry/async-pool/workflows/Test/badge.svg)](https://github.com/socketry/async-pool/actions?workflow=Test)
6
6
 
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ``` ruby
12
- gem 'async-pool'
13
- ```
14
-
15
- And then execute:
16
-
17
- ``` bash
18
- $ bundle
19
- ```
20
-
21
- Or install it yourself as:
22
-
23
- ``` bash
24
- $ gem install async-pool
25
- ```
26
-
27
7
  ## Usage
28
8
 
29
- `Async::Pool::Controller` provides support for both singleplex (one stream at a time) and multiplex resources (multiple streams at a time).
30
-
31
- `Async::Pool::Resource` is provided as an interface and to document how to use the pools. However, you wouldn't need to use this in practice and just implement the appropriate interface on your own objects.
32
-
33
- ``` ruby
34
- pool = Async::Pool::Controller.new(Async::Pool::Resource)
35
-
36
- pool.acquire do |resource|
37
- # resource is implicitly released when exiting the block.
38
- end
39
-
40
- resource = pool.acquire
9
+ Please see the [project documentation](https://socketry.github.io/async-pool/) for more details.
41
10
 
42
- # Return the resource back to the pool:
43
- pool.release(resource)
44
- ```
11
+ - [Getting Started](https://socketry.github.io/async-pool/guides/getting-started/index) - This guide explains how to use the `async-pool` gem to manage connection pooling.
45
12
 
46
13
  ## Contributing
47
14
 
@@ -55,8 +22,8 @@ We welcome contributions to this project.
55
22
 
56
23
  ### Developer Certificate of Origin
57
24
 
58
- This project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.
25
+ In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
59
26
 
60
- ### Contributor Covenant
27
+ ### Community Guidelines
61
28
 
62
- This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
29
+ This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
data.tar.gz.sig CHANGED
Binary file
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.6.1
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -40,7 +40,7 @@ cert_chain:
40
40
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
41
41
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
42
42
  -----END CERTIFICATE-----
43
- date: 2024-04-22 00:00:00.000000000 Z
43
+ date: 2024-08-03 00:00:00.000000000 Z
44
44
  dependencies:
45
45
  - !ruby/object:Gem::Dependency
46
46
  name: async
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  requirements: []
93
- rubygems_version: 3.5.3
93
+ rubygems_version: 3.5.11
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: A singleplex and multiplex resource pool for implementing robust clients.
metadata.gz.sig CHANGED
Binary file