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:
Asim Aslam
2020-08-17 22:44:45 +01:00
committed by GitHub
parent 7135787b78
commit 50ec6c748f
17 changed files with 398 additions and 418 deletions

View File

@@ -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()
}
}

View File

@@ -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")
}