Skip to content

Conversation

cl-efornaciari
Copy link
Contributor

adds purging unstarted txs beyond the cutoff

adds purging unstarted txs beyond the cutoff
@cl-efornaciari cl-efornaciari requested review from a team and dimriou as code owners October 15, 2025 17:02
Copy link
Contributor

👋 cl-efornaciari, thanks for creating this pull request!

To help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team.

Once you're ready, you can mark it as "Ready for review" to request feedback. Thanks!

return nil, nil
}
prunedTxIDs := m.pruneUnstartedTransactionsWithinDuration(pruneUnstartedTxDuration)
if prunedTxIDs != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safer to check like this:

Suggested change
if prunedTxIDs != nil {
if len(prunedTxIDs) != 0 {

because depending on the implementation, a slice could be != nil, but still have 0 elements. len method is safe to use even for nil slices.

Comment on lines +274 to +294
// Shouldn't call lock because it's being called by a method that already has the lock
func (m *InMemoryStore) pruneUnstartedTransactionsWithinDuration(threshold time.Duration) []uint64 {
var txIDsToPrune []uint64
idxTxToRetain := 0
for ; idxTxToRetain < len(m.UnstartedTransactions); idxTxToRetain++ {
tx := m.UnstartedTransactions[idxTxToRetain]
if time.Since(tx.CreatedAt) < threshold {
break
}
txIDsToPrune = append(txIDsToPrune, tx.ID)
delete(m.Transactions, tx.ID)
m.UnstartedTransactions[idxTxToRetain] = nil // prevent memory leak
}
if len(txIDsToPrune) == 0 {
return nil
}
m.UnstartedTransactions = m.UnstartedTransactions[idxTxToRetain:]
sort.Slice(txIDsToPrune, func(i, j int) bool { return txIDsToPrune[i] < txIDsToPrune[j] })
return txIDsToPrune
}

Copy link
Contributor

@dimriou dimriou Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the work 👍 . Here's a more idiomatic way for future reference:

Suggested change
// Shouldn't call lock because it's being called by a method that already has the lock
func (m *InMemoryStore) pruneUnstartedTransactionsWithinDuration(threshold time.Duration) []uint64 {
var txIDsToPrune []uint64
idxTxToRetain := 0
for ; idxTxToRetain < len(m.UnstartedTransactions); idxTxToRetain++ {
tx := m.UnstartedTransactions[idxTxToRetain]
if time.Since(tx.CreatedAt) < threshold {
break
}
txIDsToPrune = append(txIDsToPrune, tx.ID)
delete(m.Transactions, tx.ID)
m.UnstartedTransactions[idxTxToRetain] = nil // prevent memory leak
}
if len(txIDsToPrune) == 0 {
return nil
}
m.UnstartedTransactions = m.UnstartedTransactions[idxTxToRetain:]
sort.Slice(txIDsToPrune, func(i, j int) bool { return txIDsToPrune[i] < txIDsToPrune[j] })
return txIDsToPrune
}
// Shouldn't call lock because it's being called by a method that already has the lock
func (m *InMemoryStore) pruneUnstartedTransactionsWithinDuration(threshold time.Duration) (txIDsToPrune []uint64) {
for i, tx := range m.UnstartedTransactions {
if time.Since(tx.CreatedAt) < threshold {
m.UnstartedTransactions = m.UnstartedTransactions[i:]
return txIDsToPrune // you can sort before this if you want to
}
txIDsToPrune = append(txIDsToPrune, tx.ID)
delete(m.Transactions, tx.ID)
m.UnstartedTransactions[i] = nil // prevent memory leak
}
m.UnstartedTransactions = m.UnstartedTransactions[:0]
return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants