Skip to content

Commit

Permalink
Run error callback in defer
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Piotrowski <[email protected]>
  • Loading branch information
piotrpio committed Aug 8, 2024
1 parent 421e9e3 commit 58970ee
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 20 deletions.
6 changes: 2 additions & 4 deletions jetstream/jetstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,10 +1066,8 @@ func (js *jetStream) CleanupPublisher() {
paf.errCh <- paf.err
}
if errCb != nil {
// clear reply subject so that new one is created on republish
js.publisher.Unlock()
errCb(js, paf.msg, ErrJetStreamPublisherClosed)
js.publisher.Lock()
// call error handler after releasing the mutex to avoid contention
defer errCb(js, paf.msg, ErrJetStreamPublisherClosed)
}
delete(js.publisher.acks, id)
}
Expand Down
8 changes: 2 additions & 6 deletions jetstream/publish.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 The NATS Authors
// Copyright 2022-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -467,11 +467,7 @@ func (js *jetStream) resetPendingAcksOnReconnect() {
paf.errCh <- paf.err
}
if errCb != nil {
js.publisher.Unlock()
// clear reply subject so that new one is created on republish
paf.msg.Reply = ""
errCb(js, paf.msg, nats.ErrDisconnected)
js.publisher.Lock()
defer errCb(js, paf.msg, nats.ErrDisconnected)
}
delete(js.publisher.acks, id)
}
Expand Down
2 changes: 1 addition & 1 deletion jetstream/test/jetstream_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 The NATS Authors
// Copyright 2022-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
12 changes: 3 additions & 9 deletions js.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2023 The NATS Authors
// Copyright 2020-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -732,10 +732,7 @@ func (js *js) resetPendingAcksOnReconnect() {
paf.errCh <- paf.err
}
if errCb != nil {
// clear reply subject so that new one is created on republish
js.mu.Unlock()
errCb(js, paf.msg, ErrDisconnected)
js.mu.Lock()
defer errCb(js, paf.msg, ErrDisconnected)
}
delete(js.pafs, id)
}
Expand Down Expand Up @@ -768,10 +765,7 @@ func (js *js) CleanupPublisher() {
paf.errCh <- paf.err
}
if errCb != nil {
// clear reply subject so that new one is created on republish
js.mu.Unlock()
errCb(js, paf.msg, ErrJetStreamPublisherClosed)
js.mu.Lock()
defer errCb(js, paf.msg, ErrJetStreamPublisherClosed)
}
delete(js.pafs, id)
}
Expand Down

0 comments on commit 58970ee

Please sign in to comment.