micro/selector/roundrobin/roundrobin.go
Vasiliy Tolstov 5596345382 util/rand: replace all non crypto rand stuff with own rand package
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-18 12:44:18 +03:00

38 lines
841 B
Go

package roundrobin
import (
"github.com/unistack-org/micro/v3/selector"
"github.com/unistack-org/micro/v3/util/rand"
)
// NewSelector returns an initialised round robin selector
func NewSelector(opts ...selector.Option) selector.Selector {
return new(roundrobin)
}
type roundrobin struct{}
// Select return routes based on algo
func (r *roundrobin) Select(routes []string, opts ...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(addr string, err error) error { return nil }
func (r *roundrobin) Reset() error { return nil }
func (r *roundrobin) String() string {
return "roundrobin"
}