Documentation
¶
Overview ¶
Package scheduling provides a variety of scheduling and rate limiting algorithms.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
Backend represents some abstract backend that has connections and optionsllay a weight. The Value gives the actual backend value used by the application. Backend implements the io.Closer interface so callers should use defer backend.Close()
func NewBackend ¶
func NewBackend(value interface{}) *Backend
NewBackend returns a new backend using the given value and 100% weighting.
func NewWeightedBackend ¶
NewWeightedBackend returns a new backend using the given value and weithing.
type LeastConnectionsScheduler ¶
type LeastConnectionsScheduler struct {
// contains filtered or unexported fields
}
LeastConnectionsScheduler is a scheduler that uses connections to determine the next backend to use. It does not take into account any weighting applied to the backends.
func (*LeastConnectionsScheduler) GetBackends ¶
func (s *LeastConnectionsScheduler) GetBackends() []*Backend
Returns the backends in use
func (*LeastConnectionsScheduler) Schedule ¶
func (s *LeastConnectionsScheduler) Schedule() *Backend
Schedule returns the next backend according to the algorithm.
type RateLimiter ¶
RateLimiter provides the capability of limiting some activity using a given rate.
This could be a requests per second to a backend web service or a number of emails per day.
func NewRateLimiter ¶
func NewRateLimiter(limit uint64, d time.Duration) *RateLimiter
NewRateLimiter returns a new RateLimiter using the given rate (limit/duration)
func (*RateLimiter) Close ¶
func (r *RateLimiter) Close() error
Close closes the rate limiter and cleans up
func (*RateLimiter) Schedule ¶
func (r *RateLimiter) Schedule() bool
Schedule returns true if the given activity may be scheduled according to the specified rate limits.
type RoundRobinScheduler ¶
type RoundRobinScheduler struct {
// contains filtered or unexported fields
}
RoundRobinScheduler is a scheduler that simply loops through the backends returning the next one, without regard to number of connections or weighting.
func (*RoundRobinScheduler) GetBackends ¶
func (s *RoundRobinScheduler) GetBackends() []*Backend
Returns the backends in use
func (*RoundRobinScheduler) Schedule ¶
func (s *RoundRobinScheduler) Schedule() *Backend
Schedule returns the next backend according to the algorithm.
type Scheduler ¶
type Scheduler interface { // Schedule returns the next backend according to the algorithm. Schedule() *Backend // Returns the backends in use GetBackends() []*Backend }
Scheduler is an interface implemented by all schedulers.
func NewLeastConnectionsScheduler ¶
NewLeastConnectionsScheduler returns a new LeastConnectionsScheduler with the given backends.
func NewRoundRobinScheduler ¶
NewRoundRobinScheduler return a new RoundRobinScheduler
func NewWeightedLeastConnectionsScheduler ¶
NewWeightedLeastConnectionsScheduler returns a new WeightedLeastConnectionsScheduler
type WeightedLeastConnectionsScheduler ¶
type WeightedLeastConnectionsScheduler struct {
// contains filtered or unexported fields
}
WeightedLeastConnectionsScheduler is a Scheduler that uses a weighted least connections algorithm to schedule backends.
Supposing there is a server set S = {S0, S1, ..., Sn-1}, W(Si) is the weight of server Si; C(Si) is the current connection number of server Si; CSUM = ΣC(Si) (i=0, 1, .. , n-1) is the sum of current connection numbers;
The new connection is assigned to the server j, in which
(C(Sm) / CSUM)/ W(Sm) = min { (C(Si) / CSUM) / W(Si)} (i=0, 1, . , n-1), where W(Si) isn't zero
Since the CSUM is a constant in this lookup, there is no need to divide by CSUM, the condition can be optimized as
C(Sm) / W(Sm) = min { C(Si) / W(Si)} (i=0, 1, . , n-1), where W(Si) isn't zero
func (*WeightedLeastConnectionsScheduler) GetBackends ¶
func (s *WeightedLeastConnectionsScheduler) GetBackends() []*Backend
Returns the backends in use
func (*WeightedLeastConnectionsScheduler) Schedule ¶
func (s *WeightedLeastConnectionsScheduler) Schedule() *Backend
Schedule returns the next backend according to the algorithm.