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

[minor] Prefer signal.NotifyContext to signal.Notify in kanx #3180

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/kanx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ func NewServer() *Server {
}

func (s *Server) Serve(ctx context.Context, addr string) error {
stopChan := make(chan os.Signal, 1)
signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT)
ctx, can := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make it explicit here can => cancel. It becomes easier to understand for the human reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on https://go.dev/wiki/CodeReviewComments#variable-names, maybe this should be c. The readable scope of the variable is 1 line, so i'd suggest a short variable name

Copy link
Contributor

@julio-lopez julio-lopez Oct 11, 2024

Choose a reason for hiding this comment

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

Tom, yes, in general variable names should be proportional to the context and so on.
In this case, cancel stop would be a better name for clarity, especially for the casual reviewer that looks at this code. It's about making it easy to understand the intent.

defer can()
go func() {
select {
case sig := <-stopChan:
log.Info().Print("Gracefully stopping. Received Signal", field.M{"signal": sig})
case <-ctx.Done():
log.Info().Print("Gracefully stopping. Context canceled")
<-ctx.Done()
if err := ctx.Err(); err == context.Canceled {
log.Info().Print("Gracefully stopping. Parent context canceled")
} else {
log.Info().WithError(err).Print("Gracefully stopping.")
}
s.grpcs.GracefulStop()
}()
Expand Down