micro/selector/random.go
ben-toogood 6337c92cd0
selector: new selector interface with random & roundrobin implementation (#1761)
* selector: implement new selector interface plus random & roundrobin implementations

* selector/roundrobin: remove unused consts

* router: add close method to interface

* selector/roundrobin: fix concurrent map iteration and map write

* selector: replace variadic argument on Select
2020-06-30 14:54:38 +01:00

49 lines
842 B
Go

package selector
import (
"math/rand"
"github.com/micro/go-micro/v2/router"
)
type random struct{}
func (r *random) Init(opts ...Option) error {
return nil
}
func (r *random) Options() Options {
return Options{}
}
func (r *random) Select(routes []*router.Route) (*router.Route, error) {
// we can't select from an empty pool of routes
if len(routes) == 0 {
return nil, ErrNoneAvailable
}
// if there is only one route provided we'll select it
if len(routes) == 1 {
return routes[0], nil
}
// select a random route from the slice
return routes[rand.Intn(len(routes)-1)], nil
}
func (r *random) Record(route *router.Route, err error) error {
return nil
}
func (r *random) Close() error {
return nil
}
func (r *random) String() string {
return "random"
}
func newSelector(...Option) Selector {
return &random{}
}