From 81f73c9f8df6dd3078a93b6ca978ecbb9c95df16 Mon Sep 17 00:00:00 2001 From: Till <2353100+S7evinK@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:33:23 +0100 Subject: [PATCH] Reuse existing NATS connection (#3345) If using external NATS, we opened unnecessary connections. This now re-uses existing connections. [skip ci] --- setup/jetstream/nats.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/setup/jetstream/nats.go b/setup/jetstream/nats.go index 8820e86b2..8630a1411 100644 --- a/setup/jetstream/nats.go +++ b/setup/jetstream/nats.go @@ -38,7 +38,12 @@ func (s *NATSInstance) Prepare(process *process.ProcessContext, cfg *config.JetS defer natsLock.Unlock() // check if we need an in-process NATS Server if len(cfg.Addresses) != 0 { - return setupNATS(process, cfg, nil) + // reuse existing connections + if s.nc != nil { + return s.js, s.nc + } + s.js, s.nc = setupNATS(process, cfg, nil) + return s.js, s.nc } if s.Server == nil { var err error