|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
// Package grpc provides a gRPC client for micro framework
|
|
|
|
|
package grpc
|
|
|
|
|
// Package grpc provides a gRPC client
|
|
|
|
|
package grpc // import "go.unistack.org/micro-client-grpc/v3"
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
@@ -8,7 +8,6 @@ import (
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
@@ -18,17 +17,12 @@ import (
|
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
|
"go.unistack.org/micro/v3/options"
|
|
|
|
|
"go.unistack.org/micro/v3/selector"
|
|
|
|
|
"go.unistack.org/micro/v3/semconv"
|
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
"google.golang.org/grpc/encoding"
|
|
|
|
|
gmetadata "google.golang.org/grpc/metadata"
|
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
@@ -36,10 +30,6 @@ const (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type grpcClient struct {
|
|
|
|
|
funcPublish client.FuncPublish
|
|
|
|
|
funcBatchPublish client.FuncBatchPublish
|
|
|
|
|
funcCall client.FuncCall
|
|
|
|
|
funcStream client.FuncStream
|
|
|
|
|
pool *ConnPool
|
|
|
|
|
opts client.Options
|
|
|
|
|
sync.RWMutex
|
|
|
|
@@ -103,7 +93,7 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|
|
|
|
|
|
|
|
|
cf, err := g.newCodec(req.ContentType())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxRecvMsgSize := g.maxRecvMsgSizeValue()
|
|
|
|
@@ -130,9 +120,6 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|
|
|
|
grpc.WithDefaultServiceConfig(cfgService),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts := g.getGrpcDialOptions(g.opts.Context); opts != nil {
|
|
|
|
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
|
|
|
|
}
|
|
|
|
|
if opts := g.getGrpcDialOptions(opts.Context); opts != nil {
|
|
|
|
|
grpcDialOptions = append(grpcDialOptions, opts...)
|
|
|
|
|
}
|
|
|
|
@@ -147,7 +134,7 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
|
|
|
|
|
|
|
|
|
cc, err := g.pool.Get(dialCtx, addr, grpcDialOptions...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "Error sending request: %v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
// defer execution of release
|
|
|
|
@@ -217,7 +204,7 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|
|
|
|
|
|
|
|
|
cf, err := g.newCodec(req.ContentType())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dialCtx context.Context
|
|
|
|
@@ -258,7 +245,7 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|
|
|
|
|
|
|
|
|
cc, err := g.pool.Get(dialCtx, addr, grpcDialOptions...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "Error sending request: %v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
desc := &grpc.StreamDesc{
|
|
|
|
@@ -291,7 +278,7 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
|
|
|
|
// release the connection
|
|
|
|
|
g.pool.Put(cc, err)
|
|
|
|
|
// now return the error
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "Error creating stream: %v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error creating stream: %v", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set request codec
|
|
|
|
@@ -416,23 +403,26 @@ func (g *grpcClient) Init(opts ...client.Option) error {
|
|
|
|
|
g.pool.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.funcCall = g.fnCall
|
|
|
|
|
g.funcStream = g.fnStream
|
|
|
|
|
g.funcPublish = g.fnPublish
|
|
|
|
|
g.funcBatchPublish = g.fnBatchPublish
|
|
|
|
|
|
|
|
|
|
g.opts.Hooks.EachPrev(func(hook options.Hook) {
|
|
|
|
|
switch h := hook.(type) {
|
|
|
|
|
case client.HookCall:
|
|
|
|
|
g.funcCall = h(g.funcCall)
|
|
|
|
|
case client.HookStream:
|
|
|
|
|
g.funcStream = h(g.funcStream)
|
|
|
|
|
case client.HookPublish:
|
|
|
|
|
g.funcPublish = h(g.funcPublish)
|
|
|
|
|
case client.HookBatchPublish:
|
|
|
|
|
g.funcBatchPublish = h(g.funcBatchPublish)
|
|
|
|
|
if err := g.opts.Broker.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if err := g.opts.Tracer.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := g.opts.Router.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := g.opts.Logger.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := g.opts.Meter.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := g.opts.Transport.Init(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.init = true
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
@@ -455,32 +445,6 @@ func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface
|
|
|
|
|
} else if rsp == nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "rsp is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
|
|
|
|
var sp tracer.Span
|
|
|
|
|
ctx, sp = g.opts.Tracer.Start(ctx, req.Endpoint()+" rpc-client",
|
|
|
|
|
tracer.WithSpanKind(tracer.SpanKindClient),
|
|
|
|
|
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
|
|
|
|
)
|
|
|
|
|
err := g.funcCall(ctx, req, rsp, opts...)
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Dec()
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
g.opts.Meter.Summary(semconv.ClientRequestLatencyMicroseconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
g.opts.Meter.Histogram(semconv.ClientRequestDurationSeconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
|
|
|
|
|
if me := errors.FromError(err); me == nil {
|
|
|
|
|
sp.Finish()
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "success", "code", strconv.Itoa(int(200))).Inc()
|
|
|
|
|
} else {
|
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "failure", "code", strconv.Itoa(int(me.Code))).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
|
// make a copy of call opts
|
|
|
|
|
callOpts := g.opts.CallOptions
|
|
|
|
|
|
|
|
|
@@ -512,6 +476,11 @@ func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interfa
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
|
callOpts.Router = g.opts.Router
|
|
|
|
@@ -533,7 +502,7 @@ func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interfa
|
|
|
|
|
// 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", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
@@ -548,7 +517,7 @@ func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interfa
|
|
|
|
|
// TODO apply any filtering here
|
|
|
|
|
routes, err = g.opts.Lookup(ctx, req, callOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
@@ -611,31 +580,6 @@ func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interfa
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
|
|
|
|
var sp tracer.Span
|
|
|
|
|
ctx, sp = g.opts.Tracer.Start(ctx, req.Endpoint()+" rpc-client",
|
|
|
|
|
tracer.WithSpanKind(tracer.SpanKindClient),
|
|
|
|
|
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
|
|
|
|
)
|
|
|
|
|
stream, err := g.funcStream(ctx, req, opts...)
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Dec()
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
g.opts.Meter.Summary(semconv.ClientRequestLatencyMicroseconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
g.opts.Meter.Histogram(semconv.ClientRequestDurationSeconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
|
|
|
|
|
if me := status.Convert(err); me == nil {
|
|
|
|
|
sp.Finish()
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
|
|
|
|
} else {
|
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
|
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "failure", "code", strconv.Itoa(int(me.Code()))).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stream, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) fnStream(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 {
|
|
|
|
@@ -654,6 +598,11 @@ func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...c
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
|
callOpts.Router = g.opts.Router
|
|
|
|
@@ -675,7 +624,7 @@ func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...c
|
|
|
|
|
// 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", "%+v", err)
|
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
@@ -690,7 +639,7 @@ func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...c
|
|
|
|
|
// TODO apply any filtering here
|
|
|
|
|
routes, err = g.opts.Lookup(ctx, req, callOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
@@ -764,18 +713,10 @@ func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) BatchPublish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
|
|
|
|
return g.funcBatchPublish(ctx, ps, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) fnBatchPublish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
|
|
|
|
return g.publish(ctx, ps, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
|
|
|
|
return g.funcPublish(ctx, p, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *grpcClient) fnPublish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
|
|
|
|
return g.publish(ctx, []client.Message{p}, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -789,10 +730,6 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|
|
|
|
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
|
|
|
|
exchange = v
|
|
|
|
|
}
|
|
|
|
|
// get the exchange
|
|
|
|
|
if len(options.Exchange) > 0 {
|
|
|
|
|
exchange = options.Exchange
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgs := make([]*broker.Message, 0, len(ps))
|
|
|
|
|
|
|
|
|
@@ -804,16 +741,6 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|
|
|
|
for _, p := range ps {
|
|
|
|
|
md := metadata.Copy(omd)
|
|
|
|
|
md[metadata.HeaderContentType] = p.ContentType()
|
|
|
|
|
topic := p.Topic()
|
|
|
|
|
if len(exchange) > 0 {
|
|
|
|
|
topic = exchange
|
|
|
|
|
}
|
|
|
|
|
md.Set(metadata.HeaderTopic, topic)
|
|
|
|
|
iter := p.Metadata().Iterator()
|
|
|
|
|
var k, v string
|
|
|
|
|
for iter.Next(&k, &v) {
|
|
|
|
|
md.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// passed in raw data
|
|
|
|
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
|
|
|
@@ -822,15 +749,25 @@ func (g *grpcClient) publish(ctx context.Context, ps []client.Message, opts ...c
|
|
|
|
|
// use codec for payload
|
|
|
|
|
cf, err := g.newCodec(p.ContentType())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
// set the body
|
|
|
|
|
b, err := cf.Marshal(p.Payload())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.InternalServerError("go.micro.client", "%+v", err)
|
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
|
}
|
|
|
|
|
body = b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topic := p.Topic()
|
|
|
|
|
if len(exchange) > 0 {
|
|
|
|
|
topic = exchange
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k, v := range p.Metadata() {
|
|
|
|
|
md.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
md.Set(metadata.HeaderTopic, topic)
|
|
|
|
|
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -899,16 +836,22 @@ func NewClient(opts ...client.Option) client.Client {
|
|
|
|
|
options.ContentType = DefaultContentType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c := &grpcClient{
|
|
|
|
|
rc := &grpcClient{
|
|
|
|
|
opts: options,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.pool = NewConnPool(options.PoolSize, options.PoolTTL, c.poolMaxIdle(), c.poolMaxStreams())
|
|
|
|
|
rc.pool = NewConnPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())
|
|
|
|
|
c := client.Client(rc)
|
|
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
|
if codecs, ok := c.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
|
|
|
|
// wrap in reverse
|
|
|
|
|
for i := len(options.Wrappers); i > 0; i-- {
|
|
|
|
|
c = options.Wrappers[i-1](c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rc.opts.Context != nil {
|
|
|
|
|
if codecs, ok := rc.opts.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && codecs != nil {
|
|
|
|
|
for k, v := range codecs {
|
|
|
|
|
c.opts.Codecs[k] = &wrapGrpcCodec{v}
|
|
|
|
|
rc.opts.Codecs[k] = &wrapGrpcCodec{v}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -917,10 +860,5 @@ func NewClient(opts ...client.Option) client.Client {
|
|
|
|
|
encoding.RegisterCodec(&wrapMicroCodec{k})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.funcCall = c.fnCall
|
|
|
|
|
c.funcStream = c.fnStream
|
|
|
|
|
c.funcPublish = c.fnPublish
|
|
|
|
|
c.funcBatchPublish = c.fnBatchPublish
|
|
|
|
|
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|