-
Notifications
You must be signed in to change notification settings - Fork 13
OEV-605: Adds purging unstarted txs to txmv2 #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
adds purging unstarted txs beyond the cutoff
👋 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 { |
There was a problem hiding this comment.
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:
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.
// 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 | ||
} | ||
|
There was a problem hiding this comment.
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:
// 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 | |
} |
adds purging unstarted txs beyond the cutoff