Skip to content

Commit

Permalink
Move connection pool finalizer to connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Feb 21, 2019
1 parent 5728e9a commit c15c39f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
7 changes: 2 additions & 5 deletions lib/mongo/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,8 @@ def summary
def pool
@pool_lock.synchronize do
@pool ||= begin
ConnectionPool.get(self).tap do |pool|
finalizer = proc do
pool.disconnect
end
ObjectSpace.define_finalizer(self, finalizer)
ConnectionPool.new(options) do |generation|
Connection.new(self, options.merge(generation: generation))
end
end
end
Expand Down
30 changes: 7 additions & 23 deletions lib/mongo/server/connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ class ConnectionPool
#
# @since 2.0.0
def initialize(options = {}, &block)
@options = options.freeze
@queue = Queue.new(options, &block)
@options = options.dup.freeze
@queue = queue = Queue.new(@options, &block)

finalizer = proc do
queue.disconnect
end
ObjectSpace.define_finalizer(self, finalizer)
end

# @return [ Hash ] options The pool options.
Expand Down Expand Up @@ -120,27 +125,6 @@ def with_connection
protected

attr_reader :queue

private

class << self

# Creates a new connection pool for the provided server.
#
# @example Create a new connection pool.
# Mongo::Server::ConnectionPool.get(server)
#
# @param [ Mongo::Server ] server The server.
#
# @return [ Mongo::Server::ConnectionPool ] The connection pool.
#
# @since 2.0.0
def get(server)
ConnectionPool.new(server.options) do |generation|
Connection.new(server, server.options.merge(generation: generation))
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/mongo/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
end

let(:pool) do
Mongo::Server::ConnectionPool.get(server)
server.pool
end

describe '#==' do
Expand Down

0 comments on commit c15c39f

Please sign in to comment.