Skip to content
Open
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
2 changes: 1 addition & 1 deletion internal/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var commandLineOptions = []*commandLineOption{
valueType: "address",
},
upstreamModeIdx: {
description: "Defines the upstreams logic mode, possible values: load_balance, parallel, " +
description: "Defines the upstreams logic mode, possible values: load_balance, parallel, random, " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update README.md as well.

"fastest_addr (default: load_balance).",
long: "upstream-mode",
short: "",
Expand Down
1 change: 1 addition & 0 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (p *Proxy) validateConfig() (err error) {
"",
UpstreamModeFastestAddr,
UpstreamModeLoadBalance,
UpstreamModeRandom,
UpstreamModeParallel:
// Go on.
default:
Expand Down
6 changes: 6 additions & 0 deletions proxy/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func (p *Proxy) exchangeUpstreams(
switch p.UpstreamMode {
case UpstreamModeParallel:
return upstream.ExchangeParallel(ups, req)
case UpstreamModeRandom:
// Simply set ups to a random entry in ups
// Falls back to default behaviour as if ups <= 1 it will run UpstreamModeLoadBalance as intended
if len(ups) > 1 {
ups = []upstream.Upstream{ups[p.randSrc.Intn(len(ups))]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't build:

proxy/exchange.go:28:44: p.randSrc.Intn undefined (type "math/rand/v2".Source has no field or method Intn)

See go doc math/rand/v2.

}
case UpstreamModeFastestAddr:
switch req.Question[0].Qtype {
case dns.TypeA, dns.TypeAAAA:
Expand Down
5 changes: 5 additions & 0 deletions proxy/upstreammode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
// or AAAA requests only with the fastest IP address detected by ICMP
// response time or TCP connection time.
UpstreamModeFastestAddr UpstreamMode = "fastest_addr"

// UpstreamModeRandom makes server to a random upstream
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, consider using gofumpt to format the code in this repository:

  1. make go-tools
  2. ./bin/gofumpt --extra -w .

UpstreamModeRandom UpstreamMode = "random"
)

// type check
Expand All @@ -35,6 +38,7 @@ func (m *UpstreamMode) UnmarshalText(b []byte) (err error) {
case
UpstreamModeLoadBalance,
UpstreamModeParallel,
UpstreamModeRandom,
UpstreamModeFastestAddr:
*m = um
default:
Expand All @@ -43,6 +47,7 @@ func (m *UpstreamMode) UnmarshalText(b []byte) (err error) {
b,
UpstreamModeLoadBalance,
UpstreamModeParallel,
UpstreamModeRandom,
UpstreamModeFastestAddr,
)
}
Expand Down