client: add proxy option (#1885)

* client: add proxy option

* client: add WithProxy CallOption

* use address option

* ProxyAddress => Proxy
This commit is contained in:
ben-toogood
2020-07-30 15:22:36 +01:00
committed by GitHub
parent 006bbefaf3
commit e9fc5b1671
6 changed files with 35 additions and 88 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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
}