Class: Mongo::Pool
- Includes:
- ThreadLocalVariableManager
- Defined in:
- lib/mongo/util/pool.rb
Constant Summary collapse
- PING_ATTEMPTS =
6- MAX_PING_TIME =
1_000_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.
-
#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.
-
#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
- #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.
- #matches_mode(mode) ⇒ Object
- #matches_tag_set(tag_set) ⇒ Object
- #matches_tag_sets(tag_sets) ⇒ Object
- #ping ⇒ Object
-
#ping_time ⇒ Object
Refresh ping time only if we haven’t checked within the last five minutes.
-
#refresh_ping_time ⇒ Object
Return the time it takes on average to do a round-trip against this node.
- #tags ⇒ Object
- #up? ⇒ Boolean
Methods included from ThreadLocalVariableManager
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 |
# 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 @checkout_counter = 0 end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def address @address end |
#checked_out ⇒ Object
Returns the value of attribute checked_out.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def checked_out @checked_out end |
#client ⇒ Object
Returns the value of attribute client.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def client @client end |
#host ⇒ Object
Returns the value of attribute host.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def port @port end |
#size ⇒ Object
Returns the value of attribute size.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 def size @size end |
#timeout ⇒ Object
Returns the value of attribute timeout.
22 23 24 |
# File 'lib/mongo/util/pool.rb', line 22 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.
214 215 216 217 218 219 220 221 222 |
# File 'lib/mongo/util/pool.rb', line 214 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 |
#checkin(socket) ⇒ Object
Return a socket to the pool.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mongo/util/pool.rb', line 174 def checkin(socket) @connection_mutex.synchronize do if @checked_out.delete(socket) @queue.signal 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.
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 301 302 303 304 305 306 307 308 309 |
# File 'lib/mongo/util/pool.rb', line 264 def checkout @client.connect if !@client.connected? start_time = Time.now loop do @connection_mutex.synchronize do if socket_for_thread = thread_local[:sockets][self.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_local[:sockets].delete self.object_id socket = checkout_new_socket end return socket else # Otherwise, wait @queue.wait(@connection_mutex) end end 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 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.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/mongo/util/pool.rb', line 243 def checkout_existing_socket(socket=nil) if !socket socket = (@sockets - @checked_out).first 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_local[:sockets][self.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.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/mongo/util/pool.rb', line 189 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_local[:sockets][self.object_id] = socket socket end |
#close(opts = {}) ⇒ Object
Close this pool.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mongo/util/pool.rb', line 70 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
88 89 90 |
# File 'lib/mongo/util/pool.rb', line 88 def closed? @closed end |
#host_port ⇒ Object
125 126 127 |
# File 'lib/mongo/util/pool.rb', line 125 def host_port [@host, @port] end |
#host_string ⇒ Object
121 122 123 |
# File 'lib/mongo/util/pool.rb', line 121 def host_string "#{@host}:#{@port}" end |
#inspect ⇒ Object
115 116 117 118 119 |
# File 'lib/mongo/util/pool.rb', line 115 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.
226 227 228 229 230 231 232 233 234 |
# File 'lib/mongo/util/pool.rb', line 226 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 |
#matches_mode(mode) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/mongo/util/pool.rb', line 96 def matches_mode(mode) if mode == :primary && @node.secondary? || mode == :secondary && @node.primary? false else true end end |
#matches_tag_set(tag_set) ⇒ Object
105 106 107 108 109 |
# File 'lib/mongo/util/pool.rb', line 105 def matches_tag_set(tag_set) tag_set.all? do |tag, value| .has_key?(tag) && [tag] == value end end |
#matches_tag_sets(tag_sets) ⇒ Object
111 112 113 |
# File 'lib/mongo/util/pool.rb', line 111 def matches_tag_sets(tag_sets) tag_sets.all? {|set| matches_tag_set(set)} end |
#ping ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/mongo/util/pool.rb', line 165 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.
131 132 133 134 135 136 137 138 139 |
# File 'lib/mongo/util/pool.rb', line 131 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 |
#refresh_ping_time ⇒ Object
Return the time it takes on average to do a round-trip against this node.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongo/util/pool.rb', line 143 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
84 85 86 |
# File 'lib/mongo/util/pool.rb', line 84 def @node. end |
#up? ⇒ Boolean
92 93 94 |
# File 'lib/mongo/util/pool.rb', line 92 def up? !@closed end |