Class: RightScale::BaseRetryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/clients/base_retry_client.rb

Overview

Abstract base client for creating RightNet and RightApi clients with retry capability Requests are automatically retried to overcome connectivity failures A status callback is provided so that the user of the client can take action (e.g., queue requests) when connectivity is lost Health checks are sent periodically to try to recover from connectivity failures

Direct Known Subclasses

ApiClient, RouterClient

Constant Summary collapse

DEFAULT_RECONNECT_INTERVAL =

Interval between reconnect attempts

15
DEFAULT_OPEN_TIMEOUT =

Default time to wait for HTTP connection to open

2
DEFAULT_REQUEST_TIMEOUT =

Default time to wait for response from request, which is chosen to be 5 seconds greater than the response timeout inside the RightNet router

35
DEFAULT_RETRY_INTERVALS =

Default interval between successive retries and default maximum elapsed time until stop retrying These are chosen to be consistent with the retry sequencing for RightNet retryable requests (per :retry_interval and :retry_timeout agent deployer configuration parameters for RightNet router), so that if the retrying happens within the router, it will not retry here

[4, 12, 36]
DEFAULT_RETRY_TIMEOUT =
25
PERMITTED_STATE_TRANSITIONS =
{
:pending      => [:pending, :connected, :disconnected, :failed, :closed],
:connected    => [:connected, :disconnected, :failed, :closing, :closed],
:disconnected => [:connected, :disconnected, :failed, :closed],
:failed       => [:failed, :closed],
:closing      => [:closing, :closed],
:closed       => [:closed] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stateObject

State of this client: :pending, :connected, :disconnected, :failed, :closing, :closed



53
54
55
# File 'lib/right_agent/clients/base_retry_client.rb', line 53

def state
  @state
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

Parameters:

  • scope (Symbol) (defaults to: :all)

    of close action: :receive for just closing receive side of client, :all for closing both receive and send side; defaults to :all

Returns:

  • (TrueClass)

    always true



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/right_agent/clients/base_retry_client.rb', line 142

def close(scope = :all)
  if scope == :receive && state == :connected
    self.state = :closing
  else
    self.state = :closed
    @reconnect_timer.cancel if @reconnect_timer
    @reconnect_timer = nil
  end
  close_http_client("terminating")
  true
end

#communicated { ... } ⇒ TrueClass

Set callback for each successful communication excluding health checks Multiple callbacks are supported

Yields:

  • required block executed after successful communication

Returns:

  • (TrueClass)

    always true

Raises:

  • (ArgumentError)

    block missing



128
129
130
131
132
133
# File 'lib/right_agent/clients/base_retry_client.rb', line 128

def communicated(&callback)
  raise ArgumentError, "Block missing" unless callback
  @communicated_callbacks ||= []
  @communicated_callbacks << callback
  true
end

#init(type, auth_client, options) ⇒ Boolean

Set configuration of this client and initialize HTTP access

Parameters:

  • type (Symbol)

    of server for use in obtaining URL from auth_client, e.g., :router

  • auth_client (AuthClient)

    providing authorization session for HTTP requests

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :server_name (String)

    for use in reporting errors, e.g., RightNet

  • :api_version (String)

    of server for use in X-API-Version header

  • :open_timeout (Numeric)

    maximum wait for connection; defaults to DEFAULT_OPEN_TIMEOUT

  • :request_timeout (Numeric)

    maximum wait for response; defaults to DEFAULT_REQUEST_TIMEOUT

  • :retry_timeout (Numeric)

    maximum before stop retrying; defaults to DEFAULT_RETRY_TIMEOUT

  • :retry_intervals (Array)

    between successive retries; defaults to DEFAULT_RETRY_INTERVALS

  • :retry_enabled (Boolean)

    for requests that fail to connect or that return a retry result

  • :reconnect_interval (Numeric)

    for reconnect attempts after lose connectivity

  • :non_blocking (Boolean)

    i/o is to be used for HTTP requests by applying EM::HttpRequest and fibers instead of RestClient; requests remain synchronous

  • :filter_params (Array)

    symbols or strings for names of request parameters whose values are to be hidden when logging; also applied to contents of any parameters named :payload; can be augmented on individual requests

Returns:

  • (Boolean)

    whether currently connected

Raises:

  • (ArgumentError)

    auth client does not support this client type

  • (ArgumentError)

    :api_version missing



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/right_agent/clients/base_retry_client.rb', line 86

def init(type, auth_client, options)
  raise ArgumentError, "Auth client does not support server type #{type.inspect}" unless auth_client.respond_to?(type.to_s + "_url")
  raise ArgumentError, ":api_version option missing" unless options[:api_version]
  @type = type
  @auth_client = auth_client
  @http_client = nil
  @status_callbacks = []
  @options = options.dup
  @options[:server_name] ||= type.to_s
  @options[:open_timeout] ||= DEFAULT_OPEN_TIMEOUT
  @options[:request_timeout] ||= DEFAULT_REQUEST_TIMEOUT
  @options[:retry_timeout] ||= DEFAULT_RETRY_TIMEOUT
  @options[:retry_intervals] ||= DEFAULT_RETRY_INTERVALS
  @options[:reconnect_interval] ||= DEFAULT_RECONNECT_INTERVAL
  reset_stats
  @state = :pending
  create_http_client
  enable_use if check_health == :connected
  state == :connected
end

#stats(reset = false) ⇒ Hash

Current statistics for this client

Parameters:

  • reset (Boolean) (defaults to: false)

    the statistics after getting the current ones

Returns:

  • (Hash)

    current statistics

    Hash, NilClass

    “reconnects” Activity stats or nil if none

    Hash, NilClass

    “request failures” Activity stats or nil if none

    Hash, NilClass

    “request sent” Activity stats or nil if none

    Hash, NilClass

    “state” Activity stats or nil if none



163
164
165
166
167
168
# File 'lib/right_agent/clients/base_retry_client.rb', line 163

def stats(reset = false)
  stats = {}
  @stats.each { |k, v| stats[k] = v.all }
  reset_stats if reset
  stats
end

#status {|type, status| ... } ⇒ Symbol

Record callback to be notified of status changes Multiple callbacks are supported

Yields:

  • (type, status)

    called when status changes (optional)

Yield Parameters:

  • type (Symbol)

    of client reporting status change

  • state (Symbol)

    of client

Returns:

  • (Symbol)

    current state



115
116
117
118
# File 'lib/right_agent/clients/base_retry_client.rb', line 115

def status(&callback)
  @status_callbacks << callback if callback
  state
end