From 7ef834f0a88f8df3c05287b9c797987cf497ba43 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 18 Aug 2020 14:44:29 +0100 Subject: [PATCH] Revert "grpc: avoid allocations for each message (#1939)" (#1941) This reverts commit 2a23224d911b0aeccf0614afb937a18ad7d59bca. --- grpc.go | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/grpc.go b/grpc.go index ef0c9be..f230fa4 100644 --- a/grpc.go +++ b/grpc.go @@ -8,7 +8,6 @@ import ( "net" "reflect" "strings" - "sync" "sync/atomic" "time" @@ -25,11 +24,9 @@ import ( ) type grpcClient struct { - opts client.Options - codecs map[string]encoding.Codec - pool *pool - once atomic.Value - sync.RWMutex + opts client.Options + pool *pool + once atomic.Value } func init() { @@ -304,13 +301,18 @@ func (g *grpcClient) maxSendMsgSizeValue() int { } func (g *grpcClient) newGRPCCodec(contentType string) (encoding.Codec, error) { - g.RLock() - defer g.RUnlock() - - if c, ok := g.codecs[contentType]; ok { + 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) } @@ -719,22 +721,6 @@ func newClient(opts ...client.Option) client.Client { c = options.Wrappers[i-1](c) } - rc.codecs = make(map[string]encoding.Codec, len(defaultGRPCCodecs)) - for k, v := range defaultGRPCCodecs { - rc.codecs[k] = v - } - - var codecs map[string]encoding.Codec - if rc.opts.Context != nil { - if v := rc.opts.Context.Value(codecsKey{}); v != nil { - codecs = v.(map[string]encoding.Codec) - } - } - - for k, v := range codecs { - rc.codecs[k] = v - } - return c }