Class: RightScale::BlockingClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BlockingClient

Initialize client

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :api_version (String)

    for X-API-Version header

  • :health_check_path (String)

    in URI for health check resource; defaults to DEFAULT_HEALTH_CHECK_PATH



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(options)
  @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 + (options[:health_check_path] || BalancedHttpClient::DEFAULT_HEALTH_CHECK_PATH)
    request_options = {
      :open_timeout => BalancedHttpClient::DEFAULT_OPEN_TIMEOUT,
      :timeout => BalancedHttpClient::HEALTH_CHECK_TIMEOUT }
    request_options[:headers] = {"X-API-Version" => options[:api_version]} if options[:api_version]
    request(:get, "", uri.to_s, {}, request_options)
  end
end

Instance Attribute Details

#connectionsObject (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_procObject (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

Parameters:

  • reason (String)

    for closing

Returns:

  • (TrueClass)

    always true



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

Parameters:

  • verb (Symbol)

    for HTTP REST request

  • path (String)

    in URI for desired resource (ignored)

  • params (Hash)

    for HTTP request

  • request_headers (String)

    to be applied to request

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :open_timeout (Numeric)

    maximum wait for connection; defaults to DEFAULT_OPEN_TIMEOUT

  • :request_timeout (Numeric)

    maximum wait for response; defaults to DEFAULT_REQUEST_TIMEOUT

  • :poll_timeout (Numeric)

    maximum wait for individual poll; defaults to :request_timeout

Returns:

  • (Array)

    connect and request option hashes



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 options(verb, path, params, request_headers, options)
  request_options = {
    :open_timeout => options[:open_timeout] || BalancedHttpClient::DEFAULT_OPEN_TIMEOUT,
    :timeout => options[:poll_timeout] || options[: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
    request_options[:query] = "?#{BalancedHttpClient.format(params)}" if params.is_a?(Hash) && params.any?
  else
    request_options[:payload] = JSON.dump(params)
    request_options[:headers][:content_type] = "application/json"
  end
  [{}, request_options]
end

#poll(connection, request_options, stop_at) ⇒ Array

Make long-polling requests until receive data, hit error, or timeout

Parameters:

  • connection (Hash)

    to server from previous request with keys :host, :path, and :expires_at, with the :expires_at being adjusted on return

  • request_options (Hash)

    for HTTP request

  • stop_at (Time)

    time for polling

Returns:

  • (Array)

    result to be returned followed by response code, body, and headers

Raises:



125
126
127
128
129
130
131
132
# File 'lib/right_agent/clients/blocking_client.rb', line 125

def poll(connection, request_options, stop_at)
  url = connection[:host] + connection[:path] + request_options.delete(:query).to_s
  begin
    result, code, body, headers = request_once(:get, url, request_options)
  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

Parameters:

  • verb (Symbol)

    for HTTP REST request

  • path (String)

    in URI for desired resource

  • host (String)

    name of server

  • connect_options (Hash)

    for HTTP connection (ignored)

  • request_options (Hash)

    for HTTP request

Returns:

  • (Array)

    result to be returned followed by response code, body, and headers

Raises:



108
109
110
111
112
113
# File 'lib/right_agent/clients/blocking_client.rb', line 108

def request(verb, path, host, connect_options, request_options)
  url = host + path + request_options.delete(:query).to_s
  result = request_once(verb, url, request_options)
  @connections[path] = {:host => host, :path => path, :expires_at => Time.now + BalancedHttpClient::CONNECTION_REUSE_TIMEOUT }
  result
end