Skip to content
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

Merged
merged 1 commit into from
May 4, 2021

Conversation

hbagdi
Copy link
Member

@hbagdi hbagdi commented Apr 26, 2021

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.

@hbagdi hbagdi requested a review from a team as a code owner April 26, 2021 17:58
@hbagdi hbagdi force-pushed the refactor/plumb-context branch 2 times, most recently from 6decdc3 to bfe58af Compare April 26, 2021 18:27
@codecov-commenter
Copy link

codecov-commenter commented Apr 26, 2021

Codecov Report

Merging #350 (79c1fd6) into main (fc24613) will decrease coverage by 0.14%.
The diff coverage is 0.00%.

❗ Current head 79c1fd6 differs from pull request most recent head 92aafdf. Consider uploading reports for the commit 92aafdf to get more accurate results
Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
cmd/common.go 0.00% <0.00%> (ø)
cmd/common_konnect.go 15.21% <0.00%> (ø)
cmd/diff.go 72.72% <0.00%> (-2.28%) ⬇️
cmd/dump.go 24.24% <0.00%> (-0.25%) ⬇️
cmd/konnect_dump.go 27.08% <0.00%> (ø)
cmd/ping.go 35.71% <0.00%> (-2.75%) ⬇️
cmd/reset.go 27.86% <0.00%> (-0.47%) ⬇️
cmd/root.go 55.96% <0.00%> (ø)
cmd/sync.go 72.72% <0.00%> (-2.28%) ⬇️
dump/dump.go 1.56% <0.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc24613...92aafdf. Read the comment docs.

rainest
rainest previously approved these changes Apr 26, 2021
@hbagdi
Copy link
Member Author

hbagdi commented Apr 26, 2021

Will wait for @mflendrich for a review on this one.

@mmorel-35
Copy link
Contributor

The contexts of ping command seems to be missing is it deliberately?

@hbagdi hbagdi force-pushed the refactor/plumb-context branch from bfe58af to 76d64ae Compare April 27, 2021 15:59
@hbagdi
Copy link
Member Author

hbagdi commented Apr 27, 2021

The contexts of ping command seems to be missing is it deliberately?

Good catch. Added that.

@hbagdi hbagdi dismissed rainest’s stale review April 27, 2021 15:59

Code updated again

@mmorel-35
Copy link
Contributor

mmorel-35 commented Apr 27, 2021

Another one, the last one 😉?

_, err = client.Do(nil, req, &response)

@hbagdi hbagdi force-pushed the refactor/plumb-context branch from 76d64ae to 92aafdf Compare April 27, 2021 21:12
@mmorel-35
Copy link
Contributor

Shall those lines be removed with this pr?

deck/.golangci.yml

Lines 18 to 20 in 64a98cc

- linters:
- staticcheck
text: "SA1012" # do not pass a nil Context

@hbagdi
Copy link
Member Author

hbagdi commented May 3, 2021

Shall those lines be removed with this pr?

We can't because solver package has a bunch of nil ctx invocations.
This PR wasn't meant to be an exhaustive squash of all issues with ctx.

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.
@hbagdi hbagdi force-pushed the refactor/plumb-context branch from 92aafdf to 9733315 Compare May 3, 2021 20:15
@hbagdi
Copy link
Member Author

hbagdi commented May 3, 2021

Conflicts have been fixed. PTAL.

Copy link
Contributor

@mflendrich mflendrich left a 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()
Copy link
Contributor

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)

Copy link
Member Author

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

Copy link
Member Author

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?

Copy link
Contributor

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 to RunE and to the return value of cmd.Execute()
  • After cmd.Execute() returns, check if ctx.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.

Copy link
Member Author

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.

@hbagdi hbagdi merged commit 7b85526 into main May 4, 2021
@hbagdi hbagdi deleted the refactor/plumb-context branch May 4, 2021 18:52
@mmorel-35
Copy link
Contributor

Part 1of #325

AntoineJac pushed a commit that referenced this pull request Jan 23, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants