Skip to content

Commit

Permalink
fix(store/nats-js-kv): Unset conn when connection is closed
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Apr 9, 2024
1 parent 94a49ba commit 1b5e701
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions v4/store/nats-js-kv/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func NewStore(opts ...store.Option) store.Store {

n.setOption(opts...)

// register unset connection callback
f := n.nopts.ClosedCB
n.nopts.ClosedCB = func(c *nats.Conn) {
// reset connection
n.resetConn()

// execute the original callback if set
if f != nil {
f(c)
}
}

return n
}

Expand Down Expand Up @@ -360,6 +372,14 @@ func (n *natsStore) hasConn() bool {
return n.conn != nil
}

// thread safe way to reset the connection.
func (n *natsStore) resetConn() {
n.Lock()
defer n.Unlock()

n.conn = nil
}

// mustGetDefaultBucket returns the bucket with the given name creating it with default configuration if needed.
func (n *natsStore) mustGetBucketByName(name string) (nats.KeyValue, error) {
return n.mustGetBucket(&nats.KeyValueConfig{
Expand Down

0 comments on commit 1b5e701

Please sign in to comment.