Evstigneev Denis
38c5fe8b5a
All checks were successful
test / test (push) Successful in 42s
## Pull Request template Please, go through these steps before clicking submit on this PR. 1. Give a descriptive title to your PR. 2. Provide a description of your changes. 3. Make sure you have some relevant tests. 4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). **PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** Reviewed-on: #369 Co-authored-by: Evstigneev Denis <danteevstigneev@yandex.ru> Co-committed-by: Evstigneev Denis <danteevstigneev@yandex.ru>
44 lines
896 B
Go
44 lines
896 B
Go
package random
|
|
|
|
import (
|
|
"go.unistack.org/micro/v3/selector"
|
|
"go.unistack.org/micro/v3/util/rand"
|
|
)
|
|
|
|
type random struct{}
|
|
|
|
func (r *random) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) {
|
|
// we can't select from an empty pool of routes
|
|
if len(routes) == 0 {
|
|
return nil, selector.ErrNoneAvailable
|
|
}
|
|
|
|
// return the next func
|
|
return func() string {
|
|
// if there is only one route provided we'll select it
|
|
if len(routes) == 1 {
|
|
return routes[0]
|
|
}
|
|
var rng rand.Rand
|
|
// select a random route from the slice
|
|
return routes[rng.Intn(len(routes))]
|
|
}, nil
|
|
}
|
|
|
|
func (r *random) Record(_ string, _ error) error {
|
|
return nil
|
|
}
|
|
|
|
func (r *random) Reset() error {
|
|
return nil
|
|
}
|
|
|
|
func (r *random) String() string {
|
|
return "random"
|
|
}
|
|
|
|
// NewSelector returns a random selector
|
|
func NewSelector(opts ...selector.Option) selector.Selector {
|
|
return &random{}
|
|
}
|