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
This commit is contained in:
42
selector/selector.go
Normal file
42
selector/selector.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package selector
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/micro/go-micro/v2/router"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultSelector is the default selector
|
||||
DefaultSelector = NewSelector()
|
||||
|
||||
// ErrNoneAvailable is returned by select when no routes were provided to select from
|
||||
ErrNoneAvailable = errors.New("none available")
|
||||
)
|
||||
|
||||
// Selector selects a route from a pool
|
||||
type Selector interface {
|
||||
// Init a selector with options
|
||||
Init(...Option) error
|
||||
// Options the selector is using
|
||||
Options() Options
|
||||
// Select a route from the pool using the strategy
|
||||
Select([]*router.Route) (*router.Route, error)
|
||||
// Record the error returned from a route to inform future selection
|
||||
Record(*router.Route, error) error
|
||||
// Close the selector
|
||||
Close() error
|
||||
// String returns the name of the selector
|
||||
String() string
|
||||
}
|
||||
|
||||
// Options used to configure a selector
|
||||
type Options struct{}
|
||||
|
||||
// Option updates the options
|
||||
type Option func(*Options)
|
||||
|
||||
// NewSelector creates new selector and returns it
|
||||
func NewSelector(opts ...Option) Selector {
|
||||
return newSelector(opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user