mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 02:03:13 -05:00
Add traffic plugin
This allows for advanced loadbalancing and maybe geoIP loadbalancing. Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
39
plugin/traffic/assignment.go
Normal file
39
plugin/traffic/assignment.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user