File size: 649 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package allocation

import (
	"sync/atomic"

	"github.com/pinchtab/pinchtab/internal/bridge"
)

// RoundRobin cycles through candidates in order.
// Thread-safe via atomic counter.
type RoundRobin struct {
	counter atomic.Uint64
}

// NewRoundRobin creates a new RoundRobin policy.
func NewRoundRobin() *RoundRobin {
	return &RoundRobin{}
}

func (rr *RoundRobin) Name() string { return "round_robin" }

func (rr *RoundRobin) Select(candidates []bridge.Instance) (bridge.Instance, error) {
	if len(candidates) == 0 {
		return bridge.Instance{}, ErrNoCandidates
	}
	idx := rr.counter.Add(1) - 1
	return candidates[idx%uint64(len(candidates))], nil
}