Sha256: 59e36e9f8ab636587cd6b84f4cfc17fd590e6d6f3e5d0022b98049e90dc957e6

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  # This class validates list of proxies.
  # Each proxy is validated using <code>ProxyFetcher::ProxyValidator</code>.
  class ProxyListValidator
    # @!attribute [r] proxies
    #   @return [Array<ProxyFetcher::Proxy>] Source array of proxies
    attr_reader :proxies
    # @!attribute [r] valid_proxies
    #   @return [Array<ProxyFetcher::Proxy>] Array of valid proxies after validation
    attr_reader :valid_proxies

    # @param [Array<ProxyFetcher::Proxy>] *proxies
    #   Any number of <code>ProxyFetcher::Proxy</code> to validate
    def initialize(*proxies)
      @proxies = proxies.flatten
    end

    # Performs validation
    #
    # @return [Array<ProxyFetcher::Proxy>]
    #   list of valid proxies
    def validate
      target_proxies = @proxies.dup
      target_proxies_lock = Mutex.new
      connectable_proxies = []
      connectable_proxies_lock = Mutex.new
      threads = []

      ProxyFetcher.config.pool_size.times do
        threads << Thread.new do
          loop do
            proxy = target_proxies_lock.synchronize { target_proxies.shift }
            break unless proxy

            connectable_proxies_lock.synchronize { connectable_proxies << proxy } if proxy.connectable?
          end
        end
      end

      threads.each(&:join)

      @valid_proxies = connectable_proxies
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
proxy_fetcher-0.10.2 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.10.1 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.10.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.9.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.8.0 lib/proxy_fetcher/utils/proxy_list_validator.rb