Skip to content

Commit

Permalink
reuse same name for client
Browse files Browse the repository at this point in the history
  • Loading branch information
haisum committed Nov 13, 2023
1 parent 3f17554 commit 0768570
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
1 change: 0 additions & 1 deletion controllers/jetstream/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func (c *Controller) processConsumerObject(cns *apis.Consumer, jsm jsmClientFunc
if c.opts.CRDConnect {
// Create a new client
natsCtx := &natsContext{}
natsCtx.Name = fmt.Sprintf("%s-con-%s-%d", c.opts.NATSClientName, spec.DurableName, cns.Generation)
// Use JWT/NKEYS based credentials if present.
if spec.Creds != "" {
natsCtx.Credentials = spec.Creds
Expand Down
12 changes: 7 additions & 5 deletions controllers/jetstream/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ func (c *Controller) Run() error {

// Connect to NATS.
opts := make([]nats.Option, 0)
opts = append(opts, nats.Name(c.opts.NATSClientName))
// Always attempt to have a connection to NATS.
opts = append(opts, nats.MaxReconnects(-1))
natsCtxDefaults := &natsContextDefaults{Name: c.opts.NATSClientName}
if !c.opts.CRDConnect {
// Use JWT/NKEYS based credentials if present.
if c.opts.NATSCredentials != "" {
Expand All @@ -194,21 +194,23 @@ func (c *Controller) Run() error {
}

if c.opts.NATSCertificate != "" && c.opts.NATSKey != "" {
opts = append(opts, nats.ClientCert(c.opts.NATSCertificate, c.opts.NATSKey))
natsCtxDefaults.TLSCert = c.opts.NATSCertificate
natsCtxDefaults.TLSKey = c.opts.NATSKey
}

if c.opts.NATSCA != "" {
opts = append(opts, nats.RootCAs(c.opts.NATSCA))
natsCtxDefaults.TLSCAs = []string{c.opts.NATSCA}
}
ncp := newNatsConnPool(logrus.New(), &natsContextDefaults{URL: c.opts.NATSServerURL}, opts)
natsCtxDefaults.URL = c.opts.NATSServerURL
ncp := newNatsConnPool(logrus.New(), natsCtxDefaults, opts)
pooledNc, err := ncp.Get(&natsContext{})
if err != nil {
return fmt.Errorf("failed to connect to nats: %w", err)
}
pooledNc.ReturnToPool()
c.connPool = ncp
} else {
c.connPool = newNatsConnPool(logrus.New(), &natsContextDefaults{Name: c.opts.NATSClientName}, opts)
c.connPool = newNatsConnPool(logrus.New(), natsCtxDefaults, opts)
}

defer utilruntime.HandleCrash()
Expand Down
1 change: 0 additions & 1 deletion controllers/jetstream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func (c *Controller) processStreamObject(str *apis.Stream, jsm jsmClientFunc) (e
if c.opts.CRDConnect {
// Create a new client
natsCtx := &natsContext{}
natsCtx.Name = fmt.Sprintf("%s-str-%s-%d", c.opts.NATSClientName, spec.Name, str.Generation)
// Use JWT/NKEYS based credentials if present.
if spec.Creds != "" {
natsCtx.Credentials = spec.Creds
Expand Down

0 comments on commit 0768570

Please sign in to comment.