class Monga::Connections::ProxyConnection

Constants

WAIT

Pause while searching server in seconds

Public Class Methods

new(client) click to toggle source
# File lib/monga/connections/proxy_connection.rb, line 12
def initialize(client)
  @client = client
  @timeout = @client.timeout
  @requests = {}
end

Public Instance Methods

find_server!() click to toggle source

Find server unless server is found

# File lib/monga/connections/proxy_connection.rb, line 44
def find_server!
  @client.clients.each do |client|
    client.force_status! do |status|
      if status == :primary && [:primary, :primary_preferred, :secondary_preferred].include?(@client.read_pref)
        @pending_server = false
        server_found!
      elsif status == :secondary && [:secondary, :primary_preferred, :secondary_preferred].include?(@client.read_pref)
        @pending_server = false
        server_found!
      end
    end
  end
end
send_command(msg, request_id = nil, &cb) click to toggle source

If timeout is defined then collect request and start timout. If timout is not defined or zero then return exception.

# File lib/monga/connections/proxy_connection.rb, line 20
def send_command(msg, request_id = nil, &cb)
  if @timeout && @timeout > 0 
    @requests[request_id] = [msg, cb] if cb
    set_timeout
  else
    error = Monga::Exceptions::Disconnected.new "Can't find appropriate server (all disconnected)"
    cb.call(error) if cb
  end
end
server_found!() click to toggle source

YEEEHA! Send all collected requests back to client

# File lib/monga/connections/proxy_connection.rb, line 59
def server_found!
  @not_found = false
  @requests.keys.each do |request_id|
    msg, blk = @requests.delete request_id
    @client.aquire_connection.send_command(msg, request_id, &blk)
  end
end
set_timeout() click to toggle source

If timeout happend send exception to all collected requests.

# File lib/monga/connections/proxy_connection.rb, line 31
def set_timeout
  @not_found = true
  Timeout::timeout(@timeout) do
    while @not_found
      find_server!
      sleep(WAIT)
    end
  end
rescue Timeout::Error => e
  raise Monga::Exceptions::Disconnected.new "Can't find appropriate server (all disconnected)"
end
type() click to toggle source
# File lib/monga/connections/proxy_connection.rb, line 8
def type
  :block
end