Class: Proxypool::Pool
- Inherits:
-
Object
- Object
- Proxypool::Pool
- Defined in:
- lib/proxypool/pool.rb
Constant Summary collapse
- LOAD_FACTOR =
0.8
- REFRESH_INTERVAL =
seconds
600- USAGE_THRESHOLD =
seconds
60
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(proxies: [], load_factor: LOAD_FACTOR, refresh_interval: REFRESH_INTERVAL, usage_threshold: USAGE_THRESHOLD) ⇒ Pool
constructor
A new instance of Pool.
- #invalid_count ⇒ Object
- #mark_invalid!(proxy) ⇒ Object
- #next_proxy ⇒ Object
- #proxies ⇒ Object
- #refresh_proxies! ⇒ Object
- #size ⇒ Object
- #stats ⇒ Object
- #valid_count ⇒ Object
Constructor Details
#initialize(proxies: [], load_factor: LOAD_FACTOR, refresh_interval: REFRESH_INTERVAL, usage_threshold: USAGE_THRESHOLD) ⇒ Pool
Returns a new instance of Pool.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/proxypool/pool.rb', line 9 def initialize(proxies: [], load_factor: LOAD_FACTOR, refresh_interval: REFRESH_INTERVAL, usage_threshold: USAGE_THRESHOLD) (load_factor, refresh_interval, usage_threshold) @usage_threshold = usage_threshold @load_factor = load_factor @refresh_interval = refresh_interval @initial_proxies = proxies @last_updated_at = Time.now # Initialize proxies immediately proxy_data = fetch_proxies(proxies) @proxies = assign_proxies!(proxy_data) end |
Instance Method Details
#empty? ⇒ Boolean
83 84 85 |
# File 'lib/proxypool/pool.rb', line 83 def empty? size == 0 end |
#invalid_count ⇒ Object
79 80 81 |
# File 'lib/proxypool/pool.rb', line 79 def invalid_count size - valid_count end |
#mark_invalid!(proxy) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/proxypool/pool.rb', line 96 def mark_invalid!(proxy) proxy_key = case proxy when String then proxy when Proxypool::Proxy then "#{proxy.protocol}://#{proxy.ip}:#{proxy.port}" else proxy.to_s end if @proxies&.key?(proxy_key) @proxies[proxy_key] = @proxies[proxy_key].with(valid: false, last_used_at: Time.now) true else raise Proxypool::Error, "Proxy #{proxy_key} not found in the pool" end end |
#next_proxy ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/proxypool/pool.rb', line 23 def next_proxy refresh_if_needed! return nil if proxies.empty? # Find all available proxies (valid and not recently used) available_proxies = proxies.select do |key, proxy| proxy.valid && (proxy.last_used_at.nil? || Time.now - proxy.last_used_at > @usage_threshold) end # If no available proxies, return the least recently used valid proxy if available_proxies.empty? valid_proxies = proxies.select { |_, proxy| proxy.valid } return nil if valid_proxies.empty? proxy_key = valid_proxies.min_by { |_, proxy| proxy.last_used_at || Time.at(0) }.first else proxy_key = available_proxies.keys.sample end proxy = proxies[proxy_key] # Update proxy usage updated_proxy = Proxypool::Proxy.new( protocol: proxy.protocol, ip: proxy.ip, port: proxy.port, valid: proxy.valid, username: proxy.username, password: proxy.password, last_used_at: Time.now, usage_count: proxy.usage_count + 1 ) proxies[proxy_key] = updated_proxy updated_proxy end |
#proxies ⇒ Object
61 62 63 |
# File 'lib/proxypool/pool.rb', line 61 def proxies @proxies ||= {} end |
#refresh_proxies! ⇒ Object
65 66 67 68 69 |
# File 'lib/proxypool/pool.rb', line 65 def refresh_proxies! new_proxies = fetch_proxies(@initial_proxies) @proxies = assign_proxies!(new_proxies) @last_updated_at = Time.now end |
#size ⇒ Object
71 72 73 |
# File 'lib/proxypool/pool.rb', line 71 def size @proxies&.size || 0 end |
#stats ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/proxypool/pool.rb', line 87 def stats { total: size, valid: valid_count, invalid: invalid_count, last_updated: @last_updated_at } end |
#valid_count ⇒ Object
75 76 77 |
# File 'lib/proxypool/pool.rb', line 75 def valid_count @proxies&.count { |_, proxy| proxy.valid } || 0 end |