fix go-nats-streaming clientID

This commit is contained in:
Yumin Wu 2019-04-26 18:19:42 +08:00 committed by Vasiliy Tolstov
parent 5e260a4cfa
commit bf0aecf4f6

10
stan.go
View File

@ -25,6 +25,7 @@ type stanBroker struct {
sopts stan.Options sopts stan.Options
nopts []stan.Option nopts []stan.Option
clusterID string clusterID string
clientID string
connectTimeout time.Duration connectTimeout time.Duration
connectRetry bool connectRetry bool
done chan struct{} done chan struct{}
@ -136,8 +137,7 @@ func (n *stanBroker) connect() error {
defer ticker.Stop() defer ticker.Stop()
fn := func() error { fn := func() error {
clientID := uuid.New().String() c, err := stan.Connect(n.clusterID, n.clientID, n.nopts...)
c, err := stan.Connect(n.clusterID, clientID, n.nopts...)
if err == nil { if err == nil {
n.Lock() n.Lock()
n.conn = c n.conn = c
@ -190,6 +190,11 @@ func (n *stanBroker) Connect() error {
return errors.New("must specify ClusterID Option") 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 { if v, ok := n.opts.Context.Value(connectRetryKey{}).(bool); ok && v {
n.connectRetry = true n.connectRetry = true
} }
@ -220,6 +225,7 @@ func (n *stanBroker) Connect() error {
n.Lock() n.Lock()
n.nopts = nopts n.nopts = nopts
n.clusterID = clusterID n.clusterID = clusterID
n.clientID = clientID
n.Unlock() n.Unlock()
return n.connect() return n.connect()