Class: Mongo::ReplSetConnection
- Inherits:
-
Connection
- Object
- Connection
- Mongo::ReplSetConnection
- Defined in:
- lib/mongo/repl_set_connection.rb
Overview
Instantiates and manages connections to a MongoDB replica set.
Constant Summary
Constants inherited from Connection
Connection::ConditionVariable, Connection::DEFAULT_PORT, Connection::Mutex, Connection::RESPONSE_HEADER_SIZE, Connection::STANDARD_HEADER_SIZE, Connection::TCPSocket
Instance Attribute Summary collapse
-
#arbiters ⇒ Object
readonly
Returns the value of attribute arbiters.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#read_pool ⇒ Object
readonly
Returns the value of attribute read_pool.
-
#secondaries ⇒ Object
readonly
Returns the value of attribute secondaries.
-
#secondary_pools ⇒ Object
readonly
Returns the value of attribute secondary_pools.
Attributes inherited from Connection
#auths, #host_to_try, #logger, #pool_size, #primary, #primary_pool, #safe, #size
Instance Method Summary collapse
- #authenticate_pools ⇒ Object
-
#close ⇒ Object
Close the connection to the database.
-
#connect ⇒ Object
(also: #reconnect)
Create a new socket and attempt to connect to master.
- #connecting? ⇒ Boolean
-
#host ⇒ String
The replica set primary’s host name.
-
#initialize(*args) ⇒ ReplSetConnection
constructor
Create a connection to a MongoDB replica set.
- #logout_pools(db) ⇒ Object
-
#port ⇒ Integer
The replica set primary’s port.
-
#read_primary? ⇒ Boolean
(also: #primary?)
Determine whether we’re reading from a primary node.
- #reset_connection ⇒ Object deprecated Deprecated.
-
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?.
Methods inherited from Connection
#[], #active?, #add_auth, #apply_saved_authentication, #clear_auths, #connected?, #copy_database, #database_info, #database_names, #db, #drop_database, from_uri, #instrument, #lock!, #locked?, #max_bson_size, multi, #ping, #receive_message, #remove_auth, #send_message, #send_message_with_safe_check, #server_info, #server_version, #unlock!
Constructor Details
#initialize(*args) ⇒ ReplSetConnection
Create a connection to a MongoDB replica set.
Once connected to a replica set, you can find out which nodes are primary, secondary, and arbiters with the corresponding accessors: Connection#primary, Connection#secondaries, and Connection#arbiters. This is useful if your application needs to connect manually to nodes other than the primary.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mongo/repl_set_connection.rb', line 69 def initialize(*args) if args.last.is_a?(Hash) opts = args.pop else opts = {} end unless args.length > 0 raise MongoArgumentError, "A ReplSetConnection requires at least one node." end # Get seed nodes @nodes = args # Replica set name @replica_set = opts[:rs_name] # Cache the various node types when connecting to a replica set. @secondaries = [] @arbiters = [] # Connection pools for each secondary node @secondary_pools = [] @read_pool = nil # Are we allowing reads from secondaries? @read_secondary = opts.fetch(:read_secondary, false) @slave_okay = false setup(opts) end |
Instance Attribute Details
#arbiters ⇒ Object (readonly)
Returns the value of attribute arbiters.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def arbiters @arbiters end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def nodes @nodes end |
#read_pool ⇒ Object (readonly)
Returns the value of attribute read_pool.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def read_pool @read_pool end |
#secondaries ⇒ Object (readonly)
Returns the value of attribute secondaries.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def secondaries @secondaries end |
#secondary_pools ⇒ Object (readonly)
Returns the value of attribute secondary_pools.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def secondary_pools @secondary_pools end |
Instance Method Details
#authenticate_pools ⇒ Object
196 197 198 199 200 201 |
# File 'lib/mongo/repl_set_connection.rb', line 196 def authenticate_pools super @secondary_pools.each do |pool| pool.authenticate_existing end end |
#close ⇒ Object
Close the connection to the database.
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mongo/repl_set_connection.rb', line 167 def close super @read_pool = nil @secondary_pools.each do |pool| pool.close end @secondaries = [] @secondary_pools = [] @arbiters = [] @nodes_tried = [] @nodes_to_try = [] end |
#connect ⇒ Object Also known as: reconnect
Create a new socket and attempt to connect to master. If successful, sets host and port to master and returns the socket.
If connecting to a replica set, this method will replace the initially-provided seed list with any nodes known to the set.
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 |
# File 'lib/mongo/repl_set_connection.rb', line 108 def connect close @nodes_to_try = @nodes.clone while connecting? node = @nodes_to_try.shift config = check_is_master(node) if is_primary?(config) set_primary(node) else set_auxillary(node, config) end end pick_secondary_for_read if @read_secondary if connected? BSON::BSON_CODER.update_max_bson_size(self) else if @secondary_pools.empty? close # close any existing pools and sockets raise ConnectionFailure, "Failed to connect any given host:port" else close # close any existing pools and sockets raise ConnectionFailure, "Failed to connect to primary node." end end end |
#connecting? ⇒ Boolean
139 140 141 |
# File 'lib/mongo/repl_set_connection.rb', line 139 def connecting? @nodes_to_try.length > 0 end |
#host ⇒ String
The replica set primary’s host name.
146 147 148 |
# File 'lib/mongo/repl_set_connection.rb', line 146 def host super end |
#logout_pools(db) ⇒ Object
203 204 205 206 207 208 |
# File 'lib/mongo/repl_set_connection.rb', line 203 def logout_pools(db) super @secondary_pools.each do |pool| pool.logout_existing(db) end end |
#port ⇒ Integer
The replica set primary’s port.
153 154 155 |
# File 'lib/mongo/repl_set_connection.rb', line 153 def port super end |
#read_primary? ⇒ Boolean Also known as: primary?
Determine whether we’re reading from a primary node. If false, this connection connects to a secondary node and @read_secondaries is true.
161 162 163 |
# File 'lib/mongo/repl_set_connection.rb', line 161 def read_primary? !@read_pool end |
#reset_connection ⇒ Object
If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values.
183 184 185 186 187 |
# File 'lib/mongo/repl_set_connection.rb', line 183 def reset_connection close warn "ReplSetConnection#reset_connection is now deprecated. " + "Use ReplSetConnection#close instead." end |
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?
192 193 194 |
# File 'lib/mongo/repl_set_connection.rb', line 192 def slave_ok? @read_secondary || @slave_ok end |