Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add connection pool so we don't leak connections #152

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
caleblloyd marked this conversation as resolved.
Show resolved Hide resolved
// 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