Files
coredns/plugin/traffic/assignment.go
Miek Gieben d5c5ba010c Add traffic plugin
This allows for advanced loadbalancing and maybe geoIP loadbalancing.

Signed-off-by: Miek Gieben <miek@miek.nl>
2020-01-09 17:07:22 +01:00

40 lines
662 B
Go

package traffic
import (
"math/rand"
"net"
)
// assignment is an assignment for a single service. It contains multiple backends.
type assignment struct {
service string
backends []*backend
}
// backend is a backend specified by an address, port and a weight.
type backend struct {
addr net.IP
port int
weight int
}
// Select selects a backend from a, using weighted random selection
func (a assignment) Select() *backend {
total := 0
for _, b := range a.backends {
total += b.weight
}
if total == 0 {
return nil
}
r := rand.Intn(total) + 1
for _, b := range a.backends {
r -= b.weight
if r <= 0 {
return b
}
}
return nil
}