correcting grpc.go
This commit is contained in:
122
grpc.go
122
grpc.go
@@ -5,6 +5,11 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/semconv"
|
||||
"go.unistack.org/micro/v4/tracer"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"net"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -18,12 +23,10 @@ import (
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/selector"
|
||||
"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 (
|
||||
@@ -31,12 +34,10 @@ const (
|
||||
)
|
||||
|
||||
type grpcClient struct {
|
||||
funcPublish client.FuncPublish
|
||||
funcBatchPublish client.FuncBatchPublish
|
||||
funcCall client.FuncCall
|
||||
funcStream client.FuncStream
|
||||
pool *ConnPool
|
||||
opts client.Options
|
||||
funcCall client.FuncCall
|
||||
funcStream client.FuncStream
|
||||
pool *ConnPool
|
||||
opts client.Options
|
||||
sync.RWMutex
|
||||
init bool
|
||||
}
|
||||
@@ -76,10 +77,7 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
||||
var header map[string][]string
|
||||
|
||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||
header = make(map[string][]string, len(md))
|
||||
for k, v := range md {
|
||||
header[strings.ToLower(k)] = v
|
||||
}
|
||||
header = metadata.Copy(md)
|
||||
} else {
|
||||
header = make(map[string][]string, 2)
|
||||
}
|
||||
@@ -89,12 +87,11 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
||||
}
|
||||
}
|
||||
// set timeout in nanoseconds
|
||||
header["Grpc-Timeout"] = append(header["Grpc-Timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
header["grpc-timeout"] = append(header["grpc-timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
header["timeout"] = append(header["timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
header["content-type"] = append(header["content-type"], req.ContentType())
|
||||
|
||||
md := gmetadata.MD(metadata.Copy(header))
|
||||
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
||||
ctx = gmetadata.NewOutgoingContext(ctx, header)
|
||||
|
||||
cf, err := g.newCodec(req.ContentType())
|
||||
if err != nil {
|
||||
@@ -180,7 +177,7 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
||||
if opts.ResponseMetadata != nil {
|
||||
*opts.ResponseMetadata = metadata.New(gmd.Len())
|
||||
for k, v := range gmd {
|
||||
opts.ResponseMetadata.Set(k, strings.Join(v, ","))
|
||||
opts.ResponseMetadata.Append(k, v...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,24 +188,20 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
||||
var header map[string][]string
|
||||
|
||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||
header = make(map[string][]string, len(md))
|
||||
for k, v := range md {
|
||||
header[k] = v
|
||||
}
|
||||
header = metadata.Copy(md)
|
||||
} else {
|
||||
header = make(map[string][]string)
|
||||
}
|
||||
|
||||
// set timeout in nanoseconds
|
||||
if opts.StreamTimeout > time.Duration(0) {
|
||||
header["Grpc-Timeout"] = append(header["Grpc-Timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
header["grpc-timeout"] = append(header["grpc-timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
header["timeout"] = append(header["timeout"], fmt.Sprintf("%dn", opts.RequestTimeout))
|
||||
}
|
||||
// set the content type for the request
|
||||
header["content-type"] = append(header["content-type"], req.ContentType())
|
||||
|
||||
md := gmetadata.MD(metadata.Copy(header))
|
||||
ctx = gmetadata.NewOutgoingContext(ctx, md)
|
||||
ctx = gmetadata.NewOutgoingContext(ctx, header)
|
||||
|
||||
cf, err := g.newCodec(req.ContentType())
|
||||
if err != nil {
|
||||
@@ -413,8 +406,6 @@ func (g *grpcClient) Init(opts ...client.Option) error {
|
||||
|
||||
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) {
|
||||
@@ -422,12 +413,9 @@ func (g *grpcClient) Init(opts ...client.Option) error {
|
||||
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)
|
||||
}
|
||||
})
|
||||
g.init = true
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -440,37 +428,6 @@ func (g *grpcClient) NewRequest(service, method string, req interface{}, reqOpts
|
||||
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 {
|
||||
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")
|
||||
}
|
||||
|
||||
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
|
||||
@@ -601,7 +558,13 @@ func (g *grpcClient) fnCall(ctx context.Context, req client.Request, rsp interfa
|
||||
return gerr
|
||||
}
|
||||
|
||||
func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
||||
func (g *grpcClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
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")
|
||||
}
|
||||
|
||||
ts := time.Now()
|
||||
g.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
||||
var sp tracer.Span
|
||||
@@ -609,21 +572,21 @@ func (g *grpcClient) Stream(ctx context.Context, req client.Request, opts ...cli
|
||||
tracer.WithSpanKind(tracer.SpanKindClient),
|
||||
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
||||
)
|
||||
stream, err := g.funcStream(ctx, req, opts...)
|
||||
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 := status.Convert(err); me == nil {
|
||||
if me := errors.FromError(err); me == nil {
|
||||
sp.Finish()
|
||||
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "success", "code", strconv.Itoa(int(codes.OK))).Inc()
|
||||
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()
|
||||
g.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "failure", "code", strconv.Itoa(int(me.Code))).Inc()
|
||||
}
|
||||
|
||||
return stream, err
|
||||
return err
|
||||
}
|
||||
|
||||
func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
||||
@@ -754,6 +717,31 @@ func (g *grpcClient) fnStream(ctx context.Context, req client.Request, opts ...c
|
||||
return nil, grr
|
||||
}
|
||||
|
||||
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) String() string {
|
||||
return "grpc"
|
||||
}
|
||||
@@ -833,8 +821,6 @@ func NewClient(opts ...client.Option) client.Client {
|
||||
|
||||
c.funcCall = c.fnCall
|
||||
c.funcStream = c.fnStream
|
||||
c.funcPublish = c.fnPublish
|
||||
c.funcBatchPublish = c.fnBatchPublish
|
||||
|
||||
return c
|
||||
}
|
||||
|
Reference in New Issue
Block a user