module HTTP::Chainable

Public Instance Methods

proxy(lazy_mode: true, &block) click to toggle source

Choose a proxy to send HTTP request

@param lazy_mode [Boolean] Re-use the last working proxy

# 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