Back to all reviewers

optimize selection algorithms

traefik/traefik
Based on 2 comments
Go

When implementing selection algorithms that choose from a pool of candidates, design the algorithm to filter invalid options during the selection process rather than using retry logic after selection. Additionally, always include termination conditions to prevent infinite loops when no valid candidates remain.

Algorithms Go

Reviewer Prompt

When implementing selection algorithms that choose from a pool of candidates, design the algorithm to filter invalid options during the selection process rather than using retry logic after selection. Additionally, always include termination conditions to prevent infinite loops when no valid candidates remain.

The retry approach is inefficient because it may require multiple selection attempts, and each rejected selection wastes computational resources. A filtering approach evaluates constraints once during traversal.

Example of improved selection algorithm:

for {
    // Pick handler with closest deadline
    handler = heap.Pop(b).(*namedHandler)
    
    b.curDeadline = handler.deadline
    handler.deadline += 1 / handler.weight
    heap.Push(b, handler)
    
    // Filter during selection instead of retrying
    if _, down := b.status[handler.name]; !down {
        continue
    }
    
    if _, isFenced := b.fenced[handler.name]; isFenced {
        continue
    }
    
    if handler.passiveHealthChecker != nil && !handler.passiveHealthChecker.AllowRequest() {
        continue
    }
    
    // Always include termination condition
    if allHandlersFenced() {
        return nil, errors.New("no available handlers")
    }
    
    return handler, nil
}

This approach eliminates the need for separate retry methods like nextServerExcluding and prevents infinite loops when all candidates are invalid.

2
Comments Analyzed
Go
Primary Language
Algorithms
Category

Source Discussions