Class: Monga::Connections::EMProxyConnection
- Inherits:
-
Object
- Object
- Monga::Connections::EMProxyConnection
- Defined in:
- lib/monga/connections/em_proxy_connection.rb
Overview
ProxyConnection accepts requests when ReplicaSetClient didn’t know where to send requests. I.E. when client is just initialized here is no any established connections, so client waits for the connection ready to accept requests. Also, when primary is down it will collect request while nodes are voting. Importaint to say, that requests will be stored in this object only for ‘timeout` period.
Direct Known Subclasses
Constant Summary collapse
- WAIT =
Pause while searching server in seconds
0.05
Instance Method Summary collapse
-
#find_server!(i = 0) ⇒ Object
Find server unless server is found.
-
#initialize(client) ⇒ EMProxyConnection
constructor
A new instance of EMProxyConnection.
-
#send_command(msg, request_id = nil, &cb) ⇒ Object
If timeout is defined then collect request and start timeout.
-
#server_found! ⇒ Object
YEEEHA! Send all collected requests back to client.
-
#set_timeout ⇒ Object
If timeout happend send exception to all collected requests.
- #timeout_happend ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(client) ⇒ EMProxyConnection
Returns a new instance of EMProxyConnection.
11 12 13 14 15 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 11 def initialize(client) @client = client @timeout = @client.timeout @requests = {} end |
Instance Method Details
#find_server!(i = 0) ⇒ Object
Find server unless server is found
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 54 def find_server!(i = 0) @pending_server = true if @pending_timeout && !@timeout_happend size = @client.clients.size client = @client.clients[i%size] client.force_status! do |status| if status == :primary && [:primary, :primary_preferred, :secondary_preferred].include?(@client.read_pref) server_found! elsif status == :secondary && [:secondary, :primary_preferred, :secondary_preferred].include?(@client.read_pref) server_found! else EM::Timer.new(WAIT) do find_server!(i+1) end end end else timeout_happend end end |
#send_command(msg, request_id = nil, &cb) ⇒ Object
If timeout is defined then collect request and start timeout. If timeout is not defined or zero then return exception.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 23 def send_command(msg, request_id = nil, &cb) if @timeout && @timeout > 0 @requests[request_id] = [msg, cb] if cb set_timeout unless @pending_timeout find_server! unless @pending_server else error = Monga::Exceptions::Disconnected.new "Can't find appropriate server (all disconnected) without timeout" cb.call(error) if cb end end |
#server_found! ⇒ Object
YEEEHA! Send all collected requests back to client
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 76 def server_found! @pending_server = false @pending_timeout.cancel if @pending_timeout @pending_timeout = nil @timeout_happend = 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 ⇒ Object
If timeout happend send exception to all collected requests.
35 36 37 38 39 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 35 def set_timeout @pending_timeout = EM::Timer.new(@timeout) do @timeout_happend = true end end |
#timeout_happend ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 41 def timeout_happend @timeout_happend = false @pending_timeout.cancel if @pending_timeout @pending_timeout = false @pending_server = false @requests.keys.each do |request_id| msg, cb = @requests.delete request_id error = Monga::Exceptions::Disconnected.new "Can't find appropriate server (all disconnected)" cb.call(error) if cb end end |
#type ⇒ Object
17 18 19 |
# File 'lib/monga/connections/em_proxy_connection.rb', line 17 def type :em end |