2016-06-05 22:51:56 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2016-06-18 15:41:18 -05:00
|
|
|
"math"
|
2016-06-05 22:51:56 -05:00
|
|
|
"math/rand"
|
2016-06-14 16:43:06 -05:00
|
|
|
"sync"
|
2016-06-05 22:51:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// HostPool is a collection of UpstreamHosts.
|
|
|
|
type HostPool []*UpstreamHost
|
|
|
|
|
|
|
|
// Policy decides how a host will be selected from a pool.
|
|
|
|
type Policy interface {
|
|
|
|
Select(pool HostPool) *UpstreamHost
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterPolicy("random", func() Policy { return &Random{} })
|
|
|
|
RegisterPolicy("least_conn", func() Policy { return &LeastConn{} })
|
|
|
|
RegisterPolicy("round_robin", func() Policy { return &RoundRobin{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random is a policy that selects up hosts from a pool at random.
|
|
|
|
type Random struct{}
|
|
|
|
|
|
|
|
// Select selects an up host at random from the specified pool.
|
|
|
|
func (r *Random) Select(pool HostPool) *UpstreamHost {
|
2016-06-18 15:41:18 -05:00
|
|
|
|
|
|
|
// Because the number of available hosts isn't known
|
|
|
|
// up front, the host is selected via reservoir sampling
|
|
|
|
// https://en.wikipedia.org/wiki/Reservoir_sampling
|
2016-06-05 22:51:56 -05:00
|
|
|
var randHost *UpstreamHost
|
|
|
|
count := 0
|
|
|
|
for _, host := range pool {
|
|
|
|
if !host.Available() {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-18 15:41:18 -05:00
|
|
|
|
|
|
|
// (n % 1 == 0) holds for all n, therefore randHost
|
|
|
|
// will always get assigned a value if there is
|
|
|
|
// at least 1 available host
|
2016-06-05 22:51:56 -05:00
|
|
|
count++
|
2016-06-18 15:41:18 -05:00
|
|
|
if (rand.Int() % count) == 0 {
|
2016-06-05 22:51:56 -05:00
|
|
|
randHost = host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return randHost
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeastConn is a policy that selects the host with the least connections.
|
|
|
|
type LeastConn struct{}
|
|
|
|
|
|
|
|
// Select selects the up host with the least number of connections in the
|
|
|
|
// pool. If more than one host has the same least number of connections,
|
|
|
|
// one of the hosts is chosen at random.
|
|
|
|
func (r *LeastConn) Select(pool HostPool) *UpstreamHost {
|
|
|
|
var bestHost *UpstreamHost
|
|
|
|
count := 0
|
2016-06-18 15:41:18 -05:00
|
|
|
leastConn := int64(math.MaxInt64)
|
2016-06-05 22:51:56 -05:00
|
|
|
for _, host := range pool {
|
|
|
|
if !host.Available() {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-18 15:41:18 -05:00
|
|
|
|
|
|
|
if host.Conns < leastConn {
|
|
|
|
leastConn = host.Conns
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Among hosts with same least connections, perform a reservoir
|
|
|
|
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
|
|
|
|
if host.Conns == leastConn {
|
2016-06-05 22:51:56 -05:00
|
|
|
count++
|
2016-06-18 15:41:18 -05:00
|
|
|
if (rand.Int() % count) == 0 {
|
2016-06-05 22:51:56 -05:00
|
|
|
bestHost = host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestHost
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundRobin is a policy that selects hosts based on round robin ordering.
|
|
|
|
type RoundRobin struct {
|
2016-06-14 16:43:06 -05:00
|
|
|
robin uint32
|
|
|
|
mutex sync.Mutex
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select selects an up host from the pool using a round robin ordering scheme.
|
|
|
|
func (r *RoundRobin) Select(pool HostPool) *UpstreamHost {
|
|
|
|
poolLen := uint32(len(pool))
|
2016-06-14 16:43:06 -05:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
// Return next available host
|
|
|
|
for i := uint32(0); i < poolLen; i++ {
|
|
|
|
r.robin++
|
|
|
|
host := pool[r.robin%poolLen]
|
|
|
|
if host.Available() {
|
|
|
|
return host
|
|
|
|
}
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
2016-06-14 16:43:06 -05:00
|
|
|
return nil
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|