cleanup client/selector/lookup (#1937)
* cleanup client/selector/lookup * add mdns router, remove registry from client * fix roundtripper * remove comment * fix compile issue * fix mucp test * fix api router
This commit is contained in:
@@ -1,31 +1,17 @@
|
||||
package selector
|
||||
|
||||
import "github.com/micro/go-micro/v3/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
|
||||
}
|
||||
type SelectOptions struct{}
|
||||
|
||||
// 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
|
||||
@@ -33,9 +19,5 @@ func NewSelectOptions(opts ...SelectOption) SelectOptions {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.Filters == nil {
|
||||
options.Filters = make([]Filter, 0)
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package selector
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/micro/go-micro/v3/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, 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
|
||||
}
|
||||
|
||||
// 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{}
|
||||
}
|
||||
@@ -1,10 +1,44 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/micro/go-micro/v3/selector"
|
||||
)
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
// select a random route from the slice
|
||||
return routes[rand.Intn(len(routes)-1)]
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *random) Record(addr string, err 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 selector.DefaultSelector
|
||||
return new(random)
|
||||
}
|
||||
|
||||
@@ -1,110 +1,35 @@
|
||||
package roundrobin
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/v3/router"
|
||||
"github.com/micro/go-micro/v3/selector"
|
||||
)
|
||||
|
||||
var routeTTL = time.Minute * 15
|
||||
|
||||
// NewSelector returns an initalised round robin selector
|
||||
func NewSelector(opts ...selector.Option) selector.Selector {
|
||||
r := &roundrobin{
|
||||
routes: make(map[uint64]time.Time),
|
||||
ticker: time.NewTicker(time.Minute),
|
||||
}
|
||||
go r.cleanRoutes()
|
||||
return r
|
||||
return new(roundrobin)
|
||||
}
|
||||
|
||||
type roundrobin struct {
|
||||
ticker *time.Ticker
|
||||
|
||||
// routes is a map with the key being a route's hash and the value being the last time it
|
||||
// was used to perform a request
|
||||
routes map[uint64]time.Time
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (r *roundrobin) Init(opts ...selector.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *roundrobin) Options() selector.Options {
|
||||
return selector.Options{}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
type roundrobin struct{}
|
||||
|
||||
func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) {
|
||||
if len(routes) == 0 {
|
||||
return nil, selector.ErrNoneAvailable
|
||||
}
|
||||
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
var i int
|
||||
|
||||
// setLastUsed will update the last used time for a route
|
||||
setLastUsed := func(hash uint64) {
|
||||
r.routes[hash] = time.Now()
|
||||
}
|
||||
|
||||
// if a route hasn't yet been seen, prioritise it
|
||||
for _, route := range routes {
|
||||
if _, ok := r.routes[route.Hash()]; !ok {
|
||||
setLastUsed(route.Hash())
|
||||
return &route, nil
|
||||
}
|
||||
}
|
||||
|
||||
// sort the services by the time they were last used
|
||||
sort.SliceStable(routes, func(i, j int) bool {
|
||||
iLastSeen := r.routes[routes[i].Hash()]
|
||||
jLastSeen := r.routes[routes[j].Hash()]
|
||||
return iLastSeen.UnixNano() < jLastSeen.UnixNano()
|
||||
})
|
||||
|
||||
// return the route which was last used
|
||||
setLastUsed(routes[0].Hash())
|
||||
return &routes[0], nil
|
||||
return func() string {
|
||||
route := routes[i%len(routes)]
|
||||
// increment
|
||||
i++
|
||||
return route
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *roundrobin) Record(srv router.Route, err error) error {
|
||||
return nil
|
||||
}
|
||||
func (r *roundrobin) Record(addr string, err error) error { return nil }
|
||||
|
||||
func (r *roundrobin) Close() error {
|
||||
r.ticker.Stop()
|
||||
return nil
|
||||
}
|
||||
func (r *roundrobin) Reset() error { return nil }
|
||||
|
||||
func (r *roundrobin) String() string {
|
||||
return "roundrobin"
|
||||
}
|
||||
|
||||
func (r *roundrobin) cleanRoutes() {
|
||||
for _ = range r.ticker.C {
|
||||
r.Lock()
|
||||
|
||||
// copy the slice to prevent concurrent map iteration and map write
|
||||
rts := r.routes
|
||||
|
||||
for hash, t := range rts {
|
||||
if t.Unix() < time.Now().Add(-routeTTL).Unix() {
|
||||
delete(r.routes, hash)
|
||||
}
|
||||
}
|
||||
|
||||
r.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package roundrobin
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v3/router"
|
||||
"github.com/micro/go-micro/v3/selector"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -11,39 +10,31 @@ import (
|
||||
func TestRoundRobin(t *testing.T) {
|
||||
selector.Tests(t, NewSelector())
|
||||
|
||||
r1 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8000"}
|
||||
r2 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8001"}
|
||||
r3 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8002"}
|
||||
r1 := "127.0.0.1:8000"
|
||||
r2 := "127.0.0.1:8001"
|
||||
r3 := "127.0.0.1:8002"
|
||||
|
||||
sel := NewSelector()
|
||||
|
||||
// By passing r1 and r2 first, it forces a set sequence of (r1 => r2 => r3 => r1)
|
||||
|
||||
r, err := sel.Select([]router.Route{r1})
|
||||
next, err := sel.Select([]string{r1})
|
||||
r := next()
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r1, *r, "Expected route to be r1")
|
||||
assert.Equal(t, r1, r, "Expected route to be r1")
|
||||
|
||||
r, err = sel.Select([]router.Route{r2})
|
||||
next, err = sel.Select([]string{r2})
|
||||
r = next()
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r2, *r, "Expected route to be r2")
|
||||
assert.Equal(t, r2, r, "Expected route to be r2")
|
||||
|
||||
// Because r1 and r2 have been recently called, r3 should be chosen
|
||||
|
||||
r, err = sel.Select([]router.Route{r1, r2, r3})
|
||||
next, err = sel.Select([]string{r1, r2, r3})
|
||||
n1, n2, n3 := next(), next(), next()
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r3, *r, "Expected route to be r3")
|
||||
assert.Equal(t, r1, n1, "Expected route to be r3")
|
||||
assert.Equal(t, r2, n2, "Expected route to be r3")
|
||||
assert.Equal(t, r3, n3, "Expected route to be r3")
|
||||
|
||||
// r1 was called longest ago, so it should be prioritised
|
||||
|
||||
r, err = sel.Select([]router.Route{r1, r2, r3})
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r1, *r, "Expected route to be r1")
|
||||
|
||||
r, err = sel.Select([]router.Route{r1, r2, r3})
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r2, *r, "Expected route to be r2")
|
||||
|
||||
r, err = sel.Select([]router.Route{r1, r2, r3})
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r3, *r, "Expected route to be r3")
|
||||
}
|
||||
|
||||
@@ -3,35 +3,24 @@ package selector
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/micro/go-micro/v3/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, ...SelectOption) (*router.Route, error)
|
||||
Select([]string, ...SelectOption) (Next, error)
|
||||
// Record the error returned from a route to inform future selection
|
||||
Record(router.Route, error) error
|
||||
// Close the selector
|
||||
Close() error
|
||||
Record(string, error) error
|
||||
// Reset the selector
|
||||
Reset() error
|
||||
// String returns the name of the selector
|
||||
String() string
|
||||
}
|
||||
|
||||
// NewSelector creates new selector and returns it
|
||||
func NewSelector(opts ...Option) Selector {
|
||||
return newSelector(opts...)
|
||||
}
|
||||
// Next returns the next node
|
||||
type Next func() string
|
||||
|
||||
@@ -3,47 +3,35 @@ package selector
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v3/router"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Tests runs all the tests against a selector to ensure the implementations are consistent
|
||||
func Tests(t *testing.T, s Selector) {
|
||||
r1 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8000"}
|
||||
r2 := router.Route{Service: "go.micro.service.foo", Address: "127.0.0.1:8001"}
|
||||
r1 := "127.0.0.1:8000"
|
||||
r2 := "127.0.0.1:8001"
|
||||
|
||||
t.Run("Select", func(t *testing.T) {
|
||||
t.Run("NoRoutes", func(t *testing.T) {
|
||||
srv, err := s.Select([]router.Route{})
|
||||
assert.Nil(t, srv, "Route should be nil")
|
||||
_, err := s.Select([]string{})
|
||||
assert.Equal(t, ErrNoneAvailable, err, "Expected error to be none available")
|
||||
})
|
||||
|
||||
t.Run("OneRoute", func(t *testing.T) {
|
||||
srv, err := s.Select([]router.Route{r1})
|
||||
next, err := s.Select([]string{r1})
|
||||
srv := next()
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
assert.Equal(t, r1, *srv, "Expected the route to be returned")
|
||||
assert.Equal(t, r1, srv, "Expected the route to be returned")
|
||||
})
|
||||
|
||||
t.Run("MultipleRoutes", func(t *testing.T) {
|
||||
srv, err := s.Select([]router.Route{r1, r2})
|
||||
next, err := s.Select([]string{r1, r2})
|
||||
assert.Nil(t, err, "Error should be nil")
|
||||
if srv.Address != r1.Address && srv.Address != r2.Address {
|
||||
srv := next()
|
||||
if srv != r1 && srv != r2 {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user