Class: RightScale::BalancedHttpClient

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

Overview

HTTP REST client for request balanced access to RightScale servers It is intended for use by instance agents and by infrastructure servers and therefore supports both session cookie and global session-based authentication

Defined Under Namespace

Classes: NotResponding

Constant Summary collapse

RETRY_STATUS_CODES =

HTTP status codes for which a retry is warranted, which is limited to when server is not accessible for some reason (502, 503) or server response indicates that the request could not be routed for some retryable reason (504)

[502, 503, 504]
DEFAULT_OPEN_TIMEOUT =

Default time for HTTP connection to open

2
HEALTH_CHECK_TIMEOUT =

Default time to wait for health check response

5
DEFAULT_REQUEST_TIMEOUT =

Default time to wait for response from request

30
DEFAULT_HEALTH_CHECK_PATH =

Default health check path

"/health-check"
FILTERED_PARAM_VALUE =

Text used for filtered parameter value

"<hidden>"
PROXY_ENVIRONMENT_VARIABLES =

Environment variables to examine for proxy settings, in order

['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy', 'ALL_PROXY']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, options = {}) ⇒ BalancedHttpClient

Create client for making HTTP REST requests

Parameters:

  • urls (Array, String)

    of server being accessed as array or comma-separated string

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :api_version (String)

    for X-API-Version header

  • :server_name (String)

    of server for use in exceptions; defaults to host name

  • :health_check_path (String)

    in URI for health check resource; defaults to DEFAULT_HEALTH_CHECK_PATH

  • :filter_params (Array)

    symbols or strings for names of request parameters whose values are to be hidden when logging; can be augmented on individual requests



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/right_agent/clients/balanced_http_client.rb', line 69

def initialize(urls, options = {})
  @urls = split(urls)
  @api_version = options[:api_version]
  @server_name = options[:server_name]
  @filter_params = (options[:filter_params] || []).map { |p| p.to_s }

  # Create health check proc for use by request balancer
  # Strip user and password from host name since health-check does not require authorization
  @health_check_proc = Proc.new do |host|
    uri = URI.parse(host)
    uri.user = uri.password = nil
    uri.path = uri.path + (options[:health_check_path] || DEFAULT_HEALTH_CHECK_PATH)
    check_options = {
      :open_timeout => DEFAULT_OPEN_TIMEOUT,
      :timeout => HEALTH_CHECK_TIMEOUT }
    check_options[:headers] = {"X-API-Version" => @api_version} if @api_version
    RightSupport::Net::HTTPClient.new.get(uri.to_s, check_options)
  end

  # Initialize use of proxy if defined
  if (proxy_var = PROXY_ENVIRONMENT_VARIABLES.detect { |v| ENV.has_key?(v) })
    proxy = ENV[proxy_var].match(/^[[:alpha:]]+:\/\//) ? URI.parse(ENV[proxy_var]) : URI.parse("http://" + ENV[proxy_var])
    RestClient.proxy = proxy.to_s if proxy
  end

  # Initialize request balancer
  balancer_options = {
    :policy => RightSupport::Net::LB::HealthCheck,
    :health_check => @health_check_proc }
  @balancer = RightSupport::Net::RequestBalancer.new(@urls, balancer_options)
end

Class Method Details

.exception_text(exception) ⇒ String

Extract text of exception for logging For RestClient exceptions extract useful info from http_body attribute

Parameters:

  • exception (Exception, String, NilClass)

    or failure message

Returns:

  • (String)

    exception text



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/right_agent/clients/balanced_http_client.rb', line 347

def self.exception_text(exception)
  case exception
  when String
    exception
  when RestClient::Exception
    if exception.http_body.nil? || exception.http_body.empty? || exception.http_body =~ /^<html>| html /
      exception.message
    else
      exception.inspect
    end
  when RightSupport::Net::NoResult, NotResponding
    "#{exception.class}: #{exception.message}"
  when Exception
    backtrace = exception.backtrace ? " in\n" + exception.backtrace.join("\n") : ""
    "#{exception.class}: #{exception.message}" + backtrace
  else
    ""
  end
end

Instance Method Details

#check_health(host = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/right_agent/clients/balanced_http_client.rb', line 117

def check_health(host = nil)
  begin
    @health_check_proc.call(host || @urls.first)
  rescue StandardError => e
    if e.respond_to?(:http_code) && RETRY_STATUS_CODES.include?(e.http_code)
      raise NotResponding.new("#{@server_name || host} not responding", e)
    else
      raise
    end
  end
end

#delete(*args) ⇒ Object



113
114
115
# File 'lib/right_agent/clients/balanced_http_client.rb', line 113

def delete(*args)
  request(:delete, *args)
end

#get(*args) ⇒ Object



101
102
103
# File 'lib/right_agent/clients/balanced_http_client.rb', line 101

def get(*args)
  request(:get, *args)
end

#post(*args) ⇒ Object



105
106
107
# File 'lib/right_agent/clients/balanced_http_client.rb', line 105

def post(*args)
  request(:post, *args)
end

#put(*args) ⇒ Object



109
110
111
# File 'lib/right_agent/clients/balanced_http_client.rb', line 109

def put(*args)
  request(:put, *args)
end