-
Notifications
You must be signed in to change notification settings - Fork 129
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
refactor: plumb a single context throughout the code #350
Conversation
6decdc3
to
bfe58af
Compare
Codecov Report
@@ Coverage Diff @@
## main #350 +/- ##
==========================================
- Coverage 53.66% 53.51% -0.15%
==========================================
Files 60 59 -1
Lines 4877 4890 +13
==========================================
Hits 2617 2617
- Misses 1962 1975 +13
Partials 298 298
Continue to review full report at Codecov.
|
Will wait for @mflendrich for a review on this one. |
The contexts of ping command seems to be missing is it deliberately? |
bfe58af
to
76d64ae
Compare
Good catch. Added that. |
Another one, the last one 😉? Line 38 in 76d64ae
|
76d64ae
to
92aafdf
Compare
Shall those lines be removed with this pr? Lines 18 to 20 in 64a98cc
|
We can't because solver package has a bunch of nil ctx invocations. |
This patch is an effort to improve the context propagation in decK, down to every single operation. In order to do that, a single root context is now being created in the main function, and all contexts should be derived from it.
92aafdf
to
9733315
Compare
Conflicts have been fixed. PTAL. |
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.
I'm happy to see this change 👍
LGTM! With one optional comment.
} | ||
|
||
func main() { | ||
registerSignalHandler() | ||
cmd.Execute() | ||
ctx := registerSignalHandler() |
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.
Consider the following:
https://pkg.go.dev/os/signal#NotifyContext
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
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.
included this, this is a handy simplification
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.
Said too soon, @mflendrich the only problem here is that when the signal arrives, there wouldn't be a message to stdout stating that the signal was actually received. This is quite helpful in debugging. Thoughts?
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.
Hacky way (not recommended):
go func() {
select {
case <-ctx.Done():
fmt.Printf("context cancelled (%w), terminating...", ctx.Err())
}
}
Clean way:
- Replace
os.Exit(1)
throughout the codebase with errors plumbed all the way through toRunE
and to the return value ofcmd.Execute()
- After
cmd.Execute()
returns, check ifctx.Err()
is not nil. If it isn't, then the context got cancelled by the signal.
Both are somewhat tedious and may defeat the purpose of this simplification.
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.
Thought and explored a few different ways to do it but I haven't found a good way to deal with it in this PR.
This also feels minor enough that we can leave it be for now. If it is a bigger issue, please LMK and I'll address with a quick follow up PR.
I'm going to go ahead and merge this in.
Part 1of #325 |
This patch is an effort to improve the context propagation in decK, down to every single operation. In order to do that, a single root context is now being created in the main function, and all contexts should be derived from it.
This patch is an effort to improve the context propagation in decK, down
to every single operation.
In order to do that, a single root context is now being created in the
main function, and all contexts should be derived from it.