quack_pool 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +43 -0
- data/Rakefile +7 -0
- data/lib/quack_pool.rb +54 -0
- data/spec/quack_pool_spec.rb +90 -0
- data/spec/spec_helper.rb +100 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e99542e0d54f7d86d03719c0cdd3caed78da2dc5
|
4
|
+
data.tar.gz: 696ec9dedffd4e8d5a4bad879efc5b1a60db6c0a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ee146d9ed9182397ba63a8a2c9b8e5868486ea21a4581ab47369ca1abf02af809f0d45b69410c2d4fe0db529624270d9fe0728dcb5b74cb321e59c3140dcbc7
|
7
|
+
data.tar.gz: 54c20fa04f7dff1f3bf759289bf3166d231a35735aca1e5fdf31323a6df9be1396936d314f4d3c9421a9ec76836cbb558b0991d7282e0b45b3ba532f802bbbc4
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Rob Fors
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# QuackPool
|
2
|
+
*QuackPool* is a ruby gem. It is a simple resource pool that accepts a resource class to build the pool's resources from.
|
3
|
+
|
4
|
+
# Features
|
5
|
+
* thread safe
|
6
|
+
* only builds new resources as needed
|
7
|
+
* you can specify a max resource limit
|
8
|
+
|
9
|
+
# Intall
|
10
|
+
`gem install quack_pool`
|
11
|
+
|
12
|
+
# Example
|
13
|
+
```ruby
|
14
|
+
require 'quack_pool'
|
15
|
+
|
16
|
+
class Resource
|
17
|
+
end
|
18
|
+
|
19
|
+
# unlimited resources
|
20
|
+
pool = QuackPool.new(resource_class: Resource)
|
21
|
+
resource1 = pool.release_resource
|
22
|
+
resource2 = pool.release_resource
|
23
|
+
# use resources ...
|
24
|
+
pool.absorb_resource(resource1)
|
25
|
+
pool.absorb_resource(resource2)
|
26
|
+
|
27
|
+
# limited resources
|
28
|
+
pool = QuackPool.new(resource_class: Resource, size: 2)
|
29
|
+
resource1 = pool.release_resource
|
30
|
+
thread = Thread.new do
|
31
|
+
resource2 = pool.release_resource
|
32
|
+
# use resource ...
|
33
|
+
sleep 2
|
34
|
+
pool.absorb_resource(resource1)
|
35
|
+
end
|
36
|
+
sleep 1
|
37
|
+
resource3 = pool.release_resource # will block until a resource is available
|
38
|
+
# use resources ...
|
39
|
+
pool.absorb_resource(resource1)
|
40
|
+
pool.absorb_resource(resource3)
|
41
|
+
thread.join
|
42
|
+
|
43
|
+
```
|
data/Rakefile
ADDED
data/lib/quack_pool.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
class QuackPool
|
2
|
+
|
3
|
+
def initialize(resource_class: , size: Float::INFINITY)
|
4
|
+
raise ArgumentError, "'resource_class' must respond_to 'new'" unless resource_class.respond_to?(:new)
|
5
|
+
@resource_class = resource_class
|
6
|
+
raise ArgumentError, "'size' must be an Integer" unless size.is_a?(Integer) || size == Float::INFINITY
|
7
|
+
@max_size = size
|
8
|
+
@resources = []
|
9
|
+
@available_resources = []
|
10
|
+
@mutex = Mutex.new
|
11
|
+
@condition_variable = ConditionVariable.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def release_resource
|
15
|
+
@mutex.synchronize do
|
16
|
+
if @available_resources.any?
|
17
|
+
release_available_resource
|
18
|
+
elsif @resources.length < @max_size
|
19
|
+
build_new_resource
|
20
|
+
else
|
21
|
+
release_next_available_resource
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def absorb_resource(resource)
|
27
|
+
@mutex.synchronize do
|
28
|
+
raise ArgumentError, "'resource' does not belong to this pool" unless @resources.include?(resource)
|
29
|
+
raise ArgumentError, "'resource' already in pool" if @available_resources.include?(resource)
|
30
|
+
@available_resources.push(resource)
|
31
|
+
@condition_variable.signal
|
32
|
+
end
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def release_available_resource
|
39
|
+
@available_resources.pop
|
40
|
+
end
|
41
|
+
|
42
|
+
def build_new_resource
|
43
|
+
new_resource = @resource_class.new
|
44
|
+
raise "'new_resource' must be unique" if @resources.include?(new_resource)
|
45
|
+
@resources << new_resource
|
46
|
+
new_resource
|
47
|
+
end
|
48
|
+
|
49
|
+
def release_next_available_resource
|
50
|
+
@condition_variable.wait(@mutex)
|
51
|
+
release_available_resource
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'quack_pool'
|
2
|
+
|
3
|
+
RSpec.describe QuackPool do
|
4
|
+
resource_class = Class.new
|
5
|
+
|
6
|
+
context "when resource class is passed that does not respond to 'new'" do
|
7
|
+
it "should raise error" do
|
8
|
+
klass = Class.new
|
9
|
+
klass.private_class_method(:new)
|
10
|
+
stub_const 'BadResource', klass
|
11
|
+
expect { QuackPool.new(resource_class: BadResource) }.to raise_error ArgumentError
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when size is not specified" do
|
16
|
+
|
17
|
+
pool = QuackPool.new(resource_class: resource_class)
|
18
|
+
|
19
|
+
describe "#release_resource" do
|
20
|
+
|
21
|
+
it "should return instance of given resource class" do
|
22
|
+
resource = pool.release_resource
|
23
|
+
expect(resource).to be_a(resource_class)
|
24
|
+
resource = pool.release_resource
|
25
|
+
expect(resource).to be_a(resource_class)
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when resource it rereturned to pool" do
|
29
|
+
it "should return instance of given resource class" do
|
30
|
+
resource = pool.release_resource
|
31
|
+
pool.absorb_resource(resource)
|
32
|
+
resource = pool.release_resource
|
33
|
+
expect(resource).to be_a(resource_class)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#absorb_resource" do
|
40
|
+
|
41
|
+
it "should accept resource" do
|
42
|
+
resource = pool.release_resource
|
43
|
+
expect { pool.absorb_resource(resource) }.not_to raise_error
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when resource is absorbed twice" do
|
47
|
+
it "should raise error" do
|
48
|
+
resource = pool.release_resource
|
49
|
+
pool.absorb_resource(resource)
|
50
|
+
expect { pool.absorb_resource(resource) }.to raise_error ArgumentError
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when resource is returned to wrong pool" do
|
55
|
+
it "should raise error" do
|
56
|
+
resource = pool.release_resource
|
57
|
+
pool2 = QuackPool.new(resource_class: resource_class)
|
58
|
+
expect { pool2.absorb_resource(resource) }.to raise_error ArgumentError
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when size is specified" do
|
67
|
+
pool = QuackPool.new(resource_class: resource_class, size: 2)
|
68
|
+
|
69
|
+
describe "#release_resource" do
|
70
|
+
|
71
|
+
context "when no resources are available" do
|
72
|
+
it "will wait until resource has been returned to pool" do
|
73
|
+
thread = Thread.new do
|
74
|
+
resource1 = pool.release_resource
|
75
|
+
resource2 = pool.release_resource
|
76
|
+
sleep 2
|
77
|
+
pool.absorb_resource(resource1)
|
78
|
+
end
|
79
|
+
sleep 1
|
80
|
+
resource = pool.release_resource
|
81
|
+
thread.join
|
82
|
+
expect(resource).to be_a(resource_class)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
RSpec.configure do |config|
|
17
|
+
# rspec-expectations config goes here. You can use an alternate
|
18
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
19
|
+
# assertions if you prefer.
|
20
|
+
config.expect_with :rspec do |expectations|
|
21
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
22
|
+
# and `failure_message` of custom matchers include text for helper methods
|
23
|
+
# defined using `chain`, e.g.:
|
24
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
25
|
+
# # => "be bigger than 2 and smaller than 4"
|
26
|
+
# ...rather than:
|
27
|
+
# # => "be bigger than 2"
|
28
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
29
|
+
end
|
30
|
+
|
31
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
32
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
33
|
+
config.mock_with :rspec do |mocks|
|
34
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
35
|
+
# a real object. This is generally recommended, and will default to
|
36
|
+
# `true` in RSpec 4.
|
37
|
+
mocks.verify_partial_doubles = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
41
|
+
# have no way to turn it off -- the option exists only for backwards
|
42
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
43
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
44
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
45
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
46
|
+
|
47
|
+
# The settings below are suggested to provide a good initial experience
|
48
|
+
# with RSpec, but feel free to customize to your heart's content.
|
49
|
+
=begin
|
50
|
+
# This allows you to limit a spec run to individual examples or groups
|
51
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
52
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
53
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
54
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
55
|
+
config.filter_run_when_matching :focus
|
56
|
+
|
57
|
+
# Allows RSpec to persist some state between runs in order to support
|
58
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
59
|
+
# you configure your source control system to ignore this file.
|
60
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
61
|
+
|
62
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
63
|
+
# recommended. For more details, see:
|
64
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
65
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
66
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
67
|
+
config.disable_monkey_patching!
|
68
|
+
|
69
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
70
|
+
# be too noisy due to issues in dependencies.
|
71
|
+
config.warnings = true
|
72
|
+
|
73
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
74
|
+
# file, and it's useful to allow more verbose output when running an
|
75
|
+
# individual spec file.
|
76
|
+
if config.files_to_run.one?
|
77
|
+
# Use the documentation formatter for detailed output,
|
78
|
+
# unless a formatter has already been configured
|
79
|
+
# (e.g. via a command-line flag).
|
80
|
+
config.default_formatter = "doc"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Print the 10 slowest examples and example groups at the
|
84
|
+
# end of the spec run, to help surface which specs are running
|
85
|
+
# particularly slow.
|
86
|
+
config.profile_examples = 10
|
87
|
+
|
88
|
+
# Run specs in random order to surface order dependencies. If you find an
|
89
|
+
# order dependency and want to debug it, you can fix the order by providing
|
90
|
+
# the seed, which is printed after each run.
|
91
|
+
# --seed 1234
|
92
|
+
config.order = :random
|
93
|
+
|
94
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
95
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
96
|
+
# test failures related to randomization by passing the same `--seed` value
|
97
|
+
# as the one that triggered the failure.
|
98
|
+
Kernel.srand config.seed
|
99
|
+
=end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quack_pool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Fors
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple resource pool that accepts a resource class to build the pool's
|
14
|
+
resources from.
|
15
|
+
email: mail@robfors.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- Rakefile
|
23
|
+
- lib/quack_pool.rb
|
24
|
+
- spec/quack_pool_spec.rb
|
25
|
+
- spec/spec_helper.rb
|
26
|
+
homepage: https://github.com/robfors/quack_pool
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.4.8
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: A simple resource pool
|
50
|
+
test_files: []
|