Class: Pigeon::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pigeon/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, clbk = {}) ⇒ Client

Returns a new instance of Client.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pigeon/core.rb', line 51

def initialize name, opts = {}, clbk = {}
  @options   = DEFAULT_OPTIONS
  @callbacks = DEFAULT_CALLBACKS

  @options[:request_name] = name

  opts.each do |k, v|
    raise Error::Argument, "unknown option #{k}" unless VALID_OPTIONS.include?(k.to_s)
    @options[k.to_sym] = v
  end

  clbk.each do |k, v|
    raise Error::Argument, "unknown callback #{k}" unless VALID_CALLBACKS.include?(k.to_s)
    @callbacks[k.to_sym] = v
  end

  if @options[:retryable]
    Retryable.configure do |config|
      config.sleep = lambda { |n| 4**n }
    end
  end
end

Instance Attribute Details

#callbacks=(value) ⇒ Object (writeonly)

Sets the attribute callbacks

Parameters:

  • value

    the value to set the attribute callbacks to.



49
50
51
# File 'lib/pigeon/core.rb', line 49

def callbacks=(value)
  @callbacks = value
end

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



49
50
51
# File 'lib/pigeon/core.rb', line 49

def options=(value)
  @options = value
end

#request_name=(value) ⇒ Object (writeonly)

Sets the attribute request_name

Parameters:

  • value

    the value to set the attribute request_name to.



49
50
51
# File 'lib/pigeon/core.rb', line 49

def request_name=(value)
  @request_name = value
end

Instance Method Details

#circuitObject



120
121
122
# File 'lib/pigeon/core.rb', line 120

def circuit
  Circuitbox.circuit(@options[:request_name], exceptions: [HTTP::ConnectionError, HTTP::HeaderError, HTTP::RequestError, HTTP::ResponseError, HTTP::TimeoutError])
end

#config(key, value) ⇒ Object

Raises:

  • (Error::Argument)


74
75
76
77
# File 'lib/pigeon/core.rb', line 74

def config key, value
  raise Error::Argument, "unknown option #{key}" unless VALID_OPTIONS.include?(key.to_s)
  @options[key.to_sym] = value
end

#delete(url, args = {}) ⇒ Object



97
98
99
100
101
# File 'lib/pigeon/core.rb', line 97

def delete url, args = {}
  response = http(:delete, url, args)
rescue => e
  @callbacks[:HttpError]&.call(e)
end

#get(url, args = {}) ⇒ Object



79
80
81
82
83
# File 'lib/pigeon/core.rb', line 79

def get url, args = {}
  response = http(:get, url, args)
rescue => e
  @callbacks[:HttpError]&.call(e)
end

#http(method, url, args = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pigeon/core.rb', line 103

def http method, url, args = {}
  start = Time.now
  args.merge({
    read_timeout: @options[:request_timeout],
    open_timeout: @options[:request_open_timeout],
    ssl_verify:   @options[:ssl_verify]
  })
  response = Pigeon::Http::Request.new(method, url, args).execute

  uri = URI.parse(url)
  Pigeon::Statsd.new(@options[:request_name] + '_latency', tags: ["host:#{uri.host}"]).capture(action: :histogram, count: (Time.now - start))
  Pigeon::Statsd.new(@options[:request_name] + '_througput', tags: ["host:#{uri.host}"]).capture
  Pigeon::Statsd.new(@options[:request_name] + '_status', tags: ["host:#{uri.host}", "http:#{response.code}"]).capture

  response
end

#post(url, args = {}) ⇒ Object



85
86
87
88
89
# File 'lib/pigeon/core.rb', line 85

def post url, args = {}
  response = http(:post, url, args)
rescue => e
  @callbacks[:HttpError]&.call(e)
end

#put(url, args = {}) ⇒ Object



91
92
93
94
95
# File 'lib/pigeon/core.rb', line 91

def put url, args = {}
  response = http(:put, url, args)
rescue => e
  @callbacks[:HttpError]&.call(e)
end