-
Notifications
You must be signed in to change notification settings - Fork 70
fix(core): use sync.Once for SenderCacher initialization #31029 #2173
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -18,12 +18,21 @@ package core | |
|
|
||
| import ( | ||
| "runtime" | ||
| "sync" | ||
|
|
||
| "github.com/XinFinOrg/XDPoSChain/core/types" | ||
| ) | ||
|
|
||
| // SenderCacher is a concurrent transaction sender recoverer and cacher. | ||
| var SenderCacher = newTxSenderCacher(runtime.NumCPU()) | ||
| // senderCacherOnce is used to ensure that the SenderCacher is initialized only once. | ||
| var senderCacherOnce = sync.OnceValue(func() *txSenderCacher { | ||
| return newTxSenderCacher(runtime.NumCPU()) | ||
| }) | ||
|
|
||
| // SenderCacher returns the singleton instance of SenderCacher, initializing it if called for the first time. | ||
| // This function is thread-safe and ensures that initialization happens only once. | ||
| func SenderCacher() *txSenderCacher { | ||
| return senderCacherOnce() | ||
| } | ||
|
Comment on lines
+26
to
+35
|
||
|
|
||
| // txSenderCacherRequest is a request for recovering transaction senders with a | ||
| // specific signature scheme and caching it into the transactions themselves. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1613,7 +1613,7 @@ func (pool *LegacyPool) reset(oldHead, newHead *types.Header) { | |
|
|
||
| // Inject any transactions discarded due to reorgs | ||
| log.Debug("Reinjecting stale transactions", "count", len(reinject)) | ||
| core.SenderCacher.Recover(pool.signer, reinject) | ||
| core.SenderCacher().Recover(pool.signer, reinject) | ||
|
||
| pool.addTxsLocked(reinject, false) | ||
| } | ||
|
|
||
|
|
||
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.
SenderCacher()is now lazily initialized and will allocate the task channel and spawn worker goroutines on first use.insertChainruns with the chain mutex held, so the first call can shift initialization cost into a lock-held critical path. Consider initializing the cacher before acquiringbc.chainmu(or during blockchain startup) to avoid a one-time pause during the first chain insertion.