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(types = []) { ... } ⇒ 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.
-
#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, options = {}) ⇒ 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, options = {}) ⇒ 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
241 242 243 244 245 |
# File 'lib/right_agent/clients/right_http_client.rb', line 241 def close(scope = :all) @router.close(scope) if @router @api.close(scope) if @api true end |
#communicated(types = []) { ... } ⇒ TrueClass
Set callback for each successful communication excluding health checks
225 226 227 228 229 230 231 |
# File 'lib/right_agent/clients/right_http_client.rb', line 225 def communicated(types = [], &callback) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @auth.communicated(&callback) if types.empty? || types.include?(:auth) @api.communicated(&callback) if @api && (types.empty? || types.include?(:api)) @router.communicated(&callback) if @router && (types.empty? || types.include?(:router)) true end |
#init(auth_client, options = {}) ⇒ TrueClass
Initialize RightNet client Must be called before any other functions are usable
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/right_agent/clients/right_http_client.rb', line 62 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
188 189 190 191 |
# File 'lib/right_agent/clients/right_http_client.rb', line 188 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
169 170 171 172 |
# File 'lib/right_agent/clients/right_http_client.rb', line 169 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, options = {}) ⇒ 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
111 112 113 114 115 |
# File 'lib/right_agent/clients/right_http_client.rb', line 111 def push(type, payload = nil, target = nil, = {}) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth client = (@api && @api.support?(type)) ? @api : @router client.push(type, payload, target, ) end |
#request(type, payload = nil, target = nil, options = {}) ⇒ 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
148 149 150 151 152 |
# File 'lib/right_agent/clients/right_http_client.rb', line 148 def request(type, payload = nil, target = nil, = {}) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth client = (@api && @api.support?(type)) ? @api : @router client.request(type, payload, target, ) end |
#self_href ⇒ String, NilClass
Resource href associated with the user of this client
196 197 198 |
# File 'lib/right_agent/clients/right_http_client.rb', line 196 def self_href @api.self_href if @api end |
#stats(reset = false) ⇒ Hash
Current statistics for this client
255 256 257 258 259 260 261 262 |
# File 'lib/right_agent/clients/right_http_client.rb', line 255 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
210 211 212 213 214 |
# File 'lib/right_agent/clients/right_http_client.rb', line 210 def status(&callback) raise RuntimeError, "#{self.class.name}#init was not called" unless @auth @status_callbacks = (@status_callbacks || []) << callback if callback @status end |