2020-06-30 14:54:38 +01:00
|
|
|
package roundrobin
|
|
|
|
|
|
|
|
import (
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/selector"
|
2020-06-30 14:54:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewSelector returns an initalised round robin selector
|
|
|
|
func NewSelector(opts ...selector.Option) selector.Selector {
|
2020-08-17 22:44:45 +01:00
|
|
|
return new(roundrobin)
|
2020-06-30 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
type roundrobin struct{}
|
2020-07-02 16:09:48 +01:00
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) {
|
2020-06-30 14:54:38 +01:00
|
|
|
if len(routes) == 0 {
|
|
|
|
return nil, selector.ErrNoneAvailable
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
var i int
|
2020-06-30 14:54:38 +01:00
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
return func() string {
|
|
|
|
route := routes[i%len(routes)]
|
|
|
|
// increment
|
|
|
|
i++
|
|
|
|
return route
|
|
|
|
}, nil
|
2020-06-30 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
func (r *roundrobin) Record(addr string, err error) error { return nil }
|
2020-06-30 14:54:38 +01:00
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
func (r *roundrobin) Reset() error { return nil }
|
2020-06-30 14:54:38 +01:00
|
|
|
|
|
|
|
func (r *roundrobin) String() string {
|
|
|
|
return "roundrobin"
|
|
|
|
}
|