Skip to content

Commit

Permalink
Access already allocated connections in a thread safe manner when che…
Browse files Browse the repository at this point in the history
…cking out connections in the sharded threaded connection pool

I'm not positive the previous code is thread unsafe, but it
appears to be.

While here, add some documentation to the threaded connection
pool and use local variable instead of instance variable.
  • Loading branch information
jeremyevans committed Nov 8, 2018
1 parent 0a37306 commit ef4940f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Access already allocated connections in a thread safe manner when checking out connections in the sharded threaded connection pool (jeremyevans)

* Automatically support datasets using qualified tables in the class_table_inheritance plugin without having to use the :alias option (benalavi) (#1565)

* Support rename_column without emulation on SQLite 3.25+ (jeremyevans)
Expand Down
3 changes: 2 additions & 1 deletion lib/sequel/connection_pool/sharded_threaded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ def acquire(thread, server)
# Assign a connection to the thread, or return nil if one cannot be assigned.
# The caller should NOT have the mutex before calling this.
def assign_connection(thread, server)
alloc = allocated(server)
alloc = nil

do_make_new = false
sync do
alloc = allocated(server)
if conn = next_available(server)
alloc[thread] = conn
return conn
Expand Down
4 changes: 3 additions & 1 deletion lib/sequel/connection_pool/threaded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def acquire(thread)
# Assign a connection to the thread, or return nil if one cannot be assigned.
# The caller should NOT have the mutex before calling this.
def assign_connection(thread)
# Thread safe as instance variable is only assigned to local variable
# and not operated on outside mutex.
allocated = @allocated
do_make_new = false
to_disconnect = nil
Expand All @@ -184,7 +186,7 @@ def assign_connection(thread)
if (n = _size) >= (max = @max_size)
allocated.keys.each do |t|
unless t.alive?
(to_disconnect ||= []) << @allocated.delete(t)
(to_disconnect ||= []) << allocated.delete(t)
end
end
n = nil
Expand Down

0 comments on commit ef4940f

Please sign in to comment.