move reg util to own package (#1523)
* move reg util to own package * fix test * fix broken static router
This commit is contained in:
parent
4fd12430d0
commit
e5268dd0a6
@ -16,6 +16,7 @@ import (
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/metadata"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
util "github.com/micro/go-micro/v2/util/registry"
|
||||
)
|
||||
|
||||
type endpoint struct {
|
||||
@ -164,7 +165,7 @@ func (r *staticRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
||||
|
||||
// hack for stream endpoint
|
||||
if ep.apiep.Stream {
|
||||
svcs := registry.Copy(services)
|
||||
svcs := util.Copy(services)
|
||||
for _, svc := range svcs {
|
||||
if len(svc.Endpoints) == 0 {
|
||||
e := ®istry.Endpoint{}
|
||||
|
5
registry/cache/cache.go
vendored
5
registry/cache/cache.go
vendored
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
util "github.com/micro/go-micro/v2/util/registry"
|
||||
)
|
||||
|
||||
// Cache is the registry cache interface
|
||||
@ -119,7 +120,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
|
||||
// get cache ttl
|
||||
ttl := c.ttls[service]
|
||||
// make a copy
|
||||
cp := registry.Copy(services)
|
||||
cp := util.Copy(services)
|
||||
|
||||
// got services && within ttl so return cache
|
||||
if c.isValid(cp, ttl) {
|
||||
@ -152,7 +153,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
|
||||
|
||||
// cache results
|
||||
c.Lock()
|
||||
c.set(service, registry.Copy(services))
|
||||
c.set(service, util.Copy(services))
|
||||
c.Unlock()
|
||||
|
||||
return services, nil
|
||||
|
@ -1,7 +1,11 @@
|
||||
package registry
|
||||
|
||||
func addNodes(old, neu []*Node) []*Node {
|
||||
nodes := make([]*Node, len(neu))
|
||||
import (
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
)
|
||||
|
||||
func addNodes(old, neu []*registry.Node) []*registry.Node {
|
||||
nodes := make([]*registry.Node, len(neu))
|
||||
// add all new nodes
|
||||
for i, n := range neu {
|
||||
node := *n
|
||||
@ -31,8 +35,8 @@ func addNodes(old, neu []*Node) []*Node {
|
||||
return nodes
|
||||
}
|
||||
|
||||
func delNodes(old, del []*Node) []*Node {
|
||||
var nodes []*Node
|
||||
func delNodes(old, del []*registry.Node) []*registry.Node {
|
||||
var nodes []*registry.Node
|
||||
for _, o := range old {
|
||||
var rem bool
|
||||
for _, n := range del {
|
||||
@ -49,24 +53,24 @@ func delNodes(old, del []*Node) []*Node {
|
||||
}
|
||||
|
||||
// CopyService make a copy of service
|
||||
func CopyService(service *Service) *Service {
|
||||
func CopyService(service *registry.Service) *registry.Service {
|
||||
// copy service
|
||||
s := new(Service)
|
||||
s := new(registry.Service)
|
||||
*s = *service
|
||||
|
||||
// copy nodes
|
||||
nodes := make([]*Node, len(service.Nodes))
|
||||
nodes := make([]*registry.Node, len(service.Nodes))
|
||||
for j, node := range service.Nodes {
|
||||
n := new(Node)
|
||||
n := new(registry.Node)
|
||||
*n = *node
|
||||
nodes[j] = n
|
||||
}
|
||||
s.Nodes = nodes
|
||||
|
||||
// copy endpoints
|
||||
eps := make([]*Endpoint, len(service.Endpoints))
|
||||
eps := make([]*registry.Endpoint, len(service.Endpoints))
|
||||
for j, ep := range service.Endpoints {
|
||||
e := new(Endpoint)
|
||||
e := new(registry.Endpoint)
|
||||
*e = *ep
|
||||
eps[j] = e
|
||||
}
|
||||
@ -75,8 +79,8 @@ func CopyService(service *Service) *Service {
|
||||
}
|
||||
|
||||
// Copy makes a copy of services
|
||||
func Copy(current []*Service) []*Service {
|
||||
services := make([]*Service, len(current))
|
||||
func Copy(current []*registry.Service) []*registry.Service {
|
||||
services := make([]*registry.Service, len(current))
|
||||
for i, service := range current {
|
||||
services[i] = CopyService(service)
|
||||
}
|
||||
@ -84,14 +88,14 @@ func Copy(current []*Service) []*Service {
|
||||
}
|
||||
|
||||
// Merge merges two lists of services and returns a new copy
|
||||
func Merge(olist []*Service, nlist []*Service) []*Service {
|
||||
var srv []*Service
|
||||
func Merge(olist []*registry.Service, nlist []*registry.Service) []*registry.Service {
|
||||
var srv []*registry.Service
|
||||
|
||||
for _, n := range nlist {
|
||||
var seen bool
|
||||
for _, o := range olist {
|
||||
if o.Version == n.Version {
|
||||
sp := new(Service)
|
||||
sp := new(registry.Service)
|
||||
// make copy
|
||||
*sp = *o
|
||||
// set nodes
|
||||
@ -102,25 +106,25 @@ func Merge(olist []*Service, nlist []*Service) []*Service {
|
||||
srv = append(srv, sp)
|
||||
break
|
||||
} else {
|
||||
sp := new(Service)
|
||||
sp := new(registry.Service)
|
||||
// make copy
|
||||
*sp = *o
|
||||
srv = append(srv, sp)
|
||||
}
|
||||
}
|
||||
if !seen {
|
||||
srv = append(srv, Copy([]*Service{n})...)
|
||||
srv = append(srv, Copy([]*registry.Service{n})...)
|
||||
}
|
||||
}
|
||||
return srv
|
||||
}
|
||||
|
||||
// Remove removes services and returns a new copy
|
||||
func Remove(old, del []*Service) []*Service {
|
||||
var services []*Service
|
||||
func Remove(old, del []*registry.Service) []*registry.Service {
|
||||
var services []*registry.Service
|
||||
|
||||
for _, o := range old {
|
||||
srv := new(Service)
|
||||
srv := new(registry.Service)
|
||||
*srv = *o
|
||||
|
||||
var rem bool
|
@ -3,14 +3,16 @@ package registry
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
)
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
services := []*Service{
|
||||
services := []*registry.Service{
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
Address: "localhost:9999",
|
||||
@ -20,7 +22,7 @@ func TestRemove(t *testing.T) {
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
Address: "localhost:6666",
|
||||
@ -29,7 +31,7 @@ func TestRemove(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
servs := Remove([]*Service{services[0]}, []*Service{services[1]})
|
||||
servs := Remove([]*registry.Service{services[0]}, []*registry.Service{services[1]})
|
||||
if i := len(servs); i > 0 {
|
||||
t.Errorf("Expected 0 nodes, got %d: %+v", i, servs)
|
||||
}
|
||||
@ -39,11 +41,11 @@ func TestRemove(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRemoveNodes(t *testing.T) {
|
||||
services := []*Service{
|
||||
services := []*registry.Service{
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
Address: "localhost:9999",
|
||||
@ -57,7 +59,7 @@ func TestRemoveNodes(t *testing.T) {
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
Address: "localhost:6666",
|
Loading…
Reference in New Issue
Block a user