async-pool 0.7.0 → 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: 0c38dab2b02a509d2b46840bd95d0e5a79bb7dc3b952ac98ccfd4e8b8dee2f2b
4
- data.tar.gz: 5cc653ccd827b44850548caf5f0dfd2ca66732c69269eb85b83db0a991bdcb01
3
+ metadata.gz: cfc00b6087965cc5fc3f58ae151a0048fc8cf351aab9c06bdaf46b0eb65bb877
4
+ data.tar.gz: 7f9e3067d5bc75de995735ac4a025cb6cea4ee72e0e63201ec4f28c000301cc2
5
5
  SHA512:
6
- metadata.gz: 1ffea5398ccec2593a92949e5d8ef9835baef222547ee4718926cd25405bb43b96d6747ac404857f191bdbf723b980e0d6c5fcebc3770fbbad9e22b6fb3fb7b6
7
- data.tar.gz: c1e2e6104e7cb705c4329b17a4c144efd2af40ab544248da6a6a6226f348d2dd7dfbbd8715254471f8cdd5c9d425874791c2c49bf7ae9097cca3f2366e18401d
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.7.0"
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.7.0
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-06-23 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
metadata.gz.sig CHANGED
Binary file