Method: Mongo::Server::ConnectionPool#initialize
- Defined in:
- lib/mongo/server/connection_pool.rb
#initialize(server, options = {}) ⇒ ConnectionPool
Create the new connection pool.
Note: Additionally, options for connections created by this pool should
be included in the options passed here, and they will be forwarded to
any connections created by the pool.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mongo/server/connection_pool.rb', line 102 def initialize(server, = {}) unless server.is_a?(Server) raise ArgumentError, 'First argument must be a Server instance' end = .dup if [:min_size] && [:min_pool_size] && [:min_size] != [:min_pool_size] raise ArgumentError, "Min size #{[:min_size]} is not identical to min pool size #{[:min_pool_size]}" end if [:max_size] && [:max_pool_size] && [:max_size] != [:max_pool_size] raise ArgumentError, "Max size #{[:max_size]} is not identical to max pool size #{[:max_pool_size]}" end if [:wait_timeout] && [:wait_queue_timeout] && [:wait_timeout] != [:wait_queue_timeout] raise ArgumentError, "Wait timeout #{[:wait_timeout]} is not identical to wait queue timeout #{[:wait_queue_timeout]}" end [:min_size] ||= [:min_pool_size] .delete(:min_pool_size) [:max_size] ||= [:max_pool_size] .delete(:max_pool_size) if [:min_size] && [:max_size] && ([:max_size] != 0 && [:min_size] > [:max_size]) then raise ArgumentError, "Cannot have min size #{[:min_size]} exceed max size #{[:max_size]}" end if [:wait_queue_timeout] [:wait_timeout] ||= [:wait_queue_timeout] end .delete(:wait_queue_timeout) @server = server @options = .freeze @generation_manager = GenerationManager.new(server: server) @ready = false @closed = false # A connection owned by this pool should be either in the # available connections array (which is used as a stack) # or in the checked out connections set. @available_connections = available_connections = [] @checked_out_connections = Set.new @pending_connections = Set.new @interrupt_connections = [] # Mutex used for synchronizing access to @available_connections and # @checked_out_connections. The pool object is thread-safe, thus # all methods that retrieve or modify instance variables generally # must do so under this lock. @lock = Mutex.new # Background thread reponsible for maintaining the size of # the pool to at least min_size @populator = Populator.new(self, ) @populate_semaphore = Semaphore.new # Condition variable to enforce the first check in check_out: max_pool_size. # This condition variable should be signaled when the number of # unavailable connections decreases (pending + pending_connections + # checked_out_connections). @size_cv = Mongo::ConditionVariable.new(@lock) # This represents the number of threads that have made it past the size_cv # gate but have not acquired a connection to add to the pending_connections # set. @connection_requests = 0 # Condition variable to enforce the second check in check_out: max_connecting. # Thei condition variable should be signaled when the number of pending # connections decreases. @max_connecting_cv = Mongo::ConditionVariable.new(@lock) @max_connecting = .fetch(:max_connecting, DEFAULT_MAX_CONNECTING) ObjectSpace.define_finalizer(self, self.class.finalize(@available_connections, @pending_connections, @populator)) publish_cmap_event( Monitoring::Event::Cmap::PoolCreated.new(@server.address, , self) ) end |