Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: draft for hash selector #68

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion doh-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,25 @@ func NewClient(conf *config.Config) (c *Client, err error) {
}

c.selector = s

case config.Hash:
if c.conf.Other.Verbose {
log.Println(config.Random, "mode start")
}
s := selector.NewHashSelector()
for _, u := range c.conf.Upstream.UpstreamGoogle {
if err := s.Add(u.URL, selector.Google); err != nil {
return nil, err
}
}

for _, u := range c.conf.Upstream.UpstreamIETF {
if err := s.Add(u.URL, selector.IETF); err != nil {
return nil, err
}
}

c.selector = s

default:
if c.conf.Other.Verbose {
log.Println(config.Random, "mode start")
Expand Down
1 change: 1 addition & 0 deletions doh-client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
Random = "random"
NginxWRR = "weighted_round_robin"
LVSWRR = "lvs_weighted_round_robin"
Hash = "hostname_hash"
)

type upstreamDetail struct {
Expand Down
54 changes: 54 additions & 0 deletions doh-client/selector/hashSelector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) {}