-
Notifications
You must be signed in to change notification settings - Fork 698
refactor: reduce context.Background() #3888
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
base: master
Are you sure you want to change the base?
Conversation
d321bbf
to
1566004
Compare
cmd/limactl/clone.go
Outdated
@@ -71,7 +71,7 @@ func cloneAction(cmd *cobra.Command, args []string) error { | |||
if err != nil { | |||
return err | |||
} | |||
yBytes, err := yqutil.EvaluateExpression(yq, yContent) | |||
yBytes, err := yqutil.EvaluateExpression(cmd.Context(), yq, yContent) |
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.
yBytes, err := yqutil.EvaluateExpression(cmd.Context(), yq, yContent) | |
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent) |
Same for other occurrences of cmd.Context()
too
1566004
to
5426244
Compare
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, LGTM
I have only 2 stylistic nit-picks that you can address, if you want. But we already have existing functions that use _
for the single unnamed parameter, so it isn't a new issue.
There is of course the issue of failing CI, but I think this might be GitHub flakiness; we'll see...
pkg/driver/driver.go
Outdated
@@ -63,7 +63,7 @@ type Registration interface { | |||
// GuestAgent defines operations for the guest agent. | |||
type GuestAgent interface { | |||
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent. | |||
ForwardGuestAgent() bool | |||
ForwardGuestAgent(_ context.Context) bool |
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.
If a function/method has only unnamed parameters, then you don't need to specify the _
name. It is only needed when you want to provide a name for parameters with a basic type like bool
. int
, or string
, which are not self-explanatory because you cannot mix named and unnamed parameters.
ForwardGuestAgent(_ context.Context) bool | |
ForwardGuestAgent(context.Context) bool |
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.
Shall we raise a new Issue/PR focusing on removing _
for single unnamed parameter to make it consistent?
pkg/hostagent/hostagent.go
Outdated
@@ -585,19 +585,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) { | |||
remoteUnix := "/run/lima-guestagent.sock" | |||
|
|||
a.onClose = append(a.onClose, func() error { | |||
ctxBG := context.Background() |
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 feel like this is a premature optimization. You introduce an extra variable name that the user needs to keep track of instead of calling a very cheap function multiple times.
It doesn't matter much, but I would always try to minimize the context that the developer needs to carry in their head while reading the code, especially if it is a rather long function.
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.
Yeah, makes sense—I'll drop ctxBG
and just call context.Background()
directly.
Signed-off-by: ashwat287 <ashwatpas@gmail.com>
5426244
to
6a5b683
Compare
fixes #3875