Class: ProxyLoadBalancer

Inherits:
Object
  • Object
show all
Defined in:
app/services/proxy_load_balancer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProxyLoadBalancer

Returns a new instance of ProxyLoadBalancer.



5
6
7
8
# File 'app/services/proxy_load_balancer.rb', line 5

def initialize
  @tasks   = {}
  @offline = []
end

Instance Attribute Details

#offlineObject (readonly)

Returns the value of attribute offline.



3
4
5
# File 'app/services/proxy_load_balancer.rb', line 3

def offline
  @offline
end

Instance Method Details

#next(proxies) ⇒ Object

Get the least loaded proxy from the given list of proxies



11
12
13
14
15
16
17
# File 'app/services/proxy_load_balancer.rb', line 11

def next(proxies)
  exclude = @tasks.keys + @offline
  @tasks.merge!(get_counts(proxies - exclude))
  next_proxy = @tasks.select { |proxy, _| proxies.include?(proxy) }.min_by { |_, job_count| job_count }.try(:first)
  @tasks[next_proxy] += 1 if next_proxy.present?
  next_proxy
end