-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathhashSelector.go
More file actions
54 lines (43 loc) · 1.15 KB
/
hashSelector.go
File metadata and controls
54 lines (43 loc) · 1.15 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
48
49
50
51
52
53
54
package selector
import (
"errors"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type HashSelector struct {
upstreams []*Upstream
}
func NewHashSelector() *HashSelector {
return new(HashSelector)
}
func (rs *HashSelector) Add(url string, upstreamType UpstreamType) (err error) {
switch upstreamType {
case Google:
rs.upstreams = append(rs.upstreams, &Upstream{
Type: Google,
URL: url,
RequestType: "application/dns-json",
})
case IETF:
rs.upstreams = append(rs.upstreams, &Upstream{
Type: IETF,
URL: url,
RequestType: "application/dns-message",
})
default:
return errors.New("unknown upstream type")
}
return nil
}
func (rs *HashSelector) Get() *Upstream {
// here, if we have the name to be resolved (a string)
// we could compute the modulo over the size of upstream servers
// something like url.hash()%len(rs.upstreams)
// how to refactor Get() to get the name
return rs.upstreams[url.hash()%len(rs.upstreams)]
}
func (rs *HashSelector) StartEvaluate() {}
func (rs *HashSelector) ReportUpstreamStatus(upstream *Upstream, upstreamStatus upstreamStatus) {}