Add backend weight round robin select (#34)
[dns-over-https.git] / doh-client / selector / randomSelector.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
package selector

import (
	"errors"
	"math/rand"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

type RandomSelector struct {
	upstreams []*Upstream
}

func NewRandomSelector() *RandomSelector {
	return new(RandomSelector)
}

func (rs *RandomSelector) 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 *RandomSelector) Get() *Upstream {
	return rs.upstreams[rand.Intn(len(rs.upstreams)-1)]
}

func (rs *RandomSelector) StartEvaluate() {}

func (rs *RandomSelector) ReportUpstreamStatus(upstream *Upstream, upstreamStatus upstreamStatus) {}