load-balancing-1.0.1.1: Client-side load balancing utilities.

Safe HaskellNone
LanguageHaskell2010

Control.Concurrent.LoadDistribution

Description

This module provides utilities for client-side load balancing.

Synopsis

Documentation

evenlyDistributed Source #

Arguments

:: Ord resource 
=> IO (Set resource)

An IO action that can be used to obtain a tagged list of resources. This is an IO action because the set of resources is allowed to change dynamically.

-> IO (LoadBalanced resource) 

A load balancing technique that evenly load across multiple resources, where "even" is figured based on the current number of simultaneous threads using the resource.

Some notes on implentation: The priority portion of the priority queue is a tuple of the form (p :: Int, r :: Int). The first element, p, stands for "processes", and indicates the number of simultaneous threads that are using the specific resource. This value is incremented and decremented as threads acquire and release the resource. The second element, r, stands for "round robin". Its purpose is to act as a tie breaker between resources that have the same p value by giving priority to whichever resource has been chosen fewer times. It is always incremented and never decremented. Without the r component, the deterministic nature of the priority queue will heavily favor resources that come first in the sorted list of resources.

data LoadBalanced resource Source #

An object that keeps track of load balancing across threads.

withResource :: (Show resource, Ord resource) => LoadBalanced resource -> (Maybe resource -> IO a) -> IO a Source #

Execute some kind of IO Action with the least loaded resource. If, for whatever reason, there are no available resources, then Nothing is passed to the user-provided action function.

map :: Ord b => (a -> b) -> LoadBalanced a -> IO (LoadBalanced b) Source #

Derives a new load balancer from an existing load balancer, allowing the resource type to be mapped from one type to another. This requires IO (as opposed to implementing this using instance Functor) because we have to create a new shared load balancing state.