Class: Monga::Connections::ProxyConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/monga/connections/proxy_connection.rb

Constant Summary collapse

WAIT =

Pause while searching server in seconds

0.3

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ProxyConnection

Returns a new instance of ProxyConnection.



12
13
14
15
16
# File 'lib/monga/connections/proxy_connection.rb', line 12

def initialize(client)
  @client = client
  @timeout = @client.timeout
  @requests = {}
end

Instance Method Details

#find_server!Object

Find server unless server is found



44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

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



20
21
22
23
24
25
26
27
28
# 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!Object

YEEEHA! Send all collected requests back to client



59
60
61
62
63
64
65
# 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_timeoutObject

If timeout happend send exception to all collected requests.



31
32
33
34
35
36
37
38
39
40
41
# 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

#typeObject



8
9
10
# File 'lib/monga/connections/proxy_connection.rb', line 8

def type
  :block
end