Class: RightScale::RightHttpClient
- Includes:
- RightSupport::Ruby::EasySingleton
- Defined in:
- lib/right_agent/clients/right_http_client.rb
Overview
HTTP interface to RightNet router and to RightNet services in RightApi It is intended for use by instance agents and infrastructure servers The interface supports sending requests and sending/receiving events Events are received over a WebSocket if possible, otherwise via long-polling Requests to RightNet and RightApi are automatically retried to overcome connectivity failures A status callback is provided so that the user of this client can take action (e.g., queue requests) when connectivity is lost Health checks are sent periodically to try to recover from connectivity failures
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 Only router and api clients are closed, not auth client.
-
#communicated(type = nil) { ... } ⇒ TrueClass
Set callback for each successful communication excluding health checks.
-
#init(auth_client, options = {}) ⇒ TrueClass
Initialize RightNet client Must be called before any other functions are usable.
-
#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.
-
#push(type, payload = nil, target = nil, 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 = nil, target = nil, 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.
-
#self_href ⇒ String, NilClass
Resource href associated with the user of this client.
-
#stats(reset = false) ⇒ Hash
Current statistics for this client.
-
#status {|type, status| ... } ⇒ Hash
Record callback to be notified of status changes Multiple callbacks are supported.
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 Only router and api clients are closed, not auth client
234 235 236 237 238 |
# File 'lib/right_agent/clients/right_http_client.rb', line 234 def close(scope = :all) @router.close(scope) if @router @api.close(scope) if @api true end |
#communicated(type = nil) { ... } ⇒ TrueClass
Set callback for each successful communication excluding health checks
219 220 221 222 223 224 |
# File 'lib/right_agent/clients/right_http_client.rb', line 219 def communicated(type = nil, &callback) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @api.communicated(&callback) if @api && [nil, :api].include?(type) @router.communicated(&callback) if @router && [nil, :router].include?(type) true end |
#init(auth_client, options = {}) ⇒ TrueClass
Initialize RightNet client Must be called before any other functions are usable
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/right_agent/clients/right_http_client.rb', line 60 def init(auth_client, = {}) raise ArgumentError, "No authorization client provided" unless auth_client.is_a?(AuthClient) @status = {} callback = lambda { |type, state| update_status(type, state) } @auth = auth_client @status[:auth] = @auth.status(&callback) @router = RouterClient.new(@auth, ) @status[:router] = @router.status(&callback) if @auth.api_url @api = ApiClient.new(@auth, ) @status[:api] = @api.status(&callback) end true 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
182 183 184 185 |
# File 'lib/right_agent/clients/right_http_client.rb', line 182 def listen(routing_keys, &handler) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @router.listen(routing_keys, &handler) end |
#notify(event, routing_keys) ⇒ TrueClass
Route event Use WebSocket if possible Do not block this request even if in the process of closing
161 162 163 164 |
# File 'lib/right_agent/clients/right_http_client.rb', line 161 def notify(event, routing_keys) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @router.notify(event, routing_keys) end |
#push(type, payload = nil, target = nil, 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
106 107 108 109 110 |
# File 'lib/right_agent/clients/right_http_client.rb', line 106 def push(type, payload = nil, target = nil, token = nil) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth client = (@api && @api.support?(type)) ? @api : @router client.push(type, payload, target, token) end |
#request(type, payload = nil, target = nil, 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
140 141 142 143 144 |
# File 'lib/right_agent/clients/right_http_client.rb', line 140 def request(type, payload = nil, target = nil, token = nil) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth client = (@api && @api.support?(type)) ? @api : @router client.request(type, payload, target, token) end |
#self_href ⇒ String, NilClass
Resource href associated with the user of this client
190 191 192 |
# File 'lib/right_agent/clients/right_http_client.rb', line 190 def self_href @api.self_href if @api end |
#stats(reset = false) ⇒ Hash
Current statistics for this client
248 249 250 251 252 253 254 255 |
# File 'lib/right_agent/clients/right_http_client.rb', line 248 def stats(reset = false) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth stats = {} stats["auth stats"] = @auth.stats(reset) stats["router stats"] = @router.stats(reset) stats["api stats"] = @api.stats(reset) if @api stats end |
#status {|type, status| ... } ⇒ Hash
Record callback to be notified of status changes Multiple callbacks are supported
204 205 206 207 208 |
# File 'lib/right_agent/clients/right_http_client.rb', line 204 def status(&callback) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @status_callbacks = (@status_callbacks || []) << callback if callback @status end |