Strip namespace from registry router

This commit is contained in:
Asim Aslam 2020-04-08 15:38:02 +01:00 committed by Vasiliy Tolstov
parent 7970ee41af
commit f34a4d29de
2 changed files with 2 additions and 74 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"strings"
"sync" "sync"
"time" "time"
@ -29,28 +28,6 @@ type registryRouter struct {
eps map[string]*api.Service eps map[string]*api.Service
} }
func setNamespace(ns, name string) string {
ns = strings.TrimSpace(ns)
name = strings.TrimSpace(name)
// no namespace
if len(ns) == 0 {
return name
}
switch {
// has - suffix
case strings.HasSuffix(ns, "-"):
return strings.Replace(ns+name, ".", "-", -1)
// has . suffix
case strings.HasSuffix(ns, "."):
return ns + name
}
// default join .
return strings.Join([]string{ns, name}, ".")
}
func (r *registryRouter) isClosed() bool { func (r *registryRouter) isClosed() bool {
select { select {
case <-r.exit: case <-r.exit:
@ -79,10 +56,6 @@ func (r *registryRouter) refresh() {
// for each service, get service and store endpoints // for each service, get service and store endpoints
for _, s := range services { for _, s := range services {
// only get services for this namespace
if !strings.HasPrefix(s.Name, r.opts.Namespace) {
continue
}
service, err := r.rc.GetService(s.Name) service, err := r.rc.GetService(s.Name)
if err != nil { if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) { if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
@ -105,7 +78,7 @@ func (r *registryRouter) refresh() {
// process watch event // process watch event
func (r *registryRouter) process(res *registry.Result) { func (r *registryRouter) process(res *registry.Result) {
// skip these things // skip these things
if res == nil || res.Service == nil || !strings.HasPrefix(res.Service.Name, r.opts.Namespace) { if res == nil || res.Service == nil {
return return
} }
@ -350,7 +323,7 @@ func (r *registryRouter) Route(req *http.Request) (*api.Service, error) {
} }
// service name // service name
name := setNamespace(r.opts.Namespace, rp.Name) name := rp.Name
// get service // get service
services, err := r.rc.GetService(name) services, err := r.rc.GetService(name)

View File

@ -9,51 +9,6 @@ import (
"github.com/micro/go-micro/v2/api" "github.com/micro/go-micro/v2/api"
) )
func TestSetNamespace(t *testing.T) {
testCases := []struct {
namespace string
name string
expected string
}{
// default dotted path
{
"go.micro.api",
"foo",
"go.micro.api.foo",
},
// dotted end
{
"go.micro.api.",
"foo",
"go.micro.api.foo",
},
// dashed end
{
"go-micro-api-",
"foo",
"go-micro-api-foo",
},
// no namespace
{
"",
"foo",
"foo",
},
{
"go-micro-api-",
"v2.foo",
"go-micro-api-v2-foo",
},
}
for _, test := range testCases {
name := setNamespace(test.namespace, test.name)
if name != test.expected {
t.Fatalf("expected name %s got %s", test.expected, name)
}
}
}
func TestRouter(t *testing.T) { func TestRouter(t *testing.T) {
r := newRouter() r := newRouter()