Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Copy Markdown
Collaborator

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
3 changes: 2 additions & 1 deletion proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ func (p *Proxy) validateConfig() (err error) {
"",
UpstreamModeFastestAddr,
UpstreamModeLoadBalance,
UpstreamModeParallel:
UpstreamModeParallel,
UpstreamModeRandom:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PR adds UpstreamModeRandom to the switch statement, but this constant isn't defined anywhere in the visible code. You need to define this constant alongside the other UpstreamMode constants (like UpstreamModeFastestAddr, UpstreamModeLoadBalance, etc.).

@dontcrash dontcrash Jul 25, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This one I believe is fine as it is inherited from AdGuardHome?
Although I realised the switch case is wrong, fixed it up

UpstreamModeFastestAddr,
UpstreamModeLoadBalance,
UpstreamModeRandom,
UpstreamModeParallel:
// Go on.

// Go on.
default:
return fmt.Errorf("upstream mode: %w: %q", errors.ErrBadEnumValue, p.UpstreamMode)
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))]}
}
Comment thread
dontcrash marked this conversation as resolved.
Outdated
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
Copy Markdown
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,
UpstreamModeRandomm,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a typo in the variable name: UpstreamModeRandomm should be UpstreamModeRandom.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Facepalm.

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