ruby-proxifier icon indicating copy to clipboard operation
ruby-proxifier copied to clipboard

Proxy pool

Open troelskn opened this issue 10 years ago • 2 comments

Hi,

I needed a pool of proxies to cycle through and didn't see any direct support in this library. I've made a patch to supply this and thought others might benefit from it as well. If you'd like to include it in the library, I'll be happy to prepare a PR.

# An array of proxies to cycle through
class ProxyPool < Array

  def self.instance
    @instance ||= new
  end

  def initialize
    @lock = Mutex.new
    @pos = 0
  end

  def checkout
    if any?
      @lock.synchronize do
        next_item = self[@pos % length]
        @pos += 1
        next_item
      end
    end
  end

  module Hook
    def self.included(klass)
      klass.class_eval do
        def self.environment_proxy
          ProxyPool.instance.checkout || super
        end
      end
    end
  end

end # class ProxyPool

class TCPSocket
  include ProxyPool::Hook
end

Usage:

ProxyPool.instance << 'http://user:[email protected]'
ProxyPool.instance << 'http://user:[email protected]'
ProxyPool.instance << 'http://user:[email protected]'

troelskn avatar Dec 24 '14 11:12 troelskn

Very cool, I was looking for something just like this.

holoiii avatar Dec 29 '14 07:12 holoiii

How do you supply the array of proxies to cycle through?

dare05 avatar Jan 03 '15 14:01 dare05