selector: add filters to replace depricated client/selector filters (#1785)

This commit is contained in:
ben-toogood
2020-07-02 16:09:48 +01:00
committed by GitHub
parent d577dd6abe
commit 6898a65508
7 changed files with 86 additions and 91 deletions

41
selector/options.go Normal file
View File

@@ -0,0 +1,41 @@
package selector
import "github.com/micro/go-micro/v2/router"
// Options used to configure a selector
type Options struct{}
// Option updates the options
type Option func(*Options)
// Filter the routes
type Filter func([]router.Route) []router.Route
// SelectOptions used to configure selection
type SelectOptions struct {
Filters []Filter
}
// SelectOption updates the select options
type SelectOption func(*SelectOptions)
// WithFilter adds a filter to the options
func WithFilter(f Filter) SelectOption {
return func(o *SelectOptions) {
o.Filters = append(o.Filters, f)
}
}
// NewSelectOptions parses select options
func NewSelectOptions(opts ...SelectOption) SelectOptions {
var options SelectOptions
for _, o := range opts {
o(&options)
}
if options.Filters == nil {
options.Filters = make([]Filter, 0)
}
return options
}

View File

@@ -16,7 +16,15 @@ func (r *random) Options() Options {
return Options{}
}
func (r *random) Select(routes []router.Route) (*router.Route, error) {
func (r *random) Select(routes []router.Route, opts ...SelectOption) (*router.Route, error) {
// parse the options
options := NewSelectOptions(opts...)
// apply the filters
for _, f := range options.Filters {
routes = f(routes)
}
// we can't select from an empty pool of routes
if len(routes) == 0 {
return nil, ErrNoneAvailable

View File

@@ -38,7 +38,15 @@ func (r *roundrobin) Options() selector.Options {
return selector.Options{}
}
func (r *roundrobin) Select(routes []router.Route) (*router.Route, error) {
func (r *roundrobin) Select(routes []router.Route, opts ...selector.SelectOption) (*router.Route, error) {
// parse the options
options := selector.NewSelectOptions(opts...)
// apply the filters
for _, f := range options.Filters {
routes = f(routes)
}
if len(routes) == 0 {
return nil, selector.ErrNoneAvailable
}

View File

@@ -21,7 +21,7 @@ type Selector interface {
// Options the selector is using
Options() Options
// Select a route from the pool using the strategy
Select([]router.Route) (*router.Route, error)
Select([]router.Route, ...SelectOption) (*router.Route, error)
// Record the error returned from a route to inform future selection
Record(router.Route, error) error
// Close the selector
@@ -30,12 +30,6 @@ type Selector interface {
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...)

View File

@@ -32,6 +32,18 @@ func Tests(t *testing.T, s Selector) {
t.Errorf("Expected the route to be one of the inputs")
}
})
t.Run("Filters", func(t *testing.T) {
var filterApplied bool
filter := func(rts []router.Route) []router.Route {
filterApplied = true
return rts
}
_, err := s.Select([]router.Route{r1, r2}, WithFilter(filter))
assert.Nil(t, err, "Error should be nil")
assert.True(t, filterApplied, "Filters should be applied")
})
})
t.Run("Record", func(t *testing.T) {