diff --git a/grpc.go b/grpc.go index 1c7f351..7c5a008 100644 --- a/grpc.go +++ b/grpc.go @@ -6,7 +6,7 @@ import ( "crypto/tls" "fmt" "os" - "sync" + "sync/atomic" "time" "github.com/micro/go-micro/v2/broker" @@ -24,9 +24,9 @@ import ( ) type grpcClient struct { - once sync.Once opts client.Options pool *pool + once atomic.Value } func init() { @@ -570,9 +570,12 @@ func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...clie body = b } - g.once.Do(func() { - g.opts.Broker.Connect() - }) + 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) + } topic := p.Topic() @@ -641,9 +644,9 @@ func newClient(opts ...client.Option) client.Client { } rc := &grpcClient{ - once: sync.Once{}, opts: options, } + rc.once.Store(false) rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())