-
Notifications
You must be signed in to change notification settings - Fork 11
/
nameservers.go
78 lines (64 loc) · 1.71 KB
/
nameservers.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package dns
import (
"context"
cryptorand "crypto/rand"
"errors"
"io"
"math/big"
"net"
"sync/atomic"
)
// NameServers is a slice of DNS nameserver addresses.
type NameServers []net.Addr
// Random picks a random Addr from s every time.
func (s NameServers) Random(rand io.Reader) ProxyFunc {
addrsByNet := s.netAddrsMap()
maxByNet := make(map[string]*big.Int, len(addrsByNet))
for network, addrs := range addrsByNet {
maxByNet[network] = big.NewInt(int64(len(addrs)))
}
return func(_ context.Context, addr net.Addr) (net.Addr, error) {
network := addr.Network()
max, ok := maxByNet[network]
if !ok {
return nil, errors.New("no nameservers for network: " + network)
}
addrs, ok := addrsByNet[network]
if !ok {
panic("impossible")
}
idx, err := cryptorand.Int(rand, max)
if err != nil {
return nil, err
}
return addrs[idx.Uint64()], nil
}
}
// RoundRobin picks the next Addr of s by index of the last pick.
func (s NameServers) RoundRobin() ProxyFunc {
addrsByNet := s.netAddrsMap()
idxByNet := make(map[string]*uint32, len(s))
for network := range addrsByNet {
idxByNet[network] = new(uint32)
}
return func(_ context.Context, addr net.Addr) (net.Addr, error) {
network := addr.Network()
idx, ok := idxByNet[network]
if !ok {
return nil, errors.New("no nameservers for network: " + network)
}
addrs, ok := addrsByNet[network]
if !ok {
panic("impossible")
}
return addrs[int(atomic.AddUint32(idx, 1)-1)%len(addrs)], nil
}
}
func (s NameServers) netAddrsMap() map[string][]net.Addr {
addrsByNet := make(map[string][]net.Addr, len(s))
for _, addr := range s {
network := addr.Network()
addrsByNet[network] = append(addrsByNet[network], addr)
}
return addrsByNet
}