sort routes by metric

This commit is contained in:
Asim Aslam 2020-08-17 23:09:24 +01:00
parent bb7fe21c46
commit 3011bad518

View File

@ -2,6 +2,7 @@ package client
import ( import (
"context" "context"
"sort"
"github.com/micro/go-micro/v3/errors" "github.com/micro/go-micro/v3/errors"
"github.com/micro/go-micro/v3/router" "github.com/micro/go-micro/v3/router"
@ -34,6 +35,11 @@ func LookupRoute(ctx context.Context, req Request, opts CallOptions) ([]string,
return nil, errors.InternalServerError("go.micro.client", "error getting next %s node: %s", req.Service(), err.Error()) return nil, errors.InternalServerError("go.micro.client", "error getting next %s node: %s", req.Service(), err.Error())
} }
// sort by lowest metric first
sort.Slice(routes, func(i, j int) bool {
return routes[i].Metric < routes[j].Metric
})
var addrs []string var addrs []string
for _, route := range routes { for _, route := range routes {