Update registry so version is surfaced
This commit is contained in:
29
client/node_selector.go
Normal file
29
client/node_selector.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/myodc/go-micro/errors"
|
||||
"github.com/myodc/go-micro/registry"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func nodeSelector(service []*registry.Service) (*registry.Node, error) {
|
||||
if len(service) == 0 {
|
||||
return nil, errors.NotFound("go.micro.client", "Service not found")
|
||||
}
|
||||
|
||||
i := rand.Int()
|
||||
j := i % len(service)
|
||||
|
||||
if len(service[j].Nodes) == 0 {
|
||||
return nil, errors.NotFound("go.micro.client", "Service not found")
|
||||
}
|
||||
|
||||
n := i % len(service[j].Nodes)
|
||||
return service[j].Nodes[n], nil
|
||||
}
|
||||
46
client/node_selector_test.go
Normal file
46
client/node_selector_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/myodc/go-micro/registry"
|
||||
)
|
||||
|
||||
func TestNodeSelector(t *testing.T) {
|
||||
services := []*registry.Service{
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
Address: "localhost",
|
||||
Port: 9999,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.1",
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-321",
|
||||
Address: "localhost",
|
||||
Port: 6666,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
counts := map[string]int{}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
n, err := nodeSelector(services)
|
||||
if err != nil {
|
||||
t.Errorf("Expected node, got err: %v", err)
|
||||
}
|
||||
counts[n.Id]++
|
||||
}
|
||||
|
||||
t.Logf("Counts %v", counts)
|
||||
}
|
||||
@@ -3,10 +3,8 @@ package client
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/myodc/go-micro/broker"
|
||||
c "github.com/myodc/go-micro/context"
|
||||
@@ -29,10 +27,6 @@ type rpcClient struct {
|
||||
opts options
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func newRpcClient(opt ...Option) Client {
|
||||
var opts options
|
||||
|
||||
@@ -129,13 +123,11 @@ func (r *rpcClient) Call(ctx context.Context, request Request, response interfac
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
if len(service.Nodes) == 0 {
|
||||
return errors.NotFound("go.micro.client", "Service not found")
|
||||
node, err := nodeSelector(service)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n := rand.Int() % len(service.Nodes)
|
||||
node := service.Nodes[n]
|
||||
|
||||
address := node.Address
|
||||
if node.Port > 0 {
|
||||
address = fmt.Sprintf("%s:%d", address, node.Port)
|
||||
@@ -154,13 +146,11 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan in
|
||||
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
if len(service.Nodes) == 0 {
|
||||
return nil, errors.NotFound("go.micro.client", "Service not found")
|
||||
node, err := nodeSelector(service)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
n := rand.Int() % len(service.Nodes)
|
||||
node := service.Nodes[n]
|
||||
|
||||
address := node.Address
|
||||
if node.Port > 0 {
|
||||
address = fmt.Sprintf("%s:%d", address, node.Port)
|
||||
|
||||
Reference in New Issue
Block a user