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