-
Notifications
You must be signed in to change notification settings - Fork 62
/
roundrobin.go
47 lines (40 loc) · 1.13 KB
/
roundrobin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package rdns
import (
"sync"
"github.com/miekg/dns"
)
// RoundRobin is a group of resolvers that will receive equal amounts of queries.
// Failed queries are not retried.
type RoundRobin struct {
id string
resolvers []Resolver
mu sync.Mutex
current int
metrics *RouterMetrics
}
var _ Resolver = &RoundRobin{}
// NewRoundRobin returns a new instance of a round-robin resolver group.
func NewRoundRobin(id string, resolvers ...Resolver) *RoundRobin {
return &RoundRobin{
id: id,
resolvers: resolvers,
metrics: NewRouterMetrics(id, len(resolvers)),
}
}
// Resolve a DNS query using a round-robin resolver group.
func (r *RoundRobin) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
r.mu.Lock()
resolver := r.resolvers[r.current]
r.current = (r.current + 1) % len(r.resolvers)
r.mu.Unlock()
logger(r.id, q, ci).WithField("resolver", resolver).Debug("forwarding query to resolver")
r.metrics.route.Add(resolver.String(), 1)
msg, err := resolver.Resolve(q, ci)
if err != nil {
r.metrics.failure.Add(resolver.String(), 1)
}
return msg, err
}
func (r *RoundRobin) String() string {
return r.id
}