From dd5dac56607a146aa29ec4ef557c42726541a2fa Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 2 Jun 2020 13:11:59 -0700 Subject: [PATCH] Rejigger exceptions, fixes #130 --- Changes.md | 6 +++--- lib/connection_pool.rb | 8 +++----- lib/connection_pool/errors.rb | 7 +++++++ lib/connection_pool/timed_stack.rb | 10 ++-------- 4 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 lib/connection_pool/errors.rb diff --git a/Changes.md b/Changes.md index bd4cf0b..b78d0e0 100644 --- a/Changes.md +++ b/Changes.md @@ -1,9 +1,9 @@ -connection\_pool changelog ---------------------------- +# connection_pool Changelog -HEAD +2.2.3 ------ +- Pool now throws `ConnectionPool::TimeoutError` on timeout. [#130] - Use monotonic clock present in all modern Rubies [Tero Tasanen, #109] - Remove code hacks necessary for JRuby 1.7 - Expose wrapped pool from ConnectionPool::Wrapper [Thomas Lecavelier, #113] diff --git a/lib/connection_pool.rb b/lib/connection_pool.rb index 037c076..af00393 100644 --- a/lib/connection_pool.rb +++ b/lib/connection_pool.rb @@ -1,5 +1,6 @@ -require_relative 'connection_pool/version' -require_relative 'connection_pool/timed_stack' +require 'connection_pool/version' +require 'connection_pool/errors' +require 'connection_pool/timed_stack' # Generic connection pool class for sharing a limited number of objects or network connections @@ -34,9 +35,6 @@ require_relative 'connection_pool/timed_stack' class ConnectionPool DEFAULTS = {size: 5, timeout: 5} - class Error < RuntimeError - end - def self.wrap(options, &block) Wrapper.new(options, &block) end diff --git a/lib/connection_pool/errors.rb b/lib/connection_pool/errors.rb new file mode 100644 index 0000000..bf3c77b --- /dev/null +++ b/lib/connection_pool/errors.rb @@ -0,0 +1,7 @@ +require 'timeout' + +class ConnectionPool + class Error < RuntimeError; end + class ConnectionPool::PoolShuttingDownError < ConnectionPool::Error; end + class ConnectionPool::TimeoutError < Timeout::Error; end +end diff --git a/lib/connection_pool/timed_stack.rb b/lib/connection_pool/timed_stack.rb index 9d3dc1b..fbf983f 100644 --- a/lib/connection_pool/timed_stack.rb +++ b/lib/connection_pool/timed_stack.rb @@ -1,10 +1,4 @@ -require 'timeout' - -## -# Raised when you attempt to retrieve a connection from a pool that has been -# shut down. - -class ConnectionPool::PoolShuttingDownError < RuntimeError; end +require 'connection_pool/errors' ## # The TimedStack manages a pool of homogeneous connections (or any resource @@ -82,7 +76,7 @@ class ConnectionPool::TimedStack return connection if connection to_wait = deadline - current_time - raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0 + raise ConnectionPool::TimeoutError, "Waited #{timeout} sec" if to_wait <= 0 @resource.wait(@mutex, to_wait) end end