Skip to content

Commit

Permalink
3.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed May 22, 2023
1 parent f83b630 commit 89c829c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .standard.yml
@@ -1,3 +1,3 @@
ruby_version: 2.5.0
ruby_version: 3.0.0
fix: true
parallel: true
2 changes: 1 addition & 1 deletion connection_pool.gemspec
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler"
s.add_development_dependency "minitest", ">= 5.0.0"
s.add_development_dependency "rake"
s.required_ruby_version = ">= 2.5.0"
s.required_ruby_version = ">= 3.0.0"

s.metadata = {"changelog_uri" => "https://github.com/mperham/connection_pool/blob/main/Changes.md", "rubygems_mfa_required" => "true"}
end
4 changes: 2 additions & 2 deletions lib/connection_pool.rb
Expand Up @@ -96,7 +96,7 @@ def initialize(options = {}, &block)
@timeout = options.fetch(:timeout)
@auto_reload_after_fork = options.fetch(:auto_reload_after_fork)

@available = TimedStack.new(@size, &block)
@available = TimedStack.new(@size, @timeout, &block)
@key = :"pool-#{@available.object_id}"
@key_count = :"pool-#{@available.object_id}-count"
INSTANCES[self] = self if INSTANCES
Expand All @@ -122,7 +122,7 @@ def checkout(options = {})
::Thread.current[@key]
else
::Thread.current[@key_count] = 1
::Thread.current[@key] = @available.pop(options[:timeout] || @timeout)
::Thread.current[@key] = @available.pop(options)
end
end

Expand Down
18 changes: 8 additions & 10 deletions lib/connection_pool/timed_stack.rb
Expand Up @@ -14,8 +14,8 @@
# ts.push conn
#
# conn = ts.pop
# ts.pop timeout: 5
# #=> raises ConnectionPool::TimeoutError after 5 seconds
# ts.pop(timeout: 2)
# #=> raises ConnectionPool::TimeoutError after 2 seconds

class ConnectionPool::TimedStack
attr_reader :max
Expand All @@ -24,14 +24,15 @@ class ConnectionPool::TimedStack
# Creates a new pool with +size+ connections that are created from the given
# +block+.

def initialize(size = 0, &block)
def initialize(size = 0, timeout = 5, &block)
@create_block = block
@created = 0
@que = []
@max = size
@mutex = Thread::Mutex.new
@resource = Thread::ConditionVariable.new
@shutdown_block = nil
@timeout = timeout
end

##
Expand All @@ -56,14 +57,11 @@ def push(obj, options = {})
# immediately returned. If no connection is available within the given
# timeout a ConnectionPool::TimeoutError is raised.
#
# +:timeout+ is the only checked entry in +options+ and is preferred over
# the +timeout+ argument (which will be removed in a future release). Other
# options may be used by subclasses that extend TimedStack.

def pop(timeout = 0.5, options = {})
options, timeout = timeout, 0.5 if Hash === timeout
timeout = options.fetch :timeout, timeout
# +:timeout+ is the only checked entry in +options+.
# Other options may be used by subclasses that extend TimedStack.

def pop(options = nil)
timeout = options&.fetch(:timeout, @timeout) || @timeout
deadline = current_time + timeout
@mutex.synchronize do
loop do
Expand Down
2 changes: 1 addition & 1 deletion lib/connection_pool/version.rb
@@ -1,3 +1,3 @@
class ConnectionPool
VERSION = "2.4.1"
VERSION = "3.0.0"
end
21 changes: 3 additions & 18 deletions lib/connection_pool/wrapper.rb
Expand Up @@ -31,26 +31,11 @@ def respond_to?(id, *args)
end

# rubocop:disable Style/MissingRespondToMissing
if ::RUBY_VERSION >= "3.0.0"
def method_missing(name, *args, **kwargs, &block)
with do |connection|
connection.send(name, *args, **kwargs, &block)
end
end
elsif ::RUBY_VERSION >= "2.7.0"
ruby2_keywords def method_missing(name, *args, &block)
with do |connection|
connection.send(name, *args, &block)
end
end
else
def method_missing(name, *args, &block)
with do |connection|
connection.send(name, *args, &block)
end
def method_missing(name, *args, **kwargs, &block)
with do |connection|
connection.send(name, *args, **kwargs, &block)
end
end
# rubocop:enable Style/MethodMissingSuper
# rubocop:enable Style/MissingRespondToMissing
end
end
5 changes: 0 additions & 5 deletions test/test_connection_pool_timed_stack.rb
Expand Up @@ -66,11 +66,6 @@ def test_pop_empty
assert_equal "Waited 0 sec, 0/0 available", e.message
end

def test_pop_empty_2_0_compatibility
e = assert_raises(Timeout::Error) { @stack.pop 0 }
assert_equal "Waited 0 sec, 0/0 available", e.message
end

def test_pop_full
stack = ConnectionPool::TimedStack.new(1) { Object.new }

Expand Down

0 comments on commit 89c829c

Please sign in to comment.