-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fn: optimize context guard #9361
Merged
guggero
merged 4 commits into
lightningnetwork:master
from
starius:optimize-context-guard
Jan 10, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
865da9c
fn/ContextGuard: test cancelling blocking context
starius 1750aec
fn: remove uneeded argument of ctxBlocking
starius e9ab603
fn/ContextGuard: clear store of cancel funcs
starius 07c4668
fn/ContextGuard: use context.AfterFunc to wait
starius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This doesn't really remove any overhead (since
AterContext()
callspropagateCancel()
which creates the same "wait for context cancel" goroutine. But it's much simpler to read, so nice optimization in any case.Also nice catch that with the
g.quit
now being un-exported, we can skip that code path as well.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.
When the context is derived from
cancelCtx
(the most common case),propagateCancel
avoids starting additional waiting goroutines. ThecancelCtx
type has an internal mechanism to track its child contexts, allowing it to recursively cancel all children when the parent context is canceled.I added the test
TestContextGuardCountGoroutines
to verify that theCreate
method does not start any waiting goroutines. Without this optimization, the test would initiate 4000 new goroutines.It's worth noting that a goroutine is indeed started later during context cancellation. However, it immediately calls
wg.Done()
and completes, so it doesn't exist throughout the context's lifetime. While the number of goroutines remains the same, their total runtime is significantly reduced, improving efficiency.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.
Ah, right, that makes sense. Should've looked at the code in
propagateCancel
more closely I guess 🙈 Thanks for the clarification! And the additional unit test.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.
Thanks for this PR! @starius I understand the core change is to use
context.AfterFunc
instead of a goroutine for context cancellation handling, which should reduce overhead.I'm trying to understand what you mean by this.
Are you referring to starting a goroutine for each context created here (in the test)?
Additionally, the addition of the test is great. Overall, this looks like a solid improvement.
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.
Yes.