You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While looking at the current interface definition for Task I've seen that Task.Poll doesn't accept any argument nor return an error (more on this later).
Then looking at implementations of the interface, I've found that a context could be useful here:
CallbackTaskcreates a context.Background(), thus not giving the option to pass a root context that could be cancelled for different reasons.
ShellTask could instead create the command using exec.CommandContext so we could have finer control over the executed binary.
Contexts are great and very powerful when used this way. As an example, if the interface was called this way, we could have in our main.go the following piece of code:
And if the user clicks ^C while the program is calling root.Poll it will cancel the context, thus passing this cancellation information down to all processes and we will have better resource management and cancellation, rather than an abrupt termination.
What do you think?
PS: I mention this function also returning an error. I think it could be very useful information to have, for instance, implementations would perhaps return ctx.Err() which might indicate to the caller that the context was cancelled or timed out.
The text was updated successfully, but these errors were encountered:
While looking at the current interface definition for
Task
I've seen thatTask.Poll
doesn't accept any argument nor return an error (more on this later).Then looking at implementations of the interface, I've found that a context could be useful here:
CallbackTask
creates acontext.Background()
, thus not giving the option to pass a root context that could be cancelled for different reasons.ShellTask
could instead create the command usingexec.CommandContext
so we could have finer control over the executed binary.NestedTask
could pass the context to its children.Contexts are great and very powerful when used this way. As an example, if the interface was called this way, we could have in our
main.go
the following piece of code:And if the user clicks
^C
while the program is callingroot.Poll
it will cancel the context, thus passing this cancellation information down to all processes and we will have better resource management and cancellation, rather than an abrupt termination.What do you think?
PS: I mention this function also returning an
error
. I think it could be very useful information to have, for instance, implementations would perhapsreturn ctx.Err()
which might indicate to the caller that the context was cancelled or timed out.The text was updated successfully, but these errors were encountered: