Permalink
Browse files

Refactor/split methods in the threaded connection pools

This splits off a couple methods in the threaded connection pools,
next_available returns the next available connection or nil, and
checkin_connection returns a connection to the pool.
  • Loading branch information...
1 parent 0a52eed commit 9a4201b3ff119d1b2453ae439c5fba8ab5181d69 @jeremyevans committed Oct 24, 2012
Showing with 47 additions and 12 deletions.
  1. +24 −6 lib/sequel/connection_pool/sharded_threaded.rb
  2. +23 −6 lib/sequel/connection_pool/threaded.rb
@@ -174,7 +174,21 @@ def acquire(thread, server)
# available, tries to create a new connection. The calling code should already
# have the mutex before calling this.
def available(server)
- available_connections(server).pop || make_new(server)
+ next_available(server) || make_new(server)
+ end
+
+ # Return a connection to the pool of available connections for the server,
+ # returns the connection. The calling code should already have the mutex
+ # before calling this.
+ def checkin_connection(server, conn)
+ case @connection_handling
+ when :queue
+ available_connections(server).unshift(conn)
+ else
+ available_connections(server) << conn
+ end
+
+ conn
end
# Disconnect from the given server. Disconnects available connections
@@ -199,6 +213,13 @@ def make_new(server)
end
default_make_new(server) if (n || size(server)) < @max_size
end
+
+ # Return the next available connection in the pool for the given server, or nil
+ # if there is not currently an available connection for the server.
+ # The calling code should already have the mutex before calling this.
+ def next_available(server)
+ available_connections(server).pop
+ end
# Returns the connection owned by the supplied thread for the given server,
# if any. The calling code should NOT already have the mutex before calling this.
@@ -221,13 +242,10 @@ def release(thread, conn, server)
else
conn = allocated(server).delete(thread)
- case @connection_handling
- when :queue
- available_connections(server).unshift(conn)
- when :disconnect
+ if @connection_handling == :disconnect
db.disconnect_connection(conn)
else
- available_connections(server) << conn
+ checkin_connection(server, conn)
end
end
end
@@ -130,7 +130,20 @@ def acquire(thread)
# available, tries to create a new connection. The calling code should already
# have the mutex before calling this.
def available
- @available_connections.pop || make_new(DEFAULT_SERVER)
+ next_available || make_new(DEFAULT_SERVER)
+ end
+
+ # Return a connection to the pool of available connections, returns the connection.
+ # The calling code should already have the mutex before calling this.
+ def checkin_connection(conn)
+ case @connection_handling
+ when :queue
+ @available_connections.unshift(conn)
+ else
+ @available_connections << conn
+ end
+
+ conn
end
# Alias the default make_new method, so subclasses can call it directly.
@@ -147,6 +160,13 @@ def make_new(server)
super if (n || size) < @max_size
end
+ # Return the next available connection in the pool, or nil if there
+ # is not currently an available connection. The calling code should already
+ # have the mutex before calling this.
+ def next_available
+ @available_connections.pop
+ end
+
# Returns the connection owned by the supplied thread,
# if any. The calling code should NOT already have the mutex before calling this.
def owned_connection(thread)
@@ -158,13 +178,10 @@ def owned_connection(thread)
def release(thread)
conn = @allocated.delete(thread)
- case @connection_handling
- when :queue
- @available_connections.unshift(conn)
- when :disconnect
+ if @connection_handling == :disconnect
db.disconnect_connection(conn)
else
- @available_connections << conn
+ checkin_connection(conn)
end
end

0 comments on commit 9a4201b

Please sign in to comment.