class Monga::Clients::SingleInstanceClient

Attributes

status[R]

Status will inform Replica Set client of current client's status primary/secondary/nil

Public Class Methods

new(opts) click to toggle source
# File lib/monga/clients/single_instance_client.rb, line 7
def initialize(opts)
  pool_size = opts[:pool_size]
  if pool_size && pool_size > 1
    @connection_pool = Monga::ConnectionPool.new(opts)
  else
    @connection = Monga::Connection.new(opts)
  end
end

Public Instance Methods

aquire_connection() click to toggle source

If single connection then return it. If connection pool then aquire connection from pool. If connection is not connected, then status will be setted to nil.

# File lib/monga/clients/single_instance_client.rb, line 19
def aquire_connection
  conn = if @connection_pool
    @connection_pool.aquire_connection
  else
    @connection
  end
  @status = nil unless conn.connected?
  conn
end
connected?() click to toggle source
# File lib/monga/clients/single_instance_client.rb, line 29
def connected?
  aquire_connection.connected?
end
force_status!() { |status| ... } click to toggle source

Check status of connection. If ReplicaSetClient can't find connection with read_pref status it will send foce_status! to all clients while timout happend or while preferred status will be returned

# File lib/monga/clients/single_instance_client.rb, line 37
def force_status!
  if connected?
    conn = aquire_connection
    conn.is_master? do |status|
      @status = status
      yield(@status) if block_given?
    end
  else
    yield(@status) if block_given?
  end
end
primary?() click to toggle source
# File lib/monga/clients/single_instance_client.rb, line 49
def primary?
  @status == :primary
end
secondary?() click to toggle source
# File lib/monga/clients/single_instance_client.rb, line 53
def secondary?
  @status == :secondary
end