6337c92cd0
* 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
49 lines
842 B
Go
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{}
|
|
}
|