Class: RightScale::RouterClient
- Inherits:
-
BaseRetryClient
- Object
- BaseRetryClient
- RightScale::RouterClient
- Defined in:
- lib/right_agent/clients/router_client.rb
Overview
HTTP interface to RightNet router
Constant Summary collapse
- API_VERSION =
RightNet router API version for use in X-API-Version header
"2.0"- CONNECT_INTERVAL =
Initial interval between attempts to make a WebSocket connection
30- MAX_CONNECT_INTERVAL =
Maximum interval between attempts to make a WebSocket connection
60 * 60 * 24
- RECONNECT_INTERVAL =
Initial interval between attempts to reconnect or long-poll when router is not responding
2- MAX_RECONNECT_INTERVAL =
Maximum interval between attempts to reconnect or long-poll when router is not responding
60- CHECK_INTERVAL =
Interval between checks for lost WebSocket connection
5- BACKOFF_FACTOR =
Backoff factor for connect and reconnect intervals
2- NORMAL_CLOSE =
WebSocket close status codes
1000- SHUTDOWN_CLOSE =
1001- PROTOCOL_ERROR_CLOSE =
1002- UNEXPECTED_ERROR_CLOSE =
1011- DEFAULT_LISTEN_TIMEOUT =
Default time to wait for an event or to ping WebSocket
60
Constants inherited from BaseRetryClient
BaseRetryClient::DEFAULT_OPEN_TIMEOUT, BaseRetryClient::DEFAULT_RECONNECT_INTERVAL, BaseRetryClient::DEFAULT_REQUEST_TIMEOUT, BaseRetryClient::DEFAULT_RETRY_INTERVALS, BaseRetryClient::DEFAULT_RETRY_TIMEOUT, BaseRetryClient::PERMITTED_STATE_TRANSITIONS
Instance Attribute Summary
Attributes inherited from BaseRetryClient
Instance Method Summary collapse
-
#close(scope = :all) ⇒ TrueClass
Take any actions necessary to quiesce client interaction in preparation for agent termination but allow any active requests to complete.
-
#initialize(auth_client, options) ⇒ RouterClient
constructor
Create RightNet router client.
-
#listen(routing_keys) {|event| ... } ⇒ TrueClass
Receive events via an HTTP WebSocket if available, otherwise via an HTTP long-polling This is a blocking call and therefore should be used from a thread different than otherwise used with this object, e.g., EM.defer thread.
-
#notify(event, routing_keys) ⇒ TrueClass
Route event Use WebSocket if possible Do not block this request even if in the process of closing since used for request responses.
-
#push(type, payload, target, token = nil) ⇒ NilClass
Route a request to a single target or multiple targets with no response expected Persist the request en route to reduce the chance of it being lost at the expense of some additional network overhead Enqueue the request if the target is not currently available Never automatically retry the request if there is the possibility of it being duplicated Set time-to-live to be forever.
-
#request(type, payload, target, token = nil) ⇒ Result, NilClass
Route a request to a single target with a response expected Automatically retry the request if a response is not received in a reasonable amount of time or if there is a non-delivery response indicating the target is not currently available Timeout the request if a response is not received in time, typically configured to 30 sec Because of retries there is the possibility of duplicated requests, and these are detected and discarded automatically for non-idempotent actions Allow the request to expire per the agent’s configured time-to-live, typically 1 minute.
-
#stats(reset = false) ⇒ Hash
Current statistics for this client.
Methods inherited from BaseRetryClient
Constructor Details
#initialize(auth_client, options) ⇒ RouterClient
Create RightNet router client
89 90 91 92 |
# File 'lib/right_agent/clients/router_client.rb', line 89 def initialize(auth_client, ) init(:router, auth_client, .merge(:server_name => "RightNet", :api_version => API_VERSION)) @options[:listen_timeout] ||= DEFAULT_LISTEN_TIMEOUT end |
Instance Method Details
#close(scope = :all) ⇒ TrueClass
Take any actions necessary to quiesce client interaction in preparation for agent termination but allow any active requests to complete
254 255 256 257 |
# File 'lib/right_agent/clients/router_client.rb', line 254 def close(scope = :all) super @websocket.close(SHUTDOWN_CLOSE, "Agent terminating") if @websocket end |
#listen(routing_keys) {|event| ... } ⇒ TrueClass
Receive events via an HTTP WebSocket if available, otherwise via an HTTP long-polling This is a blocking call and therefore should be used from a thread different than otherwise used with this object, e.g., EM.defer thread
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/right_agent/clients/router_client.rb', line 219 def listen(routing_keys, &handler) raise ArgumentError, "Block missing" unless block_given? @connect_interval = CONNECT_INTERVAL @last_connect_time = Time.now - @connect_interval @reconnect_interval = RECONNECT_INTERVAL uuids = nil retries = 0 until [:closing, :closed].include?(state) do if @websocket @connect_interval = CONNECT_INTERVAL @reconnect_interval = RECONNECT_INTERVAL sleep(CHECK_INTERVAL) next elsif retry_connect? @last_connect_time = Time.now @close_code = @close_reason = nil @stats["reconnects"].update("websocket") if (retries += 1) > 1 next if try_connect(routing_keys, &handler) end # Resort to long-polling if WebSocket not usable uuids = try_long_poll(routing_keys, uuids, &handler) if @websocket.nil? end true end |
#notify(event, routing_keys) ⇒ TrueClass
Route event Use WebSocket if possible Do not block this request even if in the process of closing since used for request responses
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/right_agent/clients/router_client.rb', line 185 def notify(event, routing_keys) event[:uuid] ||= RightSupport::Data::UUID.generate event[:version] ||= AgentConfig.protocol_version params = {:event => event} params[:routing_keys] = routing_keys if routing_keys if @websocket path = event[:path] ? " #{event[:path]}" : "" to = routing_keys ? " to #{routing_keys.inspect}" : "" Log.info("Sending EVENT <#{event[:uuid]}> #{event[:type]}#{path}#{to}") @websocket.send(JSON.dump(params)) else make_request(:post, "/notify", params, "notify", event[:uuid], :filter_params => ["event"]) end true end |
#push(type, payload, target, token = nil) ⇒ NilClass
Route a request to a single target or multiple targets with no response expected Persist the request en route to reduce the chance of it being lost at the expense of some additional network overhead Enqueue the request if the target is not currently available Never automatically retry the request if there is the possibility of it being duplicated Set time-to-live to be forever
125 126 127 128 129 130 131 |
# File 'lib/right_agent/clients/router_client.rb', line 125 def push(type, payload, target, token = nil) params = { :type => type, :payload => payload, :target => target } make_request(:post, "/push", params, type.split("/")[2], token) end |
#request(type, payload, target, token = nil) ⇒ Result, NilClass
Route a request to a single target with a response expected Automatically retry the request if a response is not received in a reasonable amount of time or if there is a non-delivery response indicating the target is not currently available Timeout the request if a response is not received in time, typically configured to 30 sec Because of retries there is the possibility of duplicated requests, and these are detected and discarded automatically for non-idempotent actions Allow the request to expire per the agent’s configured time-to-live, typically 1 minute
161 162 163 164 165 166 167 |
# File 'lib/right_agent/clients/router_client.rb', line 161 def request(type, payload, target, token = nil) params = { :type => type, :payload => payload, :target => target } make_request(:post, "/request", params, type.split("/")[2], token) end |
#stats(reset = false) ⇒ Hash
Current statistics for this client
270 271 272 273 274 275 |
# File 'lib/right_agent/clients/router_client.rb', line 270 def stats(reset = false) events = @stats["events"].all stats = super(reset) stats["events"] = events stats end |