Merge pull request #872 from micro/round-robin

Round robin routes we've sorted by metric
This commit is contained in:
Asim Aslam 2019-10-22 11:58:44 +01:00 committed by GitHub
commit 317cf76566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes" "github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/config/options" "github.com/micro/go-micro/config/options"
@ -252,11 +253,18 @@ func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server
routes = addr routes = addr
} }
var opts []client.CallOption
// set strategy to round robin
opts = append(opts, client.WithSelectOption(selector.WithStrategy(selector.RoundRobin)))
// if the address is already set just serve it // if the address is already set just serve it
// TODO: figure it out if we should know to pick a link // TODO: figure it out if we should know to pick a link
if len(addresses) > 0 { if len(addresses) > 0 {
opts = append(opts, client.WithAddress(addresses...))
// serve the normal way // serve the normal way
return p.serveRequest(ctx, p.Client, service, endpoint, req, rsp, client.WithAddress(addresses...)) return p.serveRequest(ctx, p.Client, service, endpoint, req, rsp, opts...)
} }
// there's no links e.g we're local routing then just serve it with addresses // there's no links e.g we're local routing then just serve it with addresses
@ -288,9 +296,10 @@ func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server
// set the address to call // set the address to call
addresses := toNodes([]router.Route{route}) addresses := toNodes([]router.Route{route})
opts = append(opts, client.WithAddress(addresses...))
// do the request with the link // do the request with the link
gerr = p.serveRequest(ctx, link, service, endpoint, req, rsp, client.WithAddress(addresses...)) gerr = p.serveRequest(ctx, link, service, endpoint, req, rsp, opts...)
// return on no error since we succeeded // return on no error since we succeeded
if gerr == nil { if gerr == nil {
return nil return nil