File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ package query
22
33import (
44 "context"
5+ "slices"
6+ "sync"
57 "sync/atomic"
68
79 "github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
@@ -24,10 +26,12 @@ var (
2426type Transaction struct {
2527 tx.Identifier
2628
27- s * Session
28- onCompleted []tx.OnTransactionCompletedFunc
29-
29+ s * Session
3030 rollbackStarted atomic.Bool
31+
32+ m sync.Mutex
33+ onCompletedCalled bool
34+ onCompleted []tx.OnTransactionCompletedFunc
3135}
3236
3337func (tx * Transaction ) SessionID () string {
@@ -211,11 +215,24 @@ func (tx *Transaction) Rollback(ctx context.Context) error {
211215}
212216
213217func (tx * Transaction ) OnCompleted (f tx.OnTransactionCompletedFunc ) {
218+ tx .m .Lock ()
219+ defer tx .m .Unlock ()
220+
214221 tx .onCompleted = append (tx .onCompleted , f )
215222}
216223
217224func (tx * Transaction ) notifyOnCompleted (err error ) {
218- for _ , f := range tx .onCompleted {
225+ tx .m .Lock ()
226+ notifyCalled := tx .onCompletedCalled
227+ tx .onCompletedCalled = true
228+ onCompletedFunctions := slices .Clone (tx .onCompleted )
229+ tx .m .Unlock ()
230+
231+ if notifyCalled {
232+ return
233+ }
234+
235+ for _ , f := range onCompletedFunctions {
219236 f (err )
220237 }
221238}
You can’t perform that action at this time.
0 commit comments