Class: Mongo::Pool
Constant Summary collapse
- PING_ATTEMPTS =
6- MAX_PING_TIME =
1_000_000- PRUNE_INTERVAL =
10_000
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#checked_out ⇒ Object
Returns the value of attribute checked_out.
-
#client ⇒ Object
Returns the value of attribute client.
-
#host ⇒ Object
Returns the value of attribute host.
-
#node ⇒ Object
Returns the value of attribute node.
-
#port ⇒ Object
Returns the value of attribute port.
-
#size ⇒ Object
Returns the value of attribute size.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#authenticate_existing ⇒ Object
If a user calls DB#authenticate, and several sockets exist, then we need a way to apply the authentication on each socket.
- #check_prune ⇒ Object
-
#checkin(socket) ⇒ Object
Return a socket to the pool.
-
#checkout ⇒ Object
Check out an existing socket or create a new socket if the maximum pool size has not been exceeded.
-
#checkout_existing_socket(socket = nil) ⇒ Object
Checks out the first available socket from the pool.
-
#checkout_new_socket ⇒ Object
Adds a new socket to the pool and checks it out.
-
#close(opts = {}) ⇒ Object
Close this pool.
- #closed? ⇒ Boolean
- #healthy? ⇒ Boolean
- #host_port ⇒ Object
- #host_string ⇒ Object
-
#initialize(client, host, port, opts = {}) ⇒ Pool
constructor
Create a new pool of connections.
- #inspect ⇒ Object
-
#logout_existing(db) ⇒ Object
Store the logout op for each existing socket to be applied before the next use of each socket.
- #ping ⇒ Object
-
#ping_time ⇒ Object
Refresh ping time only if we haven’t checked within the last five minutes.
- #prune_threads ⇒ Object
-
#refresh_ping_time ⇒ Object
Return the time it takes on average to do a round-trip against this node.
- #tags ⇒ Object
- #up? ⇒ Boolean
Constructor Details
#initialize(client, host, port, opts = {}) ⇒ Pool
Create a new pool of connections.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongo/util/pool.rb', line 17 def initialize(client, host, port, opts={}) @client = client @host, @port = host, port # A Mongo::Node object. @node = opts[:node] # The string address @address = "#{@host}:#{@port}" # Pool size and timeout. @size = opts.fetch(:size, 20) @timeout = opts.fetch(:timeout, 30) # Mutex for synchronizing pool access @connection_mutex = Mutex.new # Mutex for synchronizing pings @ping_mutex = Mutex.new # Condition variable for signal and wait @queue = ConditionVariable.new # Operations to perform on a socket @socket_ops = Hash.new { |h, k| h[k] = [] } @sockets = [] @checked_out = [] @ping_time = nil @last_ping = nil @closed = false @thread_ids_to_sockets = {} @checkout_counter = 0 end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def address @address end |
#checked_out ⇒ Object
Returns the value of attribute checked_out.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def checked_out @checked_out end |
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def client @client end |
#host ⇒ Object
Returns the value of attribute host.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def host @host end |
#node ⇒ Object
Returns the value of attribute node.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def node @node end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def port @port end |
#size ⇒ Object
Returns the value of attribute size.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def size @size end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/mongo/util/pool.rb', line 7 def timeout @timeout end |
Instance Method Details
#authenticate_existing ⇒ Object
If a user calls DB#authenticate, and several sockets exist, then we need a way to apply the authentication on each socket. So we store the apply_authentication method, and this will be applied right before the next use of each socket.
186 187 188 189 190 191 192 193 194 |
# File 'lib/mongo/util/pool.rb', line 186 def authenticate_existing @connection_mutex.synchronize do @sockets.each do |socket| @socket_ops[socket] << Proc.new do @client.apply_saved_authentication(:socket => socket) end end end end |
#check_prune ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/mongo/util/pool.rb', line 241 def check_prune if @checkout_counter > PRUNE_INTERVAL @checkout_counter = 0 prune_threads else @checkout_counter += 1 end end |
#checkin(socket) ⇒ Object
Return a socket to the pool.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/mongo/util/pool.rb', line 147 def checkin(socket) @connection_mutex.synchronize do if @checked_out.delete(socket) @queue.broadcast else return false end end true end |
#checkout ⇒ Object
Check out an existing socket or create a new socket if the maximum pool size has not been exceeded. Otherwise, wait for the next available socket.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/mongo/util/pool.rb', line 253 def checkout @client.connect if !@client.connected? start_time = Time.now loop do if (Time.now - start_time) > @timeout raise ConnectionTimeoutError, "could not obtain connection within " + "#{@timeout} seconds. The max pool size is currently #{@size}; " + "consider increasing the pool size or timeout." end @connection_mutex.synchronize do check_prune socket = nil if socket_for_thread = @thread_ids_to_sockets[Thread.current.object_id] if !@checked_out.include?(socket_for_thread) socket = checkout_existing_socket(socket_for_thread) end else if @sockets.size < @size socket = checkout_new_socket elsif @checked_out.size < @sockets.size socket = checkout_existing_socket end end if socket # This calls all procs, in order, scoped to existing sockets. # At the moment, we use this to lazily authenticate and # logout existing socket connections. @socket_ops[socket].reject! do |op| op.call end if socket.closed? @checked_out.delete(socket) @sockets.delete(socket) @thread_ids_to_sockets.delete(Thread.current.object_id) socket = checkout_new_socket end return socket else # Otherwise, wait @queue.wait(@connection_mutex) end end end end |
#checkout_existing_socket(socket = nil) ⇒ Object
Checks out the first available socket from the pool.
If the pid has changed, remove the socket and check out new one.
This method is called exclusively from #checkout; therefore, it runs within a mutex.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mongo/util/pool.rb', line 215 def checkout_existing_socket(socket=nil) if !socket available = @sockets - @checked_out socket = available[rand(available.length)] end if socket.pid != Process.pid @sockets.delete(socket) if socket socket.close unless socket.closed? end checkout_new_socket else @checked_out << socket @thread_ids_to_sockets[Thread.current.object_id] = socket socket end end |
#checkout_new_socket ⇒ Object
Adds a new socket to the pool and checks it out.
This method is called exclusively from #checkout; therefore, it runs within a mutex.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mongo/util/pool.rb', line 162 def checkout_new_socket begin socket = @client.socket_class.new(@host, @port, @client.op_timeout) socket.pool = self rescue => ex socket.close if socket raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}" @node.close if @node end # If any saved authentications exist, we want to apply those # when creating new sockets. @client.apply_saved_authentication(:socket => socket) @sockets << socket @checked_out << socket @thread_ids_to_sockets[Thread.current.object_id] = socket socket end |
#close(opts = {}) ⇒ Object
Close this pool.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongo/util/pool.rb', line 57 def close(opts={}) @connection_mutex.synchronize do if opts[:soft] && !@checked_out.empty? @closing = true close_sockets(@sockets - @checked_out) else close_sockets(@sockets) @closed = true end @node.close if @node end true end |
#closed? ⇒ Boolean
80 81 82 |
# File 'lib/mongo/util/pool.rb', line 80 def closed? @closed end |
#healthy? ⇒ Boolean
75 76 77 78 |
# File 'lib/mongo/util/pool.rb', line 75 def healthy? close if @sockets.all?(&:closed?) !closed? && node.healthy? end |
#host_port ⇒ Object
98 99 100 |
# File 'lib/mongo/util/pool.rb', line 98 def host_port [@host, @port] end |
#host_string ⇒ Object
94 95 96 |
# File 'lib/mongo/util/pool.rb', line 94 def host_string "#{@host}:#{@port}" end |
#inspect ⇒ Object
88 89 90 91 92 |
# File 'lib/mongo/util/pool.rb', line 88 def inspect "#<Mongo::Pool:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{port} " + "@ping_time=#{@ping_time} #{@checked_out.size}/#{@size} sockets available " + "up=#{!closed?}>" end |
#logout_existing(db) ⇒ Object
Store the logout op for each existing socket to be applied before the next use of each socket.
198 199 200 201 202 203 204 205 206 |
# File 'lib/mongo/util/pool.rb', line 198 def logout_existing(db) @connection_mutex.synchronize do @sockets.each do |socket| @socket_ops[socket] << Proc.new do @client.db(db).issue_logout(:socket => socket) end end end end |
#ping ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/mongo/util/pool.rb', line 138 def ping begin return self.client['admin'].command({:ping => 1}, :socket => @node.socket, :timeout => MAX_PING_TIME) rescue ConnectionFailure, OperationFailure, SocketError, SystemCallError, IOError return false end end |
#ping_time ⇒ Object
Refresh ping time only if we haven’t checked within the last five minutes.
104 105 106 107 108 109 110 111 112 |
# File 'lib/mongo/util/pool.rb', line 104 def ping_time @ping_mutex.synchronize do if !@last_ping || (Time.now - @last_ping) > 300 @ping_time = refresh_ping_time @last_ping = Time.now end end @ping_time end |
#prune_threads ⇒ Object
234 235 236 237 238 239 |
# File 'lib/mongo/util/pool.rb', line 234 def prune_threads live_threads = Thread.list.map(&:object_id) @thread_ids_to_sockets.reject! do |key, value| !live_threads.include?(key) end end |
#refresh_ping_time ⇒ Object
Return the time it takes on average to do a round-trip against this node.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mongo/util/pool.rb', line 116 def refresh_ping_time trials = [] PING_ATTEMPTS.times do t1 = Time.now if !self.ping return MAX_PING_TIME end trials << (Time.now - t1) * 1000 end trials.sort! # Delete shortest and longest times trials.delete_at(trials.length-1) trials.delete_at(0) total = 0.0 trials.each { |t| total += t } (total / trials.length).ceil end |
#tags ⇒ Object
71 72 73 |
# File 'lib/mongo/util/pool.rb', line 71 def @node. end |
#up? ⇒ Boolean
84 85 86 |
# File 'lib/mongo/util/pool.rb', line 84 def up? !@closed end |