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

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

Overview

A FIFO queue of connections to be used by the connection pool. This is based on mperham's connection pool, implemented with a queue instead of a stack.

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



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

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



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

def mutex
  @mutex
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.0.0



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

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



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

def queue
  @queue
end

#resourceConditionVariable (readonly)

Returns resource The resource.

Returns:

  • (ConditionVariable)

    resource The resource.

Since:

  • 2.0.0



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

def resource
  @resource
end

Instance Method Details

#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



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

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



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

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



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

def enqueue(connection)
  mutex.synchronize do
    queue.unshift(connection)
    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



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

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_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



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

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



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

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



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

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