Skip to content

Commit

Permalink
Merge branch 'master' into stream-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-toogood authored Apr 16, 2020
2 parents 2dfaab4 + 62cedf6 commit 5bb18e6
Show file tree
Hide file tree
Showing 15 changed files with 1,603 additions and 208 deletions.
1 change: 0 additions & 1 deletion api/handler/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
&request,
client.WithContentType(ct),
)

// make the call
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
writeError(w, r, err)
Expand Down
156 changes: 117 additions & 39 deletions api/router/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ import (
"fmt"
"net/http"
"regexp"
"strings"
"sync"
"time"

"github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/router"
"github.com/micro/go-micro/v2/api/router/util"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/cache"
)

// endpoint struct, that holds compiled pcre
type endpoint struct {
hostregs []*regexp.Regexp
pathregs []util.Pattern
}

// router is the default router
type registryRouter struct {
exit chan bool
Expand All @@ -26,6 +35,8 @@ type registryRouter struct {

sync.RWMutex
eps map[string]*api.Service
// compiled regexp for host and path
ceps map[string]*endpoint
}

func (r *registryRouter) isClosed() bool {
Expand Down Expand Up @@ -67,6 +78,7 @@ func (r *registryRouter) refresh() {
}

// refresh list in 10 minutes... cruft
// use registry watching
select {
case <-time.After(time.Minute * 10):
case <-r.exit:
Expand Down Expand Up @@ -109,11 +121,11 @@ func (r *registryRouter) store(services []*registry.Service) {
names[service.Name] = true

// map per endpoint
for _, endpoint := range service.Endpoints {
for _, sep := range service.Endpoints {
// create a key service:endpoint_name
key := fmt.Sprintf("%s:%s", service.Name, endpoint.Name)
key := fmt.Sprintf("%s.%s", service.Name, sep.Name)
// decode endpoint
end := api.Decode(endpoint.Metadata)
end := api.Decode(sep.Metadata)

// if we got nothing skip
if err := api.Validate(end); err != nil {
Expand Down Expand Up @@ -154,8 +166,44 @@ func (r *registryRouter) store(services []*registry.Service) {
}

// now set the eps we have
for name, endpoint := range eps {
r.eps[name] = endpoint
for name, ep := range eps {
r.eps[name] = ep
cep := &endpoint{}

for _, h := range ep.Endpoint.Host {
if h == "" || h == "*" {
continue
}
hostreg, err := regexp.CompilePOSIX(h)
if err != nil {
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
logger.Tracef("endpoint have invalid host regexp: %v", err)
}
continue
}
cep.hostregs = append(cep.hostregs, hostreg)
}

for _, p := range ep.Endpoint.Path {
rule, err := util.Parse(p)
if err != nil {
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
logger.Tracef("endpoint have invalid path pattern: %v", err)
}
continue
}
tpl := rule.Compile()
pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "")
if err != nil {
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
logger.Tracef("endpoint have invalid path pattern: %v", err)
}
continue
}
cep.pathregs = append(cep.pathregs, pathreg)
}

r.ceps[name] = cep
}
}

Expand Down Expand Up @@ -239,60 +287,89 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
r.RLock()
defer r.RUnlock()

var idx int
if len(req.URL.Path) > 0 && req.URL.Path != "/" {
idx = 1
}
path := strings.Split(req.URL.Path[idx:], "/")

// use the first match
// TODO: weighted matching
for _, e := range r.eps {
for n, e := range r.eps {
cep, ok := r.ceps[n]
if !ok {
continue
}
ep := e.Endpoint

// match
var pathMatch, hostMatch, methodMatch bool

// 1. try method GET, POST, PUT, etc
// 2. try host example.com, foobar.com, etc
// 3. try path /foo/bar, /bar/baz, etc

// 1. try match method
var mMatch, hMatch, pMatch bool
// 1. try method
methodLoop:
for _, m := range ep.Method {
if req.Method == m {
methodMatch = true
break
if m == req.Method {
mMatch = true
break methodLoop
}
}

// no match on method pass
if len(ep.Method) > 0 && !methodMatch {
if !mMatch {
continue
}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("api method match %s", req.Method)
}

// 2. try match host
for _, h := range ep.Host {
if req.Host == h {
hostMatch = true
break
// 2. try host
if len(ep.Host) == 0 {
hMatch = true
} else {
hostLoop:
for idx, h := range ep.Host {
if h == "" || h == "*" {
hMatch = true
break hostLoop
} else {
if cep.hostregs[idx].MatchString(req.URL.Host) {
hMatch = true
break hostLoop
}
}
}
}

// no match on host pass
if len(ep.Host) > 0 && !hostMatch {
if !hMatch {
continue
}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("api host match %s", req.URL.Host)
}

// 3. try match paths
for _, p := range ep.Path {
re, err := regexp.CompilePOSIX(p)
if err == nil && re.MatchString(req.URL.Path) {
pathMatch = true
break
// 3. try path
// 3. try path
pathLoop:
for _, pathreg := range cep.pathregs {
matches, err := pathreg.Match(path, "")
if err != nil {
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("api path not match %s != %v", path, pathreg)
}
continue
}
pMatch = true
ctx := req.Context()
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(metadata.Metadata)
}
for k, v := range matches {
md[fmt.Sprintf("x-api-field-%s", k)] = v
}
md["x-api-body"] = ep.Body
*req = *req.Clone(metadata.NewContext(ctx, md))
break pathLoop
}

// no match pass
if len(ep.Path) > 0 && !pathMatch {
if !pMatch {
continue
}

// TODO: Percentage traffic

// we got here, so its a match
return e, nil
}
Expand Down Expand Up @@ -377,6 +454,7 @@ func newRouter(opts ...router.Option) *registryRouter {
opts: options,
rc: cache.New(options.Registry),
eps: make(map[string]*api.Service),
ceps: make(map[string]*endpoint),
}
go r.watch()
go r.refresh()
Expand Down
136 changes: 0 additions & 136 deletions api/router/registry/registry_test.go

This file was deleted.

Loading

0 comments on commit 5bb18e6

Please sign in to comment.