Module: HTTP::Chainable

Defined in:
lib/http-proxy/chainable.rb

Instance Method Summary collapse

Instance Method Details

#proxy(lazy_mode: true, &block) ⇒ Object

Choose a proxy to send HTTP request

Parameters:

  • lazy_mode (Boolean) (defaults to: true)

    Re-use the last working proxy



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/http-proxy/chainable.rb', line 9

def proxy(lazy_mode: true, &block)
  p = if lazy_mode && !@_last_proxy.nil?
        @_last_proxy
      else
        block_given? ? ProxyPool.get(&block) : ProxyPool.get
      end

  begin
    ret = via(p['host'], p['port'])
    @_last_proxy = p
  rescue HTTP::ConnectionError, HTTP::TimeoutError => e
    # Remove it from pool
    ProxyPool.remove(p)

    # Select another one
    p = block_given? ? ProxyPool.get(&block) : ProxyPool.get
    while p.nil?
      ProxyPool.update
      p = block_given? ? ProxyPool.get(&block) : ProxyPool.get
    end

    retry
  end

  ret
end