micro/selector/roundrobin/roundrobin.go
Vasiliy Tolstov 0ddc8de00b apply micro commit 2eb19c2e97d9316438bc66cd2cda896e8c99d026
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-11-30 22:48:00 +03:00

39 lines
792 B
Go

package roundrobin
import (
"math/rand"
"github.com/unistack-org/micro/v3/selector"
)
// 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
}
i := rand.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"
}