Load Balancers

There are two load balancing algorithms available for now:

  • random
  • round robin

Base class

class ahoyhoy.lb.iloadbalancer.ILoadBalancer(provider, session=None)[source]

Base class for load balancers.

Parameters:
  • provider – any instance of IProvider
  • session – custom session
get_or_create_endpoint(host)[source]

Concrete method to create an Endpoint from a Host object

Parameters:hostHost(‘host’, ‘port’) namedtuple
pick()[source]

Will only return nodes which have OpenState.

update()[source]

Update the hosts list with new hosts (if there are some).

Load blancers algorithms

class ahoyhoy.lb.RandomLB(provider, session=None, random_function=<bound method Random.randint of <random.Random object at 0x1c47c38>>)[source]

Bases: ahoyhoy.lb.iloadbalancer.ILoadBalancer

Implements random algorithm for chosing a host from the list.

>>> from ahoyhoy.utils import Host
>>> from ahoyhoy.lb.providers import ListProvider
>>> from ahoyhoy.lb import RandomLB
>>> rrlb = RandomLB(ListProvider(Host('google1.com1', '80'), Host('google.com', '80')))
>>> rrlb.pick()
<Endpoint/.../Host(address='google...', port='80')/<class 'ahoyhoy.circuit.circuit.ClosedState'>

Custom random function:

>>> def my_random(*args):
...    return 0
>>> rrlb = RandomLB(
...     ListProvider(Host('google1.com1', '80'), Host('google.com', '80')),
...     random_function=my_random)
>>> rrlb.pick()
<Endpoint/.../Host(address='google1.com1', port='80')/<class 'ahoyhoy.circuit.circuit.ClosedState'>
Parameters:
  • providerListProvider instance
  • random_function – function which returns random number for the given range. By default: random.randint
class ahoyhoy.lb.RoundRobinLB(provider, session=None)[source]

Bases: ahoyhoy.lb.iloadbalancer.ILoadBalancer

Implement round robin load balancing algorythm.

>>> from ahoyhoy.utils import Host
>>> from ahoyhoy.lb.providers import ListProvider
>>> from ahoyhoy.lb import RoundRobinLB
>>> rrlb = RoundRobinLB(ListProvider(Host('google1.com1', '80'), Host('google.com', '80')))
>>> rrlb.pick()
<Endpoint/.../Host(address='google1.com1', port='80')/<class 'ahoyhoy.circuit.circuit.ClosedState'>
>>> rrlb.pick()
<Endpoint/.../Host(address='google.com', port='80')/<class 'ahoyhoy.circuit.circuit.ClosedState'>
>>> rrlb.pick()
<Endpoint/.../Host(address='google1.com1', port='80')/<class 'ahoyhoy.circuit.circuit.ClosedState'>
Parameters:providerIProvider instance

Load blancers exceptions

class ahoyhoy.lb.exceptions.NoAvailableEndpointsLbException[source]

No Endpoints in the closed state.

Providers

Providers exist to give lists of hosts

Base class

class ahoyhoy.lb.providers.iprovider.IProvider[source]

A provider provides endpoints for a load balancer.

get_list()[source]

Return a new list of hosts.

List Provider

class ahoyhoy.lb.providers.ListProvider(*args)[source]

Bases: ahoyhoy.lb.providers.iprovider.IProvider

A simple list verison of the IProvider interface

Accepts a number of items to store as a list

>>> from ahoyhoy.utils import Host
>>> from ahoyhoy.lb.providers import ListProvider
>>> lp = ListProvider(Host('google1.com1', '80'), Host('google.com', '80'))
>>> lp.get_list()
(Host(address='google1.com1', port='80'), Host(address='google.com', port='80'))
Parameters:args – an iterable of items (hosts)