Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy pool #6

Open
troelskn opened this issue Dec 24, 2014 · 2 comments
Open

Proxy pool #6

troelskn opened this issue Dec 24, 2014 · 2 comments

Comments

@troelskn
Copy link

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]'
@holoiii
Copy link

holoiii commented Dec 29, 2014

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

@dare05
Copy link

dare05 commented Jan 3, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants