Class: Mongo::Server::ConnectionPool::Queue

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/server/connection_pool/queue.rb

Overview

A LIFO queue of connections to be used by the connection pool. This is based on mperham's connection pool.

Since:

  • 2.0.0

Constant Summary

MAX_SIZE =

The default max size for the queue.

Since:

  • 2.0.0

5.freeze
MIN_SIZE =

The default min size for the queue.

Since:

  • 2.0.0

1.freeze
WAIT_TIMEOUT =

The default timeout, in seconds, to wait for a connection.

Since:

  • 2.0.0

1.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Queue

Initialize the new queue. Will yield the block the number of times for the initial size of the queue.

Examples:

Create the queue.

Mongo::Pool::Queue.new(max_pool_size: 5) { Connection.new }

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :max_pool_size (Integer)

    The maximum size.

  • :min_pool_size (Integer)

    The minimum size.

  • :wait_queue_timeout (Float)

    The time to wait, in seconds, for a free connection.

Since:

  • 2.0.0



109
110
111
112
113
114
115
116
# File 'lib/mongo/server/connection_pool/queue.rb', line 109

def initialize(options = {}, &block)
  @block = block
  @connections = 0
  @options = options
  @queue = Array.new(min_size) { create_connection }
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Instance Attribute Details

#mutexMutex (readonly)

Returns mutex The mutex used for synchronization.

Returns:

  • (Mutex)

    mutex The mutex used for synchronization.

Since:

  • 2.0.0



42
43
44
# File 'lib/mongo/server/connection_pool/queue.rb', line 42

def mutex
  @mutex
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.0.0



45
46
47
# File 'lib/mongo/server/connection_pool/queue.rb', line 45

def options
  @options
end

#queueArray (readonly)

Returns queue The underlying array of connections.

Returns:

  • (Array)

    queue The underlying array of connections.

Since:

  • 2.0.0



39
40
41
# File 'lib/mongo/server/connection_pool/queue.rb', line 39

def queue
  @queue
end

#resourceConditionVariable (readonly)

Returns resource The resource.

Returns:

  • (ConditionVariable)

    resource The resource.

Since:

  • 2.0.0



48
49
50
# File 'lib/mongo/server/connection_pool/queue.rb', line 48

def resource
  @resource
end

Instance Method Details

#close_stale_sockets!Object

Close sockets that have been open for longer than the max idle time, if the

option is set.

Examples:

Close the stale sockets

queue.close_stale_sockets!

Since:

  • 2.5.0



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mongo/server/connection_pool/queue.rb', line 186

def close_stale_sockets!
  return unless max_idle_time

  to_refresh = []
  queue.each do |connection|
    if last_checkin = connection.last_checkin
      if (Time.now - last_checkin) > max_idle_time
        to_refresh << connection
      end
    end
  end

  mutex.synchronize do
    num_checked_out = @connections - queue.size
    min_size_delta = [(min_size - num_checked_out), 0].max

    to_refresh.each do |connection|
      if queue.include?(connection)
        connection.disconnect!
        if queue.index(connection) < min_size_delta
          begin; connection.connect!; rescue; end
        end
      end
    end
  end
end

#dequeueMongo::Pool::Connection

Dequeue a connection from the queue, waiting for the provided timeout for an item if none is in the queue.

Examples:

Dequeue a connection.

queue.dequeue

Returns:

  • (Mongo::Pool::Connection)

    The next connection.

Since:

  • 2.0.0



59
60
61
62
63
# File 'lib/mongo/server/connection_pool/queue.rb', line 59

def dequeue
  mutex.synchronize do
    dequeue_connection
  end
end

#disconnect!true

Disconnect all connections in the queue.

Examples:

Disconnect all connections.

queue.disconnect!

Returns:

  • (true)

    Always true.

Since:

  • 2.1.0



73
74
75
76
77
78
# File 'lib/mongo/server/connection_pool/queue.rb', line 73

def disconnect!
  mutex.synchronize do
    queue.each{ |connection| connection.disconnect! }
    true
  end
end

#enqueue(connection) ⇒ Object

Enqueue a connection in the queue.

Examples:

Enqueue a connection.

queue.enqueue(connection)

Parameters:

  • connection (Mongo::Pool::Connection)

    The connection.

Since:

  • 2.0.0



88
89
90
91
92
93
# File 'lib/mongo/server/connection_pool/queue.rb', line 88

def enqueue(connection)
  mutex.synchronize do
    queue.unshift(connection.record_checkin!)
    resource.broadcast
  end
end

#inspectString

Get a pretty printed string inspection for the queue.

Examples:

Inspect the queue.

queue.inspect

Returns:

  • (String)

    The queue inspection.

Since:

  • 2.0.0



126
127
128
129
# File 'lib/mongo/server/connection_pool/queue.rb', line 126

def inspect
  "#<Mongo::Pool::Queue:0x#{object_id} min_size=#{min_size} max_size=#{max_size} " +
    "wait_timeout=#{wait_timeout} current_size=#{queue.size}>"
end

#max_idle_timeFloat

The maximum seconds a socket can remain idle since it has been checked in to the pool.

Examples:

Get the max idle time.

queue.max_idle_time

Returns:

  • (Float)

    The max socket idle time in seconds.

Since:

  • 2.5.0



175
176
177
# File 'lib/mongo/server/connection_pool/queue.rb', line 175

def max_idle_time
  @max_idle_time ||= options[:max_idle_time]
end

#max_sizeInteger

Get the maximum size of the queue.

Examples:

Get the max size.

queue.max_size

Returns:

  • (Integer)

    The maximum size of the queue.

Since:

  • 2.0.0



139
140
141
# File 'lib/mongo/server/connection_pool/queue.rb', line 139

def max_size
  @max_size ||= options[:max_pool_size] || MAX_SIZE
end

#min_sizeInteger

Get the minimum size of the queue.

Examples:

Get the min size.

queue.min_size

Returns:

  • (Integer)

    The minimum size of the queue.

Since:

  • 2.0.0



151
152
153
# File 'lib/mongo/server/connection_pool/queue.rb', line 151

def min_size
  @min_size ||= options[:min_pool_size] || MIN_SIZE
end

#wait_timeoutFloat

The time to wait, in seconds, for a connection to become available.

Examples:

Get the wait timeout.

queue.wait_timeout

Returns:

  • (Float)

    The queue wait timeout.

Since:

  • 2.0.0



163
164
165
# File 'lib/mongo/server/connection_pool/queue.rb', line 163

def wait_timeout
  @wait_timeout ||= options[:wait_queue_timeout] || WAIT_TIMEOUT
end