Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix: Add mutex to protect connection in nats.NatsConnector (#514)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Pitman <[email protected]>
  • Loading branch information
arthurpitman authored Jul 13, 2022
1 parent 5415a61 commit 3a171cc
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkg/sdk/connector/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"sync"
"time"

"github.com/google/uuid"
"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/sdk/connector/logger"
"github.com/nats-io/nats.go"
"os"
"time"
)

var _ NATS = (*NatsConnector)(nil)
Expand Down Expand Up @@ -43,10 +45,11 @@ type ProcessEventFn func(msg *nats.Msg) error
// NatsConnector can be used to subscribe to certain events
// on the NATS event system
type NatsConnector struct {
connection *nats.Conn
connectURL string
subscriptions map[string]*nats.Subscription
logger logger.Logger
connectionLock sync.Mutex
connection *nats.Conn
connectURL string
subscriptions map[string]*nats.Subscription
logger logger.Logger
}

// WithLogger sets the logger to use
Expand Down Expand Up @@ -86,6 +89,8 @@ func NewFromEnv() *NatsConnector {
// Note that this will automatically and indefinitely try to reconnect
// as soon as it looses connection
func (nc *NatsConnector) ensureConnection() (*nats.Conn, error) {
nc.connectionLock.Lock()
defer nc.connectionLock.Unlock()

if !nc.connection.IsConnected() {
disconnectLogger := func(con *nats.Conn, err error) {
Expand Down

0 comments on commit 3a171cc

Please sign in to comment.