forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroundrobin.go
More file actions
47 lines (40 loc) · 1.12 KB
/
Copy pathroundrobin.go
File metadata and controls
47 lines (40 loc) · 1.12 KB
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 (
"fmt"
"strings"
"sync"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
// RoundRobin is a group of recolvers that will receive equal amounts of queries.
// Failed queries are not retried.
type RoundRobin struct {
resolvers []Resolver
mu sync.Mutex
current int
}
var _ Resolver = &RoundRobin{}
// NewRoundRobin returns a new instance of a round-robin resolver group.
func NewRoundRobin(resolvers ...Resolver) *RoundRobin {
return &RoundRobin{resolvers: 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()
Log.WithFields(logrus.Fields{
"client": ci.SourceIP,
"qname": qName(q),
"resolver": resolver.String(),
}).Trace("forwarding query to resolver")
return resolver.Resolve(q, ci)
}
func (r *RoundRobin) String() string {
var s []string
for _, resolver := range r.resolvers {
s = append(s, resolver.String())
}
return fmt.Sprintf("RoundRobin(%s)", strings.Join(s, ";"))
}