From d7ecb58f6cf60d9bed319f8d6850241f13d24eea Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 21 Apr 2020 15:54:40 +0100 Subject: [PATCH] Add network proxying (#1556) * Add network proxying * go fmt --- client/grpc/grpc.go | 22 ++++----------------- client/rpc_client.go | 46 +++++++++----------------------------------- util/net/net.go | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 55 deletions(-) diff --git a/client/grpc/grpc.go b/client/grpc/grpc.go index 30dd97f8..d41d977e 100644 --- a/client/grpc/grpc.go +++ b/client/grpc/grpc.go @@ -6,7 +6,6 @@ import ( "crypto/tls" "fmt" "net" - "os" "strings" "sync/atomic" "time" @@ -20,6 +19,7 @@ import ( "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/util/config" + pnet "github.com/micro/go-micro/v2/util/net" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -74,27 +74,13 @@ func (g *grpcClient) secure(addr string) grpc.DialOption { } func (g *grpcClient) next(request client.Request, opts client.CallOptions) (selector.Next, error) { - service := request.Service() - - // get proxy - if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 { - // default name - if prx == "service" { - prx = "go.micro.proxy" - } - service = prx - } - - // get proxy address - if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { - opts.Address = []string{prx} - } + service, address, _ := pnet.Proxy(request.Service(), opts.Address) // return remote address - if len(opts.Address) > 0 { + if len(address) > 0 { return func() (*registry.Node, error) { return ®istry.Node{ - Address: opts.Address[0], + Address: address[0], }, nil }, nil } diff --git a/client/rpc_client.go b/client/rpc_client.go index ea68bfb2..19a18bdb 100644 --- a/client/rpc_client.go +++ b/client/rpc_client.go @@ -3,7 +3,6 @@ package client import ( "context" "fmt" - "os" "sync/atomic" "time" @@ -17,6 +16,7 @@ import ( "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/transport" "github.com/micro/go-micro/v2/util/buf" + "github.com/micro/go-micro/v2/util/net" "github.com/micro/go-micro/v2/util/pool" ) @@ -322,46 +322,18 @@ func (r *rpcClient) Options() Options { return r.opts } -// hasProxy checks if we have proxy set in the environment -func (r *rpcClient) hasProxy() bool { - // get proxy - if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 { - return true - } - - // get proxy address - if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { - return true - } - - return false -} - // next returns an iterator for the next nodes to call func (r *rpcClient) next(request Request, opts CallOptions) (selector.Next, error) { - service := request.Service() - - // get proxy - if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 { - // default name - if prx == "service" { - prx = "go.micro.proxy" - } - service = prx - } - - // get proxy address - if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { - opts.Address = []string{prx} - } + // try get the proxy + service, address, _ := net.Proxy(request.Service(), opts.Address) // return remote address - if len(opts.Address) > 0 { - nodes := make([]*registry.Node, len(opts.Address)) + if len(address) > 0 { + nodes := make([]*registry.Node, len(address)) - for i, address := range opts.Address { + for i, addr := range address { nodes[i] = ®istry.Node{ - Address: address, + Address: addr, // Set the protocol Metadata: map[string]string{ "protocol": "mucp", @@ -461,7 +433,7 @@ func (r *rpcClient) Call(ctx context.Context, request Request, response interfac retries := callOpts.Retries // disable retries when using a proxy - if r.hasProxy() { + if _, _, ok := net.Proxy(request.Service(), callOpts.Address); ok { retries = 0 } @@ -552,7 +524,7 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, opts ...CallOpt retries := callOpts.Retries // disable retries when using a proxy - if r.hasProxy() { + if _, _, ok := net.Proxy(request.Service(), callOpts.Address); ok { retries = 0 } diff --git a/util/net/net.go b/util/net/net.go index 0ce674fa..6a18aca7 100644 --- a/util/net/net.go +++ b/util/net/net.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net" + "os" "strconv" "strings" ) @@ -77,3 +78,40 @@ 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 + if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 { + // default name + if prx == "service" { + prx = "go.micro.proxy" + } + service = prx + hasProxy = true + } + + // get proxy address + if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { + address = []string{prx} + hasProxy = true + } + + 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_NEWORK_ADDRESS"); len(prx) > 0 { + address = []string{prx} + hasProxy = true + } + + return service, address, hasProxy +}