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
- provider – any instance of
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: - provider –
ListProvider
instance - random_function – function which returns random number for the given range. By default:
random.randint
- provider –
-
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: provider – IProvider
instance
Providers¶
Providers exist to give lists of hosts
Base class¶
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)