micro/selector/roundrobin/roundrobin.go
Evstigneev Denis 9d6a44b783
Some checks failed
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled
fixed struct alignment && refactor linter
2024-12-09 13:07:01 +03:00

38 lines
814 B
Go

package roundrobin
import (
"go.unistack.org/micro/v3/selector"
"go.unistack.org/micro/v3/util/rand"
)
// NewSelector returns an initialised round robin selector
func NewSelector(_ ...selector.Option) selector.Selector {
return new(roundrobin)
}
type roundrobin struct{}
// Select return routes based on algo
func (r *roundrobin) Select(routes []string, _ ...selector.SelectOption) (selector.Next, error) {
if len(routes) == 0 {
return nil, selector.ErrNoneAvailable
}
var rng rand.Rand
i := rng.Intn(len(routes))
return func() string {
route := routes[i%len(routes)]
// increment
i++
return route
}, nil
}
func (r *roundrobin) Record(_ string, _ error) error { return nil }
func (r *roundrobin) Reset() error { return nil }
func (r *roundrobin) String() string {
return "roundrobin"
}