Class: Songkick::Transport::Curb

Inherits:
Base
  • Object
show all
Defined in:
lib/songkick/transport/curb.rb

Constant Summary collapse

DEFAULT_HEADERS =

Send this so that curb will not implement Section 8.2.3 of RFC 2616, which our Ruby HTTP servers are not equipped to respond to. (If not sent it causes an additional 1s of latency for all requests with post data longer than 1k)

{"Expect" => ""}

Constants inherited from Base

Base::DEFAULT_INSTRUMENTATION_LABEL

Instance Attribute Summary

Attributes inherited from Base

#basic_auth, #host, #instrumenter, #timeout, #user_agent, #user_error_codes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#do_verb

Methods included from Base::API

#delete, #get, #head, #options, #patch, #post, #put, #with_basic_auth, #with_headers, #with_params, #with_timeout

Constructor Details

#initialize(host, options = {}) ⇒ Curb

Returns a new instance of Curb.



19
20
21
22
23
# File 'lib/songkick/transport/curb.rb', line 19

def initialize(host, options = {})
  super(host, options)
  @no_signal  = !!options[:no_signal]
  Thread.current[:transport_curb_easy] ||= options[:connection]
end

Class Method Details

.clear_thread_connectionObject



15
16
17
# File 'lib/songkick/transport/curb.rb', line 15

def self.clear_thread_connection
  Thread.current[:transport_curb_easy] = nil
end

Instance Method Details

#connectionObject



25
26
27
# File 'lib/songkick/transport/curb.rb', line 25

def connection
  Thread.current[:transport_curb_easy] ||= Curl::Easy.new
end

#execute_request(req) ⇒ Object



37
38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/songkick/transport/curb.rb', line 37

def execute_request(req)
  self.instrumentation_payload_extras = {} if self.instrumenter
  connection.reset

  connection.url     = req.url
  timeout            = req.timeout || @timeout
  connection.timeout = timeout
  connection.encoding = ''
  connection.headers.update(DEFAULT_HEADERS.merge(req.headers))
  connection.nosignal = true if @no_signal

  response_headers = {}

  connection.on_header do |header_line|
    line = header_line.sub(/\r\n$/, '')
    parts = line.split(/:\s*/)
    if parts.size >= 2
      header_name, value = parts.shift, parts * ':'
      if response_headers[header_name]
        response_headers[header_name] << ", #{value}"
      else
        response_headers[header_name] = value
      end
    end
    header_line.bytesize
  end

  if req.use_body?
    connection.__send__("http_#{req.verb}", req.body)
  else
    connection.http(req.verb.upcase)
  end

  if self.instrumenter
    self.instrumentation_payload_extras[:connect_time] = connection.connect_time
    self.instrumentation_payload_extras[:name_lookup_time] = connection.name_lookup_time
  end

  process(req, connection.response_code, response_headers, connection.body_str)

rescue Curl::Err::HostResolutionError => error
  logger.warn "Could not resolve host: #{@host}"
  raise Transport::HostResolutionError, req

rescue Curl::Err::ConnectionFailedError => error
  logger.warn "Could not connect to host: #{@host}"
  raise Transport::ConnectionFailedError, req

rescue Curl::Err::TimeoutError => error
  logger.warn "Request timed out after #{timeout}s : #{req}"
  raise Transport::TimeoutError, req

rescue Curl::Err::GotNothingError => error
  logger.warn "Got nothing: #{req}"
  raise Transport::UpstreamError, req

rescue Curl::Err::RecvError => error
  logger.warn "Failure receiving network data: #{error.message} : #{req}"
  raise Transport::UpstreamError, req

end

#instrumentation_payload_extrasObject



29
30
31
# File 'lib/songkick/transport/curb.rb', line 29

def instrumentation_payload_extras
  Thread.current[:transport_curb_payload_extras] ||= {}
end

#instrumentation_payload_extras=(extras) ⇒ Object



33
34
35
# File 'lib/songkick/transport/curb.rb', line 33

def instrumentation_payload_extras=(extras)
  Thread.current[:transport_curb_payload_extras] = {}
end