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

Add guidance on goroutine lifecycle management #158

Merged
merged 14 commits into from
Oct 19, 2022
Merged

Add guidance on goroutine lifecycle management #158

merged 14 commits into from
Oct 19, 2022

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Oct 17, 2022

This is mostly an RFC based on some internal discussions.
In general, we prefer for goroutines to have well-managed lifecycles.
No uncontrolled background work that cannot be stopped.

This change tries to distill some of the guidance around it into a style
guide entry.
It's not super comprehensive, but it tries to cover some of the guiding
principles.

Rendered: /~https://github.com/uber-go/guide/blob/goroutines/style.md#dont-fire-and-forget-goroutines

This is mostly an RFC based on some internal discussions.
In general, we prefer for goroutines to have well-managed lifecycles.
No uncontrolled background work that cannot be stopped.

This change tries to distill some of the guidance around it into a style
guide entry.
It's not super comprehensive, but it tries to cover some of the guiding
principles.
@abhinav abhinav requested review from alxn and mway October 17, 2022 21:53
style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
Copy link
Contributor

@prashantv prashantv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this, I've been thinking about this same recommendation after leaving multiple reviews in a similar vein!

style.md Outdated Show resolved Hide resolved
style.md Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
Copy link
Contributor

@mway mway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks @abhinav!

style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
style.md Outdated Show resolved Hide resolved
style.md Show resolved Hide resolved
@abhinav
Copy link
Collaborator Author

abhinav commented Oct 19, 2022

Thanks for the feedback, all. Besides the minor comments, I've made the following changes:

  • the original code sample is fully "correct" in that it includes both, how to stop a worker and how to wait for it to exit.
  • that you should always wait for the goroutine to exit is now a separate subsection demonstrating use of wait groups and channels for the purpose
  • the "don't spawn goroutines in init()" section code sample now includes implementation of how you'd signal stop and wait for exit as well.

Copy link
Contributor

@mway mway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff!

style.md Outdated
Comment on lines 1942 to 1943
stop := make(chan struct{}) // tells the goroutine to stop
done := make(chan struct{}) // tells us that the goroutine exited
Copy link
Contributor

@mway mway Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these intentionally ungrouped (e.g. to minimize example LOC)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah. Good catch.

@abhinav abhinav merged commit 4478e67 into master Oct 19, 2022
@abhinav abhinav deleted the goroutines branch October 19, 2022 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants