2019-06-03 19:44:43 +02:00
|
|
|
// Package grpc provides a gRPC client
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2020-02-19 10:44:35 +02:00
|
|
|
"net"
|
2020-06-02 18:56:26 +02:00
|
|
|
"reflect"
|
2020-02-19 10:44:35 +02:00
|
|
|
"strings"
|
2020-02-19 01:05:38 +02:00
|
|
|
"sync/atomic"
|
2019-06-03 19:44:43 +02:00
|
|
|
"time"
|
|
|
|
|
2020-01-30 13:39:00 +02:00
|
|
|
"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"
|
2020-04-21 16:54:40 +02:00
|
|
|
pnet "github.com/micro/go-micro/v2/util/net"
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/encoding"
|
|
|
|
gmetadata "google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
type grpcClient struct {
|
|
|
|
opts client.Options
|
|
|
|
pool *pool
|
2020-02-19 01:05:38 +02:00
|
|
|
once atomic.Value
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-06-18 19:51:52 +02:00
|
|
|
encoding.RegisterCodec(wrapCodec{jsonCodec{}})
|
2019-06-27 14:08:06 +02:00
|
|
|
encoding.RegisterCodec(wrapCodec{protoCodec{}})
|
2019-06-18 19:51:52 +02:00
|
|
|
encoding.RegisterCodec(wrapCodec{bytesCodec{}})
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// secure returns the dial option for whether its a secure or insecure connection
|
2020-02-19 10:44:35 +02:00
|
|
|
func (g *grpcClient) secure(addr string) grpc.DialOption {
|
|
|
|
// first we check if theres'a tls config
|
2019-06-03 19:44:43 +02:00
|
|
|
if g.opts.Context != nil {
|
|
|
|
if v := g.opts.Context.Value(tlsAuth{}); v != nil {
|
|
|
|
tls := v.(*tls.Config)
|
|
|
|
creds := credentials.NewTLS(tls)
|
2020-02-19 10:44:35 +02:00
|
|
|
// return tls config if it exists
|
2019-06-03 19:44:43 +02:00
|
|
|
return grpc.WithTransportCredentials(creds)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 10:44:35 +02:00
|
|
|
|
|
|
|
// default config
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
defaultCreds := grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
|
|
|
|
|
|
|
|
// check if the address is prepended with https
|
|
|
|
if strings.HasPrefix(addr, "https://") {
|
|
|
|
return defaultCreds
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no port is specified or port is 443 default to tls
|
|
|
|
_, port, err := net.SplitHostPort(addr)
|
|
|
|
// assuming with no port its going to be secured
|
|
|
|
if port == "443" {
|
|
|
|
return defaultCreds
|
|
|
|
} else if err != nil && strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return defaultCreds
|
|
|
|
}
|
|
|
|
|
|
|
|
// other fallback to insecure
|
2019-06-03 19:44:43 +02:00
|
|
|
return grpc.WithInsecure()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) next(request client.Request, opts client.CallOptions) (selector.Next, error) {
|
2020-04-21 16:54:40 +02:00
|
|
|
service, address, _ := pnet.Proxy(request.Service(), opts.Address)
|
2019-06-08 20:40:44 +02:00
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
// return remote address
|
2020-04-21 16:54:40 +02:00
|
|
|
if len(address) > 0 {
|
2019-06-03 19:44:43 +02:00
|
|
|
return func() (*registry.Node, error) {
|
|
|
|
return ®istry.Node{
|
2020-04-21 16:54:40 +02:00
|
|
|
Address: address[0],
|
2019-06-03 19:44:43 +02:00
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get next nodes from the selector
|
2019-11-30 23:20:33 +02:00
|
|
|
next, err := g.opts.Selector.Select(service, opts.SelectOptions...)
|
2019-08-11 04:14:41 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == selector.ErrNotFound {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", "service %s: %s", service, err.Error())
|
|
|
|
}
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", "error selecting %s node: %s", service, err.Error())
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) call(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
2020-02-24 16:15:20 +02:00
|
|
|
var header map[string]string
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
address := node.Address
|
|
|
|
|
2020-02-24 16:15:20 +02:00
|
|
|
header = make(map[string]string)
|
2019-06-03 19:44:43 +02:00
|
|
|
if md, ok := metadata.FromContext(ctx); ok {
|
2020-02-24 16:15:20 +02:00
|
|
|
header = make(map[string]string, len(md))
|
2019-06-03 19:44:43 +02:00
|
|
|
for k, v := range md {
|
2020-03-10 18:47:01 +02:00
|
|
|
header[strings.ToLower(k)] = v
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
2020-02-24 16:15:20 +02:00
|
|
|
} else {
|
|
|
|
header = make(map[string]string)
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout in nanoseconds
|
|
|
|
header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout)
|
|
|
|
// set the content type for the request
|
|
|
|
header["x-content-type"] = req.ContentType()
|
2020-03-10 18:47:01 +02:00
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
md := gmetadata.New(header)
|
|
|
|
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
|
|
|
|
|
|
|
cf, err := g.newGRPCCodec(req.ContentType())
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRecvMsgSize := g.maxRecvMsgSizeValue()
|
|
|
|
maxSendMsgSize := g.maxSendMsgSizeValue()
|
|
|
|
|
|
|
|
var grr error
|
2019-10-10 07:55:16 +02:00
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
grpcDialOptions := []grpc.DialOption{
|
|
|
|
grpc.WithTimeout(opts.DialTimeout),
|
2020-02-19 10:44:35 +02:00
|
|
|
g.secure(address),
|
2019-06-03 19:44:43 +02:00
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
|
|
|
grpc.MaxCallSendMsgSize(maxSendMsgSize),
|
2019-10-09 08:45:51 +02:00
|
|
|
),
|
|
|
|
}
|
2019-10-10 07:55:16 +02:00
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
if opts := g.getGrpcDialOptions(); opts != nil {
|
|
|
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, err := g.pool.getConn(address, grpcDialOptions...)
|
2019-06-03 19:44:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// defer execution of release
|
|
|
|
g.pool.release(address, cc, grr)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ch := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2020-05-01 15:59:50 +02:00
|
|
|
grpcCallOptions := []grpc.CallOption{
|
|
|
|
grpc.ForceCodec(cf),
|
|
|
|
grpc.CallContentSubtype(cf.Name())}
|
2019-10-09 08:45:51 +02:00
|
|
|
if opts := g.getGrpcCallOptions(); opts != nil {
|
|
|
|
grpcCallOptions = append(grpcCallOptions, opts...)
|
|
|
|
}
|
|
|
|
err := cc.Invoke(ctx, methodToGRPC(req.Service(), req.Endpoint()), req.Body(), rsp, grpcCallOptions...)
|
2019-06-03 19:44:43 +02:00
|
|
|
ch <- microError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
grr = err
|
|
|
|
case <-ctx.Done():
|
2020-02-05 23:13:14 +02:00
|
|
|
grr = errors.Timeout("go.micro.client", "%v", ctx.Err())
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return grr
|
|
|
|
}
|
|
|
|
|
2020-06-02 18:56:26 +02:00
|
|
|
func (g *grpcClient) stream(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
2020-02-24 16:15:20 +02:00
|
|
|
var header map[string]string
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
address := node.Address
|
|
|
|
|
|
|
|
if md, ok := metadata.FromContext(ctx); ok {
|
2020-02-24 16:15:20 +02:00
|
|
|
header = make(map[string]string, len(md))
|
2019-06-03 19:44:43 +02:00
|
|
|
for k, v := range md {
|
|
|
|
header[k] = v
|
|
|
|
}
|
2020-02-24 16:15:20 +02:00
|
|
|
} else {
|
|
|
|
header = make(map[string]string)
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout in nanoseconds
|
2020-04-01 00:22:11 +02:00
|
|
|
if opts.StreamTimeout > time.Duration(0) {
|
|
|
|
header["timeout"] = fmt.Sprintf("%d", opts.StreamTimeout)
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
// set the content type for the request
|
|
|
|
header["x-content-type"] = req.ContentType()
|
|
|
|
|
|
|
|
md := gmetadata.New(header)
|
|
|
|
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
|
|
|
|
|
|
|
cf, err := g.newGRPCCodec(req.ContentType())
|
|
|
|
if err != nil {
|
2020-06-02 18:56:26 +02:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var dialCtx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if opts.DialTimeout >= 0 {
|
|
|
|
dialCtx, cancel = context.WithTimeout(ctx, opts.DialTimeout)
|
|
|
|
} else {
|
|
|
|
dialCtx, cancel = context.WithCancel(ctx)
|
|
|
|
}
|
|
|
|
defer cancel()
|
2019-06-17 21:05:58 +02:00
|
|
|
|
|
|
|
wc := wrapCodec{cf}
|
2019-10-10 07:55:16 +02:00
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
grpcDialOptions := []grpc.DialOption{
|
2020-02-13 20:51:32 +02:00
|
|
|
grpc.WithTimeout(opts.DialTimeout),
|
2020-02-19 10:44:35 +02:00
|
|
|
g.secure(address),
|
2019-10-09 08:45:51 +02:00
|
|
|
}
|
2019-10-10 07:55:16 +02:00
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
if opts := g.getGrpcDialOptions(); opts != nil {
|
|
|
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
|
|
|
}
|
2019-06-17 21:05:58 +02:00
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
cc, err := grpc.DialContext(dialCtx, address, grpcDialOptions...)
|
2019-06-03 19:44:43 +02:00
|
|
|
if err != nil {
|
2020-06-02 18:56:26 +02:00
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
desc := &grpc.StreamDesc{
|
|
|
|
StreamName: req.Service() + req.Endpoint(),
|
|
|
|
ClientStreams: true,
|
|
|
|
ServerStreams: true,
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:59:50 +02:00
|
|
|
grpcCallOptions := []grpc.CallOption{
|
|
|
|
grpc.ForceCodec(wc),
|
|
|
|
grpc.CallContentSubtype(cf.Name()),
|
|
|
|
}
|
2019-10-09 08:45:51 +02:00
|
|
|
if opts := g.getGrpcCallOptions(); opts != nil {
|
|
|
|
grpcCallOptions = append(grpcCallOptions, opts...)
|
|
|
|
}
|
2020-01-09 19:00:14 +02:00
|
|
|
|
2020-01-20 00:53:56 +02:00
|
|
|
// create a new cancelling context
|
|
|
|
newCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
st, err := cc.NewStream(newCtx, desc, methodToGRPC(req.Service(), req.Endpoint()), grpcCallOptions...)
|
2019-06-03 19:44:43 +02:00
|
|
|
if err != nil {
|
2020-02-15 13:35:08 +02:00
|
|
|
// we need to cleanup as we dialled and created a context
|
|
|
|
// cancel the context
|
|
|
|
cancel()
|
|
|
|
// close the connection
|
|
|
|
cc.Close()
|
|
|
|
// now return the error
|
2020-06-02 18:56:26 +02:00
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error creating stream: %v", err))
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 19:51:52 +02:00
|
|
|
codec := &grpcCodec{
|
|
|
|
s: st,
|
|
|
|
c: wc,
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:05:58 +02:00
|
|
|
// set request codec
|
|
|
|
if r, ok := req.(*grpcRequest); ok {
|
2019-06-18 19:51:52 +02:00
|
|
|
r.codec = codec
|
2019-06-17 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:56:26 +02:00
|
|
|
// setup the stream response
|
|
|
|
stream := &grpcStream{
|
|
|
|
context: ctx,
|
|
|
|
request: req,
|
|
|
|
response: &response{
|
|
|
|
conn: cc,
|
|
|
|
stream: st,
|
|
|
|
codec: cf,
|
|
|
|
gcodec: codec,
|
|
|
|
},
|
2019-06-08 20:40:44 +02:00
|
|
|
stream: st,
|
2020-06-02 18:56:26 +02:00
|
|
|
conn: cc,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the stream as the response
|
|
|
|
val := reflect.ValueOf(rsp).Elem()
|
|
|
|
val.Set(reflect.ValueOf(stream).Elem())
|
|
|
|
return nil
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
2019-12-27 14:25:58 +02:00
|
|
|
func (g *grpcClient) poolMaxStreams() int {
|
|
|
|
if g.opts.Context == nil {
|
|
|
|
return DefaultPoolMaxStreams
|
|
|
|
}
|
|
|
|
v := g.opts.Context.Value(poolMaxStreams{})
|
|
|
|
if v == nil {
|
|
|
|
return DefaultPoolMaxStreams
|
|
|
|
}
|
|
|
|
return v.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) poolMaxIdle() int {
|
|
|
|
if g.opts.Context == nil {
|
|
|
|
return DefaultPoolMaxIdle
|
|
|
|
}
|
|
|
|
v := g.opts.Context.Value(poolMaxIdle{})
|
|
|
|
if v == nil {
|
|
|
|
return DefaultPoolMaxIdle
|
|
|
|
}
|
|
|
|
return v.(int)
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
func (g *grpcClient) maxRecvMsgSizeValue() int {
|
|
|
|
if g.opts.Context == nil {
|
|
|
|
return DefaultMaxRecvMsgSize
|
|
|
|
}
|
|
|
|
v := g.opts.Context.Value(maxRecvMsgSizeKey{})
|
|
|
|
if v == nil {
|
|
|
|
return DefaultMaxRecvMsgSize
|
|
|
|
}
|
|
|
|
return v.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) maxSendMsgSizeValue() int {
|
|
|
|
if g.opts.Context == nil {
|
|
|
|
return DefaultMaxSendMsgSize
|
|
|
|
}
|
|
|
|
v := g.opts.Context.Value(maxSendMsgSizeKey{})
|
|
|
|
if v == nil {
|
|
|
|
return DefaultMaxSendMsgSize
|
|
|
|
}
|
|
|
|
return v.(int)
|
|
|
|
}
|
|
|
|
|
2019-06-11 10:52:35 +02:00
|
|
|
func (g *grpcClient) newGRPCCodec(contentType string) (encoding.Codec, error) {
|
2019-06-03 19:44:43 +02:00
|
|
|
codecs := make(map[string]encoding.Codec)
|
|
|
|
if g.opts.Context != nil {
|
|
|
|
if v := g.opts.Context.Value(codecsKey{}); v != nil {
|
|
|
|
codecs = v.(map[string]encoding.Codec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c, ok := codecs[contentType]; ok {
|
|
|
|
return wrapCodec{c}, nil
|
|
|
|
}
|
|
|
|
if c, ok := defaultGRPCCodecs[contentType]; ok {
|
|
|
|
return wrapCodec{c}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) Init(opts ...client.Option) error {
|
|
|
|
size := g.opts.PoolSize
|
|
|
|
ttl := g.opts.PoolTTL
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&g.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update pool configuration if the options changed
|
|
|
|
if size != g.opts.PoolSize || ttl != g.opts.PoolTTL {
|
|
|
|
g.pool.Lock()
|
|
|
|
g.pool.size = g.opts.PoolSize
|
|
|
|
g.pool.ttl = int64(g.opts.PoolTTL.Seconds())
|
|
|
|
g.pool.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) Options() client.Options {
|
|
|
|
return g.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
|
2019-07-07 13:44:09 +02:00
|
|
|
return newGRPCEvent(topic, msg, g.opts.ContentType, opts...)
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) NewRequest(service, method string, req interface{}, reqOpts ...client.RequestOption) client.Request {
|
|
|
|
return newGRPCRequest(service, method, req, g.opts.ContentType, reqOpts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2020-02-26 20:34:40 +02:00
|
|
|
if req == nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", "req is nil")
|
|
|
|
} else if rsp == nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", "rsp is nil")
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := g.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
next, err := g.next(req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
|
|
|
// no deadline so we create a new one
|
2019-12-03 09:25:58 +02:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
|
|
|
|
defer cancel()
|
2019-06-03 19:44:43 +02:00
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
|
|
|
opt := client.WithRequestTimeout(time.Until(d))
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// make copy of call method
|
|
|
|
gcall := g.call
|
|
|
|
|
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
gcall = callOpts.CallWrappers[i-1](gcall)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return errors.New("go.micro.client", "request timeout", 408)
|
|
|
|
call := func(i int) error {
|
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, err := callOpts.Backoff(ctx, req, i)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// select next node
|
|
|
|
node, err := next()
|
2019-08-11 04:14:41 +02:00
|
|
|
service := req.Service()
|
|
|
|
if err != nil {
|
|
|
|
if err == selector.ErrNotFound {
|
|
|
|
return errors.InternalServerError("go.micro.client", "service %s: %s", service, err.Error())
|
|
|
|
}
|
|
|
|
return errors.InternalServerError("go.micro.client", "error selecting %s node: %s", service, err.Error())
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make the call
|
|
|
|
err = gcall(ctx, node, req, rsp, callOpts)
|
2020-03-17 13:27:20 +02:00
|
|
|
g.opts.Selector.Mark(service, node, err)
|
2020-03-16 12:33:38 +02:00
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, callOpts.Retries+1)
|
|
|
|
var gerr error
|
|
|
|
|
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2019-06-27 07:06:53 +02:00
|
|
|
go func(i int) {
|
2019-06-03 19:44:43 +02:00
|
|
|
ch <- call(i)
|
2019-06-27 07:06:53 +02:00
|
|
|
}(i)
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case err := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := g.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
next, err := g.next(req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// #200 - streams shouldn't have a request timeout set on the context
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-06-02 18:56:26 +02:00
|
|
|
// make a copy of stream
|
|
|
|
gstream := g.stream
|
|
|
|
|
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
gstream = callOpts.CallWrappers[i-1](gstream)
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
call := func(i int) (client.Stream, error) {
|
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, err := callOpts.Backoff(ctx, req, i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := next()
|
2019-08-11 04:14:41 +02:00
|
|
|
service := req.Service()
|
|
|
|
if err != nil {
|
|
|
|
if err == selector.ErrNotFound {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", "service %s: %s", service, err.Error())
|
|
|
|
}
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", "error selecting %s node: %s", service, err.Error())
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:56:26 +02:00
|
|
|
// make the call
|
|
|
|
stream := &grpcStream{}
|
|
|
|
err = g.stream(ctx, node, req, stream, callOpts)
|
|
|
|
|
2019-08-11 04:14:41 +02:00
|
|
|
g.opts.Selector.Mark(service, node, err)
|
2019-06-03 19:44:43 +02:00
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
stream client.Stream
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan response, callOpts.Retries+1)
|
|
|
|
var grr error
|
|
|
|
|
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2019-06-27 07:06:53 +02:00
|
|
|
go func(i int) {
|
2019-06-03 19:44:43 +02:00
|
|
|
s, err := call(i)
|
|
|
|
ch <- response{s, err}
|
2019-06-27 07:06:53 +02:00
|
|
|
}(i)
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case rsp := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if rsp.err == nil {
|
|
|
|
return rsp.stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return nil, rsp.err
|
|
|
|
}
|
|
|
|
|
|
|
|
grr = rsp.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, grr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
2019-11-25 18:31:43 +02:00
|
|
|
var options client.PublishOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
md, ok := metadata.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = make(map[string]string)
|
|
|
|
}
|
|
|
|
md["Content-Type"] = p.ContentType()
|
2019-11-25 18:31:43 +02:00
|
|
|
md["Micro-Topic"] = p.Topic()
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-08-18 12:28:21 +02:00
|
|
|
cf, err := g.newGRPCCodec(p.ContentType())
|
2019-06-03 19:44:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-11-25 18:31:43 +02:00
|
|
|
var body []byte
|
|
|
|
|
|
|
|
// passed in raw data
|
|
|
|
if d, ok := p.Payload().(*raw.Frame); ok {
|
|
|
|
body = d.Data
|
|
|
|
} else {
|
|
|
|
// set the body
|
|
|
|
b, err := cf.Marshal(p.Payload())
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
body = b
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:05:38 +02:00
|
|
|
if !g.once.Load().(bool) {
|
|
|
|
if err = g.opts.Broker.Connect(); err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
g.once.Store(true)
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-11-25 18:31:43 +02:00
|
|
|
topic := p.Topic()
|
|
|
|
|
|
|
|
// get the exchange
|
|
|
|
if len(options.Exchange) > 0 {
|
|
|
|
topic = options.Exchange
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.opts.Broker.Publish(topic, &broker.Message{
|
2019-06-03 19:44:43 +02:00
|
|
|
Header: md,
|
2019-11-25 18:31:43 +02:00
|
|
|
Body: body,
|
2020-04-28 18:29:00 +02:00
|
|
|
}, broker.PublishContext(options.Context))
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) String() string {
|
|
|
|
return "grpc"
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:45:51 +02:00
|
|
|
func (g *grpcClient) getGrpcDialOptions() []grpc.DialOption {
|
2019-10-10 07:55:16 +02:00
|
|
|
if g.opts.CallOptions.Context == nil {
|
2019-10-09 08:45:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-10 07:55:16 +02:00
|
|
|
v := g.opts.CallOptions.Context.Value(grpcDialOptions{})
|
2019-10-09 08:45:51 +02:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, ok := v.([]grpc.DialOption)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption {
|
|
|
|
if g.opts.CallOptions.Context == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v := g.opts.CallOptions.Context.Value(grpcCallOptions{})
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, ok := v.([]grpc.CallOption)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
func newClient(opts ...client.Option) client.Client {
|
2019-12-29 23:07:55 +02:00
|
|
|
options := client.NewOptions()
|
|
|
|
// default content type for grpc
|
|
|
|
options.ContentType = "application/grpc+proto"
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
rc := &grpcClient{
|
|
|
|
opts: options,
|
|
|
|
}
|
2020-02-19 01:05:38 +02:00
|
|
|
rc.once.Store(false)
|
2019-12-29 23:07:55 +02:00
|
|
|
|
2019-12-27 14:25:58 +02:00
|
|
|
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
c := client.Client(rc)
|
|
|
|
|
|
|
|
// wrap in reverse
|
|
|
|
for i := len(options.Wrappers); i > 0; i-- {
|
|
|
|
c = options.Wrappers[i-1](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(opts ...client.Option) client.Client {
|
|
|
|
return newClient(opts...)
|
|
|
|
}
|