Class: LeelaClient::HTTPTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/leela_client/transport.rb

Instance Method Summary collapse

Constructor Details

#initialize(ring, opts) ⇒ HTTPTransport

Returns a new instance of HTTPTransport.



42
43
44
45
# File 'lib/leela_client/transport.rb', line 42

def initialize(ring, opts)
  @ring = ring
  @opts = opts
end

Instance Method Details

#send(metrics) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/leela_client/transport.rb', line 47

def send(metrics)
  sent  = 0
  proto = @opts[:ssl] ? "https" : "http"
  port  = @opts[:port] || (@opts[:ssl] ? 443 : 80)
  LeelaClient::LoadBalancer.group(@ring, metrics).each do |addr, ms|
    ms.each do |m|
      uri = URI("#{proto}://#{addr[1]}:#{port}/v1/" + ::CGI::escape(m.key))
      res = ::Net::HTTP.start(addr[1],
                              uri.port,
                              :use_ssl      => uri.scheme == "https",
                              :open_timeout => @opts[:timeout] || 5,
                              :read_timeout => @opts[:timeout] || 5,
                              :ssl_timeout  => @opts[:timeout] || 5) do |http|
        req              = ::Net::HTTP::Post.new(uri.path)
        req.body         = ::JSON::dump(m.serialize_json)
        req.content_type = "application/json"
        http.request(req)
      end
      if (res.code.to_i == 201)
        sent += 1
      end
    end
  end
  return(sent)
end