Class: RightScale::BalancedHttpClient
- 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
-
.exception_text(exception) ⇒ String
Extract text of exception for logging For RestClient exceptions extract useful info from http_body attribute.
Instance Method Summary collapse
- #check_health(host = nil) ⇒ Object
- #delete(*args) ⇒ Object
- #get(*args) ⇒ Object
-
#initialize(urls, options = {}) ⇒ BalancedHttpClient
constructor
Create client for making HTTP REST requests.
- #post(*args) ⇒ Object
- #put(*args) ⇒ Object
Constructor Details
#initialize(urls, options = {}) ⇒ BalancedHttpClient
Create client for making HTTP REST 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, = {}) @urls = split(urls) @api_version = [:api_version] @server_name = [:server_name] @filter_params = ([: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 + ([:health_check_path] || DEFAULT_HEALTH_CHECK_PATH) = { :open_timeout => DEFAULT_OPEN_TIMEOUT, :timeout => HEALTH_CHECK_TIMEOUT } [:headers] = {"X-API-Version" => @api_version} if @api_version RightSupport::Net::HTTPClient.new.get(uri.to_s, ) 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 = { :policy => RightSupport::Net::LB::HealthCheck, :health_check => @health_check_proc } @balancer = RightSupport::Net::RequestBalancer.new(@urls, ) end |
Class Method Details
.exception_text(exception) ⇒ String
Extract text of exception for logging For RestClient exceptions extract useful info from http_body attribute
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. else exception.inspect end when RightSupport::Net::NoResult, NotResponding "#{exception.class}: #{exception.}" when Exception backtrace = exception.backtrace ? " in\n" + exception.backtrace.join("\n") : "" "#{exception.class}: #{exception.}" + 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 |