Class: RightScale::BlockingClient
- Defined in:
- lib/right_agent/clients/blocking_client.rb
Overview
Interface to HTTP using RightSupport::Net::HTTPClient and RestClient This interfaces blocks the given thread until an HTTP response is received
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Hash of active connections with request path as key and hash value containing :host and :expires_at.
-
#health_check_proc ⇒ Object
readonly
Fully configured health check procedure for use with this client.
Instance Method Summary collapse
-
#close(reason) ⇒ TrueClass
Close all persistent connections.
-
#initialize(options) ⇒ BlockingClient
constructor
Initialize client.
-
#options(verb, path, params, request_headers, options) ⇒ Array
Construct options for HTTP request.
-
#poll(connection, request_options, stop_at) ⇒ Array
Make long-polling requests until receive data, hit error, or timeout.
-
#request(verb, path, host, connect_options, request_options) ⇒ Array
Make HTTP request.
Constructor Details
#initialize(options) ⇒ BlockingClient
Initialize client
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/right_agent/clients/blocking_client.rb', line 44 def initialize() @connections = {} # Initialize use of proxy if defined if (v = BalancedHttpClient::PROXY_ENVIRONMENT_VARIABLES.detect { |v| ENV.has_key?(v) }) proxy_uri = ENV[v].match(/^[[:alpha:]]+:\/\//) ? URI.parse(ENV[v]) : URI.parse("http://" + ENV[v]) RestClient.proxy = proxy_uri.to_s if proxy_uri end # 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] || BalancedHttpClient::DEFAULT_HEALTH_CHECK_PATH) = { :open_timeout => BalancedHttpClient::DEFAULT_OPEN_TIMEOUT, :timeout => BalancedHttpClient::HEALTH_CHECK_TIMEOUT } [:headers] = {"X-API-Version" => [:api_version]} if [:api_version] request(:get, "", uri.to_s, {}, ) end end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Hash of active connections with request path as key and hash value containing :host and :expires_at
37 38 39 |
# File 'lib/right_agent/clients/blocking_client.rb', line 37 def connections @connections end |
#health_check_proc ⇒ Object (readonly)
Fully configured health check procedure for use with this client
33 34 35 |
# File 'lib/right_agent/clients/blocking_client.rb', line 33 def health_check_proc @health_check_proc end |
Instance Method Details
#close(reason) ⇒ TrueClass
Close all persistent connections
139 140 141 142 |
# File 'lib/right_agent/clients/blocking_client.rb', line 139 def close(reason) @connections = {} true end |
#options(verb, path, params, request_headers, options) ⇒ Array
Construct options for HTTP request
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/right_agent/clients/blocking_client.rb', line 79 def (verb, path, params, request_headers, ) = { :open_timeout => [:open_timeout] || BalancedHttpClient::DEFAULT_OPEN_TIMEOUT, :timeout => [:poll_timeout] || [:request_timeout] || BalancedHttpClient::DEFAULT_REQUEST_TIMEOUT, :headers => request_headers } if [:get, :delete].include?(verb) # Doing own formatting because :query option for HTTPClient uses addressable gem # for conversion and that gem encodes arrays in a Rails-compatible fashion without [] # markers and that is inconsistent with what sinatra expects [:query] = "?#{BalancedHttpClient.format(params)}" if params.is_a?(Hash) && params.any? else [:payload] = JSON.dump(params) [:headers][:content_type] = "application/json" end [{}, ] end |
#poll(connection, request_options, stop_at) ⇒ Array
Make long-polling requests until receive data, hit error, or timeout
125 126 127 128 129 130 131 132 |
# File 'lib/right_agent/clients/blocking_client.rb', line 125 def poll(connection, , stop_at) url = connection[:host] + connection[:path] + .delete(:query).to_s begin result, code, body, headers = request_once(:get, url, ) end until result || Time.now >= stop_at connection[:expires_at] = Time.now + BalancedHttpClient::CONNECTION_REUSE_TIMEOUT [result, code, body, headers] end |
#request(verb, path, host, connect_options, request_options) ⇒ Array
Make HTTP request
108 109 110 111 112 113 |
# File 'lib/right_agent/clients/blocking_client.rb', line 108 def request(verb, path, host, , ) url = host + path + .delete(:query).to_s result = request_once(verb, url, ) @connections[path] = {:host => host, :path => path, :expires_at => Time.now + BalancedHttpClient::CONNECTION_REUSE_TIMEOUT } result end |