Class: Yup::RequestForwarder

Inherits:
Object
  • Object
show all
Defined in:
lib/yup/request_forwarder.rb

Instance Method Summary collapse

Constructor Details

#initialize(http_method, request_url, headers, body, forward_to, timeout) ⇒ RequestForwarder

Returns a new instance of RequestForwarder.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yup/request_forwarder.rb', line 5

def initialize(http_method, request_url, headers, body, forward_to, timeout)
  @http_method = http_method
  @request_url = request_url
  @headers     = headers
  @body        = body
  @forward_to  = forward_to
  @timeout     = timeout
  @logger      = Yup.logger.clone

  @headers.merge!(
    'Host'       => @forward_to,
    'Connection' => 'Close')
end

Instance Method Details

#performObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yup/request_forwarder.rb', line 19

def perform
  http_method = @http_method.to_sym
  http_url    = "http://#{@forward_to}#{@request_url}"
  http = EventMachine::HttpRequest.
    new(http_url,
        :inactivity_timeout => @timeout).
    send(http_method,
         :head => @headers,
         :body => @body)

  @logger.progname = "Yup::RequestForwarder (##{self.__id__.to_s(36)} received at #{Time.now.to_s})"

  http.callback do
    Yup.watermark += 1

    if http.response_header && http.response_header.status && http.response_header.status / 100 == 2
      log_response(http)
      @logger.info "Success"
    else
      log_response(http)
      if Yup.retry_unless_2xx
        @logger.info "Fail: got status code #{http.response_header.status}; will retry after #{Yup.resend_delay} seconds"
        EventMachine.add_timer(Yup.resend_delay, &self.method(:retry))
      else
        @logger.info "Fail; will not retry"
      end
    end
  end

  http.errback do
    log_response(http)
    @logger.info "Error: #{http.inspect}: #{http.error}; will retry after #{Yup.resend_delay} seconds"

    EventMachine.add_timer(Yup.resend_delay, &self.method(:retry))
  end
end

#retryObject



56
57
58
# File 'lib/yup/request_forwarder.rb', line 56

def retry
  self.perform
end