Skip to content

Commit dba773b

Browse files
committed
Adding finalizer to cluster
1 parent 14f9b14 commit dba773b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/mongo/cluster.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
113113
subscribe_to(Event::PRIMARY_ELECTED, Event::PrimaryElected.new(self))
114114

115115
seeds.each{ |seed| add(seed) }
116+
ObjectSpace.define_finalizer(self, self.class.finalize(pools))
117+
end
118+
119+
# Finalize the cluster for garbage collection. Disconnects all the scoped
120+
# connection pools.
121+
#
122+
# @example Finalize the cluster.
123+
# Cluster.finalize(pools)
124+
#
125+
# @param [ Hash<Address, Server::ConnectionPool> ] pools The connection
126+
# pools.
127+
#
128+
# @return [ Proc ] The Finalizer.
129+
#
130+
# @since 2.2.0
131+
def self.finalize(pools)
132+
proc do
133+
pools.values.each do |pool|
134+
pool.disconnect!
135+
end
136+
end
116137
end
117138

118139
# Get the nicer formatted string for use in inspection.

0 commit comments

Comments
 (0)