From bf0aecf4f6b531eb45d6f9819893f9055bef1c5c Mon Sep 17 00:00:00 2001 From: Yumin Wu Date: Fri, 26 Apr 2019 18:19:42 +0800 Subject: [PATCH] fix go-nats-streaming clientID --- stan.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stan.go b/stan.go index 1b0f31e..760ccfe 100644 --- a/stan.go +++ b/stan.go @@ -25,6 +25,7 @@ type stanBroker struct { sopts stan.Options nopts []stan.Option clusterID string + clientID string connectTimeout time.Duration connectRetry bool done chan struct{} @@ -136,8 +137,7 @@ func (n *stanBroker) connect() error { defer ticker.Stop() fn := func() error { - clientID := uuid.New().String() - c, err := stan.Connect(n.clusterID, clientID, n.nopts...) + c, err := stan.Connect(n.clusterID, n.clientID, n.nopts...) if err == nil { n.Lock() n.conn = c @@ -190,6 +190,11 @@ func (n *stanBroker) Connect() error { return errors.New("must specify ClusterID Option") } + clientID, ok := n.opts.Context.Value(clientIDKey{}).(string) + if !ok || len(clientID) == 0 { + clientID = uuid.New().String() + } + if v, ok := n.opts.Context.Value(connectRetryKey{}).(bool); ok && v { n.connectRetry = true } @@ -220,6 +225,7 @@ func (n *stanBroker) Connect() error { n.Lock() n.nopts = nopts n.clusterID = clusterID + n.clientID = clientID n.Unlock() return n.connect()