mirror of
https://github.com/bolucat/Archive.git
synced 2025-10-13 11:54:02 +08:00
51 lines
978 B
Go
51 lines
978 B
Go
package selector
|
|
|
|
import (
|
|
"errors"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
rand.NewSource(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))]
|
|
}
|
|
|
|
func (rs *RandomSelector) StartEvaluate() {}
|
|
|
|
func (rs *RandomSelector) ReportUpstreamStatus(upstream *Upstream, upstreamStatus upstreamStatus) {}
|