Class | Mongo::MongoReplicaSetClient |
In: |
lib/mongo/mongo_replica_set_client.rb
|
Parent: | MongoClient |
Instantiates and manages connections to a MongoDB replica set.
REPL_SET_OPTS | = | [ :refresh_mode, :refresh_interval, :read_secondary, :rs_name, :name |
manager | [R] | |
refresh_interval | [R] | |
refresh_mode | [R] | |
refresh_version | [R] | |
replica_set_name | [R] | |
seeds | [R] |
Create a connection to a MongoDB replica set.
If no args are provided, it will check ENV["MONGODB_URI"].
Once connected to a replica set, you can find out which nodes are primary, secondary, and arbiters with the corresponding accessors: MongoClient#primary, MongoClient#secondaries, and MongoClient#arbiters. This is useful if your application needs to connect manually to nodes other than the primary.
@overload initialize(seeds=ENV["MONGODB_URI"], opts={})
@param [Array<String>, Array<Array(String, Integer)>] seeds @option opts [String, Integer, Symbol] :w (1) Set default number of nodes to which a write should be acknowledged @option opts [Boolean] :j (false) Set journal acknowledgement @option opts [Integer] :wtimeout (nil) Set acknowledgement timeout @option opts [Boolean] :fsync (false) Set fsync acknowledgement. Notes about write concern options: Write concern options are propagated to objects instantiated from this MongoReplicaSetClient. These defaults can be overridden upon instantiation of any object by explicitly setting an options hash on initialization. @option opts [:primary, :primary_preferred, :secondary, :secondary_preferred, :nearest] :read (:primary) A "read preference" determines the candidate replica set members to which a query or command can be sent. [:primary] * Read from primary only. * Cannot be combined with tags. [:primary_preferred] * Read from primary if available, otherwise read from a secondary. [:secondary] * Read from secondary if available. [:secondary_preferred] * Read from a secondary if available, otherwise read from the primary. [:nearest] * Read from any member. @option opts [Array<Hash{ String, Symbol => Tag Value }>] :tag_sets ([]) Read from replica-set members with these tags. @option opts [Integer] :secondary_acceptable_latency_ms (15) The acceptable nearest available member for a member to be considered "near". @option opts [Logger] :logger (nil) Logger instance to receive driver operation log. @option opts [Integer] :pool_size (1) The maximum number of socket connections allowed per connection pool. Note: this setting is relevant only for multi-threaded applications. @option opts [Float] :pool_timeout (5.0) When all of the connections a pool are checked out, this is the number of seconds to wait for a new connection to be released before throwing an exception. Note: this setting is relevant only for multi-threaded applications. @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out. @option opts [Float] :connect_timeout (30) The number of seconds to wait before timing out a connection attempt. @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL. @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the state of the connection every :refresh_interval seconds. Replica set connection failures will always trigger a complete refresh. This option is useful when you want to add new nodes or remove replica set nodes not currently in use by the driver. @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds between calls to check the replica set's state. @note the number of seed nodes does not have to be equal to the number of replica set members. The purpose of seed nodes is to permit the driver to find at least one replica set member even if a member is down.
@example Connect to a replica set and provide two seed nodes.
MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'])
@example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named ‘prod’:
MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'], :name => 'prod')
@example Connect to a replica set providing two seed nodes and allowing reads from a secondary node:
MongoReplicaSetClient.new(['localhost:30000', 'localhost:30001'], :read => :secondary)
@see api.mongodb.org/ruby/current/file.REPLICA_SETS.html Replica sets in Ruby
@raise [MongoArgumentError] This is raised for usage errors.
@raise [ConnectionFailure] This is raised for the various connection failures.
Determine whether we‘re reading from a primary node. If false, this connection connects to a secondary node and @read_secondaries is true.
@return [Boolean]
If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values. @deprecated
Returns true if it‘s okay to read from a secondary node.
This method exist primarily so that Cursor objects will generate query messages with a slaveOkay value of true.
@return [Boolean] true