Skip to content

Commit f668845

Browse files
committed
feat: prevent potential deadlocks
1 parent 1e809a0 commit f668845

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/storage/ledger/balances.go

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ledger
33
import (
44
"context"
55
"math/big"
6+
"slices"
67
"strings"
78

89
"github.com/formancehq/go-libs/v2/platform/postgres"
@@ -49,6 +50,21 @@ func (store *Store) GetBalances(ctx context.Context, query ledgercontroller.Bala
4950
}
5051
}
5152

53+
// prevent deadlocks by sorting the accountsVolumes slice
54+
slices.SortStableFunc(accountsVolumes, func(i, j AccountsVolumesWithLedger) int {
55+
if i.Account < j.Account {
56+
return -1
57+
} else if i.Account > j.Account {
58+
return 1
59+
} else if i.Asset < j.Asset {
60+
return -1
61+
} else if i.Asset > j.Asset {
62+
return 1
63+
} else {
64+
return 0
65+
}
66+
})
67+
5268
// Try to insert volumes using last move (to keep compat with previous version) or 0 values.
5369
// This way, if the account has a 0 balance at this point, it will be locked as any other accounts.
5470
// If the complete sql transaction fails, the account volumes will not be inserted.

0 commit comments

Comments
 (0)