Skip to content

Commit

Permalink
proxy: return algo
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Dec 18, 2023
1 parent 2ad8274 commit df5863b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions proxy/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ func exchange(u upstream.Upstream, req *dns.Msg, c Clock) (resp *dns.Msg, dur ti

// upstreamRTTStats is the statistics for a single upstream's round-trip time.
type upstreamRTTStats struct {
// avgRTT is the current average round-trip time in seconds. The float64 is
// the returning type of [time.Duration.Seconds] method and is used to avoid
// unnecessary divisions.
avgRTT float64

// reqNum is the number of requests to the upstream.
reqNum uint64
// rttSum is the sum of all the round-trip times in microseconds. The
// float64 type is used since it's capable of representing about 285 years
// in microseconds.
rttSum float64

// reqNum is the number of requests to the upstream. The float64 type is
// used since to avoid unnecessary type conversions.
reqNum float64
}

// update returns updated stats after adding given RTT.
func (stats upstreamRTTStats) update(rtt time.Duration) (updated upstreamRTTStats) {
return upstreamRTTStats{
// See https://en.wikipedia.org/wiki/Moving_average#Cumulative_average.
avgRTT: (rtt.Seconds() + stats.avgRTT*float64(stats.reqNum)) / float64(stats.reqNum+1),
rttSum: stats.rttSum + float64(rtt.Microseconds()),
reqNum: stats.reqNum + 1,
}
}
Expand All @@ -125,11 +125,11 @@ func (p *Proxy) calcWeights(ups []upstream.Upstream) (weights []float64) {

for _, u := range ups {
stat := p.upstreamRTTStats[u.Address()]
if stat.avgRTT == 0 {
if stat.rttSum == 0 || stat.reqNum == 0 {
// Use 1 as the default weight.
weights = append(weights, 1)
} else {
weights = append(weights, 1/stat.avgRTT)
weights = append(weights, 1/(stat.rttSum/stat.reqNum))
}
}

Expand Down
22 changes: 11 additions & 11 deletions proxy/exchange_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
servers []upstream.Upstream
}{{
wantStat: map[upstream.Upstream]int64{
fastUps: 8906,
slowerUps: 920,
slowestUps: 174,
fastUps: 8917,
slowerUps: 911,
slowestUps: 172,
},
clock: zeroingClock,
name: "all_good",
servers: []upstream.Upstream{slowestUps, slowerUps, fastUps},
}, {
wantStat: map[upstream.Upstream]int64{
fastUps: 9074,
slowerUps: 926,
err1Ups: 8,
fastUps: 9081,
slowerUps: 919,
err1Ups: 7,
},
clock: zeroingClock,
name: "one_bad",
Expand All @@ -179,18 +179,18 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
servers: []upstream.Upstream{err2Ups, err1Ups},
}, {
wantStat: map[upstream.Upstream]int64{
fastUps: 7806,
slowerUps: 830,
fastUps: 7803,
slowerUps: 833,
fastestUps: 1365,
},
clock: zeroingClock,
name: "error_once",
servers: []upstream.Upstream{fastUps, slowerUps, fastestUps},
}, {
wantStat: map[upstream.Upstream]int64{
each200: 5308,
each100: 3099,
each50: 1682,
each200: 5316,
each100: 3090,
each50: 1683,
},
clock: constClock,
name: "error_each_nth",
Expand Down

0 comments on commit df5863b

Please sign in to comment.