Class: EsClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/es_client/client.rb

Constant Summary collapse

RETRY_TIMES =
1
HTTP_VERBS =
%i(get post put delete head)
SUCCESS_HTTP_CODES =
[200, 201]

Instance Method Summary collapse

Constructor Details

#initialize(host, options) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/es_client/client.rb', line 9

def initialize(host, options)
  @host = host
  @options = options
end

Instance Method Details

#httpObject



49
50
51
# File 'lib/es_client/client.rb', line 49

def http
  @http ||= Excon.new(@host, @options)
end

#log(message, level = :info) ⇒ Object



57
58
59
# File 'lib/es_client/client.rb', line 57

def log(message, level=:info)
  EsClient.logger.try!(level, message)
end

#reconnect!Object



53
54
55
# File 'lib/es_client/client.rb', line 53

def reconnect!
  @http = nil
end

#request(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/es_client/client.rb', line 27

def request(options)
  retry_times = 0
  begin
    raw_response = http.request(options)
    response = ::EsClient::Response.new(raw_response.body, raw_response.status, raw_response.headers)
    EsClient.logger.request(http, response, options) if EsClient.logger.try!(:debug?)
    response
  rescue Excon::Errors::SocketError => e
    if retry_times >= RETRY_TIMES
      EsClient.logger.exception(e, http, options) if EsClient.logger
      raise
    end
    retry_times += 1
    EsClient.logger.info "[#{retry_times}] Try reconnect to #{@host}"
    reconnect!
    retry
  rescue Excon::Errors::BadRequest => e
    EsClient.logger.exception(e, http, options.merge(response: e.response.body)) if EsClient.logger
    raise
  end
end