Files
Archive/echo/internal/lb/round_robin.go
2024-07-21 20:29:05 +02:00

46 lines
787 B
Go

package lb
import (
"time"
"go.uber.org/atomic"
)
type Node struct {
Address string
Label string
HandShakeDuration time.Duration
}
func (n *Node) Clone() *Node {
return &Node{
Address: n.Address,
Label: n.Label,
HandShakeDuration: n.HandShakeDuration,
}
}
// RoundRobin is an interface for representing round-robin balancing.
type RoundRobin interface {
Next() *Node
}
type roundrobin struct {
nodeList []*Node
next *atomic.Int64
len int
}
func NewRoundRobin(nodeList []*Node) RoundRobin {
len := len(nodeList)
next := atomic.NewInt64(0)
return &roundrobin{nodeList: nodeList, len: len, next: next}
}
func (r *roundrobin) Next() *Node {
n := r.next.Add(1)
next := r.nodeList[(int(n)-1)%r.len]
return next
}