1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

move selector

This commit is contained in:
Asim Aslam 2020-12-12 20:14:50 +00:00
parent de4f3ee4a2
commit df687fe5d4
35 changed files with 58 additions and 281 deletions

View File

@ -8,8 +8,8 @@ import (
"github.com/micro/go-micro/v2/api/handler"
api "github.com/micro/go-micro/v2/api/proto"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/util/ctx"
)

View File

@ -8,8 +8,8 @@ import (
"strings"
api "github.com/micro/go-micro/v2/api/proto"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
"github.com/oxtoacart/bpool"
)

View File

@ -10,7 +10,7 @@ import (
"github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/handler"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/selector"
)
const (

View File

@ -14,7 +14,6 @@ import (
"github.com/micro/go-micro/v2/api/handler"
"github.com/micro/go-micro/v2/api/internal/proto"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/codec/jsonrpc"
"github.com/micro/go-micro/v2/codec/protorpc"
@ -22,6 +21,7 @@ import (
"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/selector"
"github.com/micro/go-micro/v2/util/ctx"
"github.com/micro/go-micro/v2/util/qson"
"github.com/oxtoacart/bpool"

View File

@ -14,9 +14,9 @@ import (
"github.com/gobwas/ws/wsutil"
"github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
raw "github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/selector"
)
// serveWebsocket will stream rpc back over websockets assuming json

View File

@ -13,7 +13,7 @@ import (
"github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/handler"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/selector"
)
const (

View File

@ -13,11 +13,11 @@ import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
raw "github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
pnet "github.com/micro/go-micro/v2/util/net"
"google.golang.org/grpc"

View File

@ -6,10 +6,10 @@ import (
"testing"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/selector"
pgrpc "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

View File

@ -5,9 +5,9 @@ import (
"time"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/transport"
)

View File

@ -8,12 +8,12 @@ import (
"github.com/google/uuid"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/codec"
raw "github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v2/util/buf"
"github.com/micro/go-micro/v2/util/net"

View File

@ -5,10 +5,10 @@ import (
"fmt"
"testing"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/selector"
)
func newTestRegistry() registry.Registry {

View File

@ -10,8 +10,6 @@ import (
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/grpc"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/profile/http"
@ -20,11 +18,11 @@ import (
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/runtime"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store"
"github.com/micro/go-micro/v2/transport"
authutil "github.com/micro/go-micro/v2/util/auth"
"github.com/micro/go-micro/v2/util/wrapper"
// clients
cgrpc "github.com/micro/go-micro/v2/client/grpc"
@ -51,9 +49,9 @@ import (
lRuntime "github.com/micro/go-micro/v2/runtime/local"
// selectors
"github.com/micro/go-micro/v2/client/selector/dns"
"github.com/micro/go-micro/v2/client/selector/router"
"github.com/micro/go-micro/v2/client/selector/static"
"github.com/micro/go-micro/v2/selector/dns"
"github.com/micro/go-micro/v2/selector/router"
"github.com/micro/go-micro/v2/selector/static"
// transports
thttp "github.com/micro/go-micro/v2/transport/http"
@ -417,44 +415,6 @@ func (c *cmd) Before(ctx *cli.Context) error {
var serverOpts []server.Option
var clientOpts []client.Option
// setup a client to use when calling the runtime. It is important the auth client is wrapped
// after the cache client since the wrappers are applied in reverse order and the cache will use
// some of the headers set by the auth client.
authFn := func() auth.Auth { return *c.opts.Auth }
cacheFn := func() *client.Cache { return (*c.opts.Client).Options().Cache }
microClient := wrapper.CacheClient(cacheFn, grpc.NewClient())
microClient = wrapper.AuthClient(authFn, microClient)
// Set the store
if name := ctx.String("store"); len(name) > 0 {
s, ok := c.opts.Stores[name]
if !ok {
return fmt.Errorf("Unsupported store: %s", name)
}
*c.opts.Store = s(store.WithClient(microClient))
}
// Set the runtime
if name := ctx.String("runtime"); len(name) > 0 {
r, ok := c.opts.Runtimes[name]
if !ok {
return fmt.Errorf("Unsupported runtime: %s", name)
}
*c.opts.Runtime = r(runtime.WithClient(microClient))
}
// Set the tracer
if name := ctx.String("tracer"); len(name) > 0 {
r, ok := c.opts.Tracers[name]
if !ok {
return fmt.Errorf("Unsupported tracer: %s", name)
}
*c.opts.Tracer = r()
}
// Set the client
if name := ctx.String("client"); len(name) > 0 {
// only change if we have the client and type differs
@ -471,8 +431,38 @@ func (c *cmd) Before(ctx *cli.Context) error {
}
}
// Set the store
if name := ctx.String("store"); len(name) > 0 {
s, ok := c.opts.Stores[name]
if !ok {
return fmt.Errorf("Unsupported store: %s", name)
}
*c.opts.Store = s(store.WithClient(*c.opts.Client))
}
// Set the runtime
if name := ctx.String("runtime"); len(name) > 0 {
r, ok := c.opts.Runtimes[name]
if !ok {
return fmt.Errorf("Unsupported runtime: %s", name)
}
*c.opts.Runtime = r(runtime.WithClient(*c.opts.Client))
}
// Set the tracer
if name := ctx.String("tracer"); len(name) > 0 {
r, ok := c.opts.Tracers[name]
if !ok {
return fmt.Errorf("Unsupported tracer: %s", name)
}
*c.opts.Tracer = r()
}
// Setup auth
authOpts := []auth.Option{auth.WithClient(microClient)}
authOpts := []auth.Option{auth.WithClient(*c.opts.Client)}
if len(ctx.String("auth_id")) > 0 || len(ctx.String("auth_secret")) > 0 {
authOpts = append(authOpts, auth.Credentials(

View File

@ -6,12 +6,12 @@ import (
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/runtime"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store"
"github.com/micro/go-micro/v2/transport"

View File

@ -14,7 +14,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/client"
cmucp "github.com/micro/go-micro/v2/client/mucp"
rtr "github.com/micro/go-micro/v2/client/selector/router"
"github.com/micro/go-micro/v2/logger"
pbNet "github.com/micro/go-micro/v2/network/proto"
"github.com/micro/go-micro/v2/network/proxy"
@ -23,6 +22,7 @@ import (
"github.com/micro/go-micro/v2/network/tunnel"
bun "github.com/micro/go-micro/v2/network/tunnel/broker"
tun "github.com/micro/go-micro/v2/network/tunnel/transport"
rtr "github.com/micro/go-micro/v2/selector/router"
"github.com/micro/go-micro/v2/server"
smucp "github.com/micro/go-micro/v2/server/mucp"
"github.com/micro/go-micro/v2/transport"

View File

@ -11,7 +11,6 @@ import (
"time"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/errors"
@ -19,6 +18,7 @@ import (
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/network/proxy"
"github.com/micro/go-micro/v2/network/router"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server"
)

View File

@ -8,13 +8,13 @@ import (
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/cmd"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/runtime"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store"
"github.com/micro/go-micro/v2/transport"

View File

@ -13,9 +13,9 @@ import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/cmd"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/transport"
)

View File

@ -6,8 +6,8 @@ import (
"net"
"strconv"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
)
type dnsSelector struct {

View File

@ -4,7 +4,7 @@ import (
"context"
"time"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/selector"
)
// Set the registry cache ttl

View File

@ -2,7 +2,7 @@
package registry
import (
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/selector"
)
// NewSelector returns a new registry selector

View File

@ -6,9 +6,9 @@ import (
"sort"
"sync"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/network/router"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
)
type routerSelector struct {

View File

@ -2,8 +2,8 @@
package static
import (
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
)
// staticSelector is a static selector

View File

@ -8,9 +8,9 @@ import (
"net/http"
"strings"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/selector"
)
// Write sets the status and body on a http ResponseWriter

View File

@ -4,7 +4,7 @@ import (
"errors"
"net/http"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/selector"
)
type roundTripper struct {

View File

@ -2,14 +2,12 @@ package wrapper
import (
"context"
"reflect"
"strings"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/debug/stats"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/server"
)
@ -171,142 +169,3 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac
// call without an auth token
return a.Client.Call(ctx, req, rsp, opts...)
}
// AuthClient wraps requests with the auth header
func AuthClient(auth func() auth.Auth, c client.Client) client.Client {
return &authWrapper{c, auth}
}
// AuthHandler wraps a server handler to perform auth
func AuthHandler(fn func() auth.Auth) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// get the auth.Auth interface
a := fn()
// Check for debug endpoints which should be excluded from auth
if strings.HasPrefix(req.Endpoint(), "Debug.") {
return h(ctx, req, rsp)
}
// Extract the token if present. Note: if noop is being used
// then the token can be blank without erroring
var account *auth.Account
if header, ok := metadata.Get(ctx, "Authorization"); ok {
// Ensure the correct scheme is being used
if !strings.HasPrefix(header, auth.BearerScheme) {
return errors.Unauthorized(req.Service(), "invalid authorization header. expected Bearer schema")
}
// Strip the prefix and inspect the resulting token
account, _ = a.Inspect(strings.TrimPrefix(header, auth.BearerScheme))
}
// Extract the namespace header
ns, ok := metadata.Get(ctx, "Micro-Namespace")
if !ok {
ns = a.Options().Namespace
ctx = metadata.Set(ctx, "Micro-Namespace", ns)
}
// Check the issuer matches the services namespace. TODO: Stop allowing go.micro to access
// any namespace and instead check for the server issuer.
if account != nil && account.Issuer != ns && account.Issuer != "go.micro" {
return errors.Forbidden(req.Service(), "Account was not issued by %v", ns)
}
// construct the resource
res := &auth.Resource{
Type: "service",
Name: req.Service(),
Endpoint: req.Endpoint(),
}
// Verify the caller has access to the resource
err := a.Verify(account, res, auth.VerifyContext(ctx))
if err != nil && account != nil {
return errors.Forbidden(req.Service(), "Forbidden call made to %v:%v by %v", req.Service(), req.Endpoint(), account.ID)
} else if err != nil {
return errors.Unauthorized(req.Service(), "Unauthorized call made to %v:%v", req.Service(), req.Endpoint())
}
// There is an account, set it in the context
if account != nil {
ctx = auth.ContextWithAccount(ctx, account)
}
// The user is authorised, allow the call
return h(ctx, req, rsp)
}
}
}
type cacheWrapper struct {
cacheFn func() *client.Cache
client.Client
}
// Call executes the request. If the CacheExpiry option was set, the response will be cached using
// a hash of the metadata and request as the key.
func (c *cacheWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
// parse the options
var options client.CallOptions
for _, o := range opts {
o(&options)
}
// if the client doesn't have a cacbe setup don't continue
cache := c.cacheFn()
if cache == nil {
return c.Client.Call(ctx, req, rsp, opts...)
}
// if the cache expiry is not set, execute the call without the cache
if options.CacheExpiry == 0 {
return c.Client.Call(ctx, req, rsp, opts...)
}
// if the response is nil don't call the cache since we can't assign the response
if rsp == nil {
return c.Client.Call(ctx, req, rsp, opts...)
}
// check to see if there is a response cached, if there is assign it
if r, ok := cache.Get(ctx, &req); ok {
val := reflect.ValueOf(rsp).Elem()
val.Set(reflect.ValueOf(r).Elem())
return nil
}
// don't cache the result if there was an error
if err := c.Client.Call(ctx, req, rsp, opts...); err != nil {
return err
}
// set the result in the cache
cache.Set(ctx, &req, rsp, options.CacheExpiry)
return nil
}
// CacheClient wraps requests with the cache wrapper
func CacheClient(cacheFn func() *client.Cache, c client.Client) client.Client {
return &cacheWrapper{cacheFn, c}
}
type staticClient struct {
address string
client.Client
}
func (s *staticClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
return s.Client.Call(ctx, req, rsp, append(opts, client.WithAddress(s.address))...)
}
func (s *staticClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
return s.Client.Stream(ctx, req, append(opts, client.WithAddress(s.address))...)
}
// StaticClient sets an address on every call
func StaticClient(address string, c client.Client) client.Client {
return &staticClient{address, c}
}

View File

@ -1,72 +0,0 @@
package wrapper_test
import (
"context"
"testing"
"github.com/micro/go-micro/v2/broker"
bmemory "github.com/micro/go-micro/v2/broker/memory"
"github.com/micro/go-micro/v2/client"
rmemory "github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/server"
tmemory "github.com/micro/go-micro/v2/transport/memory"
wrapper "github.com/micro/go-micro/v2/util/wrapper"
)
type TestFoo struct {
}
type TestReq struct{}
type TestRsp struct {
Data string
}
func (h *TestFoo) Bar(ctx context.Context, req *TestReq, rsp *TestRsp) error {
rsp.Data = "pass"
return nil
}
func TestStaticClientWrapper(t *testing.T) {
var err error
req := client.NewRequest("go.micro.service.foo", "TestFoo.Bar", &TestReq{}, client.WithContentType("application/json"))
rsp := &TestRsp{}
reg := rmemory.NewRegistry()
brk := bmemory.NewBroker(broker.Registry(reg))
tr := tmemory.NewTransport()
srv := server.NewServer(
server.Broker(brk),
server.Registry(reg),
server.Name("go.micro.service.foo"),
server.Address("127.0.0.1:0"),
server.Transport(tr),
)
if err = srv.Handle(srv.NewHandler(&TestFoo{})); err != nil {
t.Fatal(err)
}
if err = srv.Start(); err != nil {
t.Fatal(err)
}
cli := client.NewClient(
client.Registry(reg),
client.Broker(brk),
client.Transport(tr),
)
w1 := wrapper.StaticClient("xxx_localhost:12345", cli)
if err = w1.Call(context.TODO(), req, nil); err == nil {
t.Fatal("address xxx_#localhost:12345 must not exists and call must be failed")
}
w2 := wrapper.StaticClient(srv.Options().Address, cli)
if err = w2.Call(context.TODO(), req, rsp); err != nil {
t.Fatal(err)
} else if rsp.Data != "pass" {
t.Fatalf("something wrong with response: %#+v", rsp)
}
}