From e9fc5b1671d335c1332751969cdeaeeabc8cafeb Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Thu, 30 Jul 2020 15:22:36 +0100 Subject: [PATCH] client: add proxy option (#1885) * client: add proxy option * client: add WithProxy CallOption * use address option * ProxyAddress => Proxy --- client/grpc/grpc.go | 10 ++++++++++ client/mucp/mucp.go | 15 ++++++++++++--- client/options.go | 9 +++++++++ client/util.go | 12 ++++-------- util/net/net.go | 41 ----------------------------------------- util/net/net_test.go | 36 ------------------------------------ 6 files changed, 35 insertions(+), 88 deletions(-) diff --git a/client/grpc/grpc.go b/client/grpc/grpc.go index 2ed71bd9..4ee17387 100644 --- a/client/grpc/grpc.go +++ b/client/grpc/grpc.go @@ -411,6 +411,11 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface callOpts.Selector = g.opts.Selector } + // inject proxy address + if len(callOpts.Address) == 0 && len(g.opts.Proxy) > 0 { + callOpts.Address = []string{g.opts.Proxy} + } + // lookup the route to send the reques to route, err := client.LookupRoute(req, callOpts) if err != nil { @@ -514,6 +519,11 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli callOpts.Selector = g.opts.Selector } + // inject proxy address + if len(callOpts.Address) == 0 && len(g.opts.Proxy) > 0 { + callOpts.Address = []string{g.opts.Proxy} + } + // lookup the route to send the reques to route, err := client.LookupRoute(req, callOpts) if err != nil { diff --git a/client/mucp/mucp.go b/client/mucp/mucp.go index 997f273b..d40c650e 100644 --- a/client/mucp/mucp.go +++ b/client/mucp/mucp.go @@ -17,7 +17,6 @@ import ( "github.com/micro/go-micro/v3/registry" "github.com/micro/go-micro/v3/transport" "github.com/micro/go-micro/v3/util/buf" - "github.com/micro/go-micro/v3/util/net" "github.com/micro/go-micro/v3/util/pool" ) @@ -379,6 +378,11 @@ func (r *rpcClient) Call(ctx context.Context, request client.Request, response i callOpts.Selector = r.opts.Selector } + // inject proxy address + if len(callOpts.Address) == 0 && len(r.opts.Proxy) > 0 { + callOpts.Address = []string{r.opts.Proxy} + } + // lookup the route to send the request via route, err := client.LookupRoute(request, callOpts) if err != nil { @@ -403,7 +407,7 @@ func (r *rpcClient) Call(ctx context.Context, request client.Request, response i retries := callOpts.Retries // disable retries when using a proxy - if _, _, ok := net.Proxy(request.Service(), callOpts.Address); ok { + if len(r.opts.Proxy) > 0 { retries = 0 } @@ -475,6 +479,11 @@ func (r *rpcClient) Stream(ctx context.Context, request client.Request, opts ... callOpts.Selector = r.opts.Selector } + // inject proxy address + if len(callOpts.Address) == 0 && len(r.opts.Proxy) > 0 { + callOpts.Address = []string{r.opts.Proxy} + } + // lookup the route to send the request via route, err := client.LookupRoute(request, callOpts) if err != nil { @@ -504,7 +513,7 @@ func (r *rpcClient) Stream(ctx context.Context, request client.Request, opts ... retries := callOpts.Retries // disable retries when using a proxy - if _, _, ok := net.Proxy(request.Service(), callOpts.Address); ok { + if len(r.opts.Proxy) > 0 { retries = 0 } diff --git a/client/options.go b/client/options.go index 4106393f..4eab7b0c 100644 --- a/client/options.go +++ b/client/options.go @@ -17,6 +17,8 @@ import ( type Options struct { // Used to select codec ContentType string + // Proxy address to send requests via + Proxy string // Plugged interfaces Broker broker.Broker @@ -149,6 +151,13 @@ func ContentType(ct string) Option { } } +// Proxy sets the proxy address +func Proxy(addr string) Option { + return func(o *Options) { + o.Proxy = addr + } +} + // PoolSize sets the connection pool size func PoolSize(d int) Option { return func(o *Options) { diff --git a/client/util.go b/client/util.go index 41946bba..169690e3 100644 --- a/client/util.go +++ b/client/util.go @@ -6,19 +6,15 @@ import ( "github.com/micro/go-micro/v3/errors" "github.com/micro/go-micro/v3/router" "github.com/micro/go-micro/v3/selector" - pnet "github.com/micro/go-micro/v3/util/net" ) // LookupRoute for a request using the router and then choose one using the selector func LookupRoute(req Request, opts CallOptions) (*router.Route, error) { - // check to see if the proxy has been set, if it has we don't need to lookup the routes; net.Proxy - // returns a slice of addresses, so we'll use a random one. Eventually we should to use the - // selector for this. - service, addresses, _ := pnet.Proxy(req.Service(), opts.Address) - if len(addresses) > 0 { + // check to see if an address was provided as a call option + if len(opts.Address) > 0 { return &router.Route{ - Service: service, - Address: addresses[rand.Int()%len(addresses)], + Service: req.Service(), + Address: opts.Address[rand.Int()%len(opts.Address)], }, nil } diff --git a/util/net/net.go b/util/net/net.go index 02d6ad2f..0ce674fa 100644 --- a/util/net/net.go +++ b/util/net/net.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net" - "os" "strconv" "strings" ) @@ -78,43 +77,3 @@ func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, e // why are we here? return nil, fmt.Errorf("unable to bind to %s", addr) } - -// Proxy returns the proxy and the address if it exits -func Proxy(service string, address []string) (string, []string, bool) { - var hasProxy bool - - // get proxy. we parse out address if present - if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 { - // default name - if prx == "service" { - prx = "go.micro.proxy" - address = nil - } - - // check if its an address - if v := strings.Split(prx, ":"); len(v) > 1 { - address = []string{prx} - } - - service = prx - hasProxy = true - - return service, address, hasProxy - } - - if prx := os.Getenv("MICRO_NETWORK"); len(prx) > 0 { - // default name - if prx == "service" { - prx = "go.micro.network" - } - service = prx - hasProxy = true - } - - if prx := os.Getenv("MICRO_NETWORK_ADDRESS"); len(prx) > 0 { - address = []string{prx} - hasProxy = true - } - - return service, address, hasProxy -} diff --git a/util/net/net_test.go b/util/net/net_test.go index 28d490d4..d8516d88 100644 --- a/util/net/net_test.go +++ b/util/net/net_test.go @@ -2,7 +2,6 @@ package net import ( "net" - "os" "testing" ) @@ -25,38 +24,3 @@ func TestListen(t *testing.T) { // Expect addr DO NOT has extra ":" at the end! } - -// TestProxyEnv checks whether we have proxy/network settings in env -func TestProxyEnv(t *testing.T) { - service := "foo" - address := []string{"bar"} - - s, a, ok := Proxy(service, address) - if ok { - t.Fatal("Should not have proxy", s, a, ok) - } - - test := func(key, val, expectSrv, expectAddr string) { - // set env - os.Setenv(key, val) - - s, a, ok := Proxy(service, address) - if !ok { - t.Fatal("Expected proxy") - } - if len(expectSrv) > 0 && s != expectSrv { - t.Fatal("Expected proxy service", expectSrv, "got", s) - } - if len(expectAddr) > 0 { - if len(a) == 0 || a[0] != expectAddr { - t.Fatal("Expected proxy address", expectAddr, "got", a) - } - } - - os.Unsetenv(key) - } - - test("MICRO_PROXY", "service", "go.micro.proxy", "") - test("MICRO_NETWORK", "service", "go.micro.network", "") - test("MICRO_NETWORK_ADDRESS", "10.0.0.1:8081", "", "10.0.0.1:8081") -}