# File lib/monga/clients/replica_set_client.rb, line 51 def primary pr = @clients.detect{ |c| c.primary? && c.connected? } pr.aquire_connection if pr end
class Monga::Clients::ReplicaSetClient
Attributes
clients[R]
read_pref[R]
timeout[R]
Public Class Methods
new(opts)
click to toggle source
ReplicaSetClient
creates SingleInstanceClient
to each server. Accepts
-
servers - you could pas them as a array of servers (['1.1.1.1:27017', '1.1.1.2:27017']), or as a array of hashes: ([{host: '1.1.1.1', port: 27017}, {host: '1.1.1.2', port: 27017}])
-
read_pref
- read preferrence (:primary, :primary_preferred, :secondary, :secondary_preferred) -
pool_size - connection pool size to each server
-
type - connection type (:em/:sync/:block)
# File lib/monga/clients/replica_set_client.rb, line 11 def initialize(opts) @timeout = opts[:timeout] @read_pref = opts[:read_pref] || :primary servers = opts.delete :servers @clients = servers.map do |server| case server when Hash Monga::Clients::SingleInstanceClient.new(opts.merge(server)) when String h, p = server.split(":") o = { host: h, port: p.to_i } Monga::Clients::SingleInstanceClient.new(opts.merge(o)) end end @proxy_connection = Monga::Connection.proxy_connection_class(opts[:type], self) end
Public Instance Methods
aquire_connection()
click to toggle source
Aquires connection due to read_pref
option
# File lib/monga/clients/replica_set_client.rb, line 31 def aquire_connection server = case @read_pref when :primary primary when :secondary secondary when :primary_preferred primary || secondary when :secondary_preferred secondary || primary when :nearest raise ArgumentError, "nearest read preferrence is not implemented yet" else raise ArgumentError, "`#{@read_pref}` is not valid read preferrence, use :primary, :primary_preferred, :secondary, or :secondary_preferred" end server || @proxy_connection end
primary()
click to toggle source
Fetch primary server
secondary()
click to toggle source
Fetch secondary server
# File lib/monga/clients/replica_set_client.rb, line 57 def secondary sc = @clients.select{ |c| c.secondary? && c.connected? }.sample sc.aquire_connection if sc end