Skip to content

Commit

Permalink
Added support of fadeIn in powerOfNRandomChoices
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Zavodskikh <[email protected]>
  • Loading branch information
Roman Zavodskikh committed Oct 27, 2023
1 parent c8561da commit 415b905
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions loadbalancer/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,25 @@ func (p *powerOfRandomNChoices) Apply(ctx *routing.LBContext) routing.LBEndpoint
for i := 1; i < p.numberOfChoices; i++ {
ce := ctx.LBEndpoints[p.rnd.Intn(ne)]

if p.getScore(ce) > p.getScore(best) {
if p.getScore(ctx, ce) > p.getScore(ctx, best) {
best = ce
}
}
return best
}

// getScore returns negative value of inflightrequests count.
func (p *powerOfRandomNChoices) getScore(e routing.LBEndpoint) int64 {
// getScore returns negative value of inflightrequests count, discounted by fadeIn multiplier if needed
func (p *powerOfRandomNChoices) getScore(ctx *routing.LBContext, e routing.LBEndpoint) float64 {
now := time.Now()
f := fadeIn(
now,
ctx.Route.LBFadeInDuration,
ctx.Route.LBFadeInExponent,
e.Detected,
)

// endpoints with higher inflight request should have lower score
return -int64(e.Metrics.GetInflightRequests())
return -float64(e.Metrics.GetInflightRequests()) / f
}

type (
Expand Down

0 comments on commit 415b905

Please sign in to comment.