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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mongo/util/pool.rb', line 31 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.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def address @address end |
#checked_out ⇒ Object
Returns the value of attribute checked_out.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def checked_out @checked_out end |
#client ⇒ Object
Returns the value of attribute client.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def client @client end |
#host ⇒ Object
Returns the value of attribute host.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def host @host end |
#node ⇒ Object
Returns the value of attribute node.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def node @node end |
#port ⇒ Object
Returns the value of attribute port.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def port @port end |
#size ⇒ Object
Returns the value of attribute size.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 def size @size end |
#timeout ⇒ Object
Returns the value of attribute timeout.
21 22 23 |
# File 'lib/mongo/util/pool.rb', line 21 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.
202 203 204 205 206 207 208 209 210 |
# File 'lib/mongo/util/pool.rb', line 202 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
257 258 259 260 261 262 263 264 |
# File 'lib/mongo/util/pool.rb', line 257 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.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mongo/util/pool.rb', line 161 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.
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/mongo/util/pool.rb', line 269 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.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/mongo/util/pool.rb', line 231 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.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongo/util/pool.rb', line 176 def checkout_new_socket begin socket = @client.socket_class.new(@host, @port, @client.op_timeout, @client.connect_timeout, @client.socket_opts) socket.pool = self rescue => ex socket.close if socket @node.close if @node raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}" 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mongo/util/pool.rb', line 71 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
94 95 96 |
# File 'lib/mongo/util/pool.rb', line 94 def closed? @closed end |
#healthy? ⇒ Boolean
89 90 91 92 |
# File 'lib/mongo/util/pool.rb', line 89 def healthy? close if @sockets.all?(&:closed?) !closed? && node.healthy? end |
#host_port ⇒ Object
112 113 114 |
# File 'lib/mongo/util/pool.rb', line 112 def host_port [@host, @port] end |
#host_string ⇒ Object
108 109 110 |
# File 'lib/mongo/util/pool.rb', line 108 def host_string "#{@host}:#{@port}" end |
#inspect ⇒ Object
102 103 104 105 106 |
# File 'lib/mongo/util/pool.rb', line 102 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.
214 215 216 217 218 219 220 221 222 |
# File 'lib/mongo/util/pool.rb', line 214 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
152 153 154 155 156 157 158 |
# File 'lib/mongo/util/pool.rb', line 152 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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/mongo/util/pool.rb', line 118 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
250 251 252 253 254 255 |
# File 'lib/mongo/util/pool.rb', line 250 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mongo/util/pool.rb', line 130 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
85 86 87 |
# File 'lib/mongo/util/pool.rb', line 85 def @node. end |
#up? ⇒ Boolean
98 99 100 |
# File 'lib/mongo/util/pool.rb', line 98 def up? !@closed end |