-
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?
Changes from 3 commits
cbdfc46
c60bc69
75e5d66
c633e2f
1a8fc5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,6 +21,8 @@ const ( | |||||
// pruneSubset controls the subset of confirmed transactions to prune when the structure reaches its max limit. | ||||||
// i.e. if the value is 3 and the limit is 90, 30 transactions will be pruned. | ||||||
pruneSubset = 3 | ||||||
// TODO: This was chosen arbitrarily, we should determine a better value based on expected usage. | ||||||
pruneUnstartedTxDuration = 1 * time.Hour | ||||||
) | ||||||
|
||||||
type InMemoryStore struct { | ||||||
|
@@ -269,6 +271,27 @@ func (m *InMemoryStore) UpdateTransactionBroadcast(txID uint64, txNonce uint64, | |||||
return nil | ||||||
} | ||||||
|
||||||
// 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 | ||||||
} | ||||||
|
||||||
func (m *InMemoryStore) UpdateUnstartedTransactionWithNonce(nonce uint64) (*types.Transaction, error) { | ||||||
m.Lock() | ||||||
defer m.Unlock() | ||||||
|
@@ -277,6 +300,11 @@ func (m *InMemoryStore) UpdateUnstartedTransactionWithNonce(nonce uint64) (*type | |||||
m.lggr.Debugf("Unstarted transactions queue is empty for address: %v", m.address) | ||||||
return nil, nil | ||||||
} | ||||||
prunedTxIDs := m.pruneUnstartedTransactionsWithinDuration(pruneUnstartedTxDuration) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will panic. Needs to be before the check on line 299. |
||||||
if prunedTxIDs != nil { | ||||||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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: