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

Checking context in Go() #7

Open
horgh opened this issue Apr 23, 2021 · 0 comments
Open

Checking context in Go() #7

horgh opened this issue Apr 23, 2021 · 0 comments

Comments

@horgh
Copy link

horgh commented Apr 23, 2021

Hi again!

Another use of this package brought to mind a possible improvement: Checking whether the context is cancelled in Go().

This isn't something golang.org/x/sync does, but given this package's Go() can block, I wonder if it might be useful.

In my use case I am calling Go() thousands of times with a limited number of workers. I wondered what would happen if one of the goroutines returned with an error. Currently in my case they will do some non-context aware work before using the context and then they would see it is cancelled and return. So there would be a bunch of work done and likely Go() would be blocked, causing the error to not be seeing as quickly as it might be otherwise. Whereas if Go() checked the context and returned without invoking the callback, this would be less of a concern.

Basically my use looks something like:

for _, work := range thingsToDo {
    eg.Go(func() error {
        somethingNotContextAware()
        return somethingContextAware(ctx)
    })
}

if err := eg.Wait(); err != nil {
    return err
}

Essentially I'm thinking of making it as quick as possible to get to knowing there was an error.

I could check the context in my callback or make my code more context aware, but I suppose I'm thinking it might be a nice addition to this package. Although I wonder whether it'd need an additional Go() function that is context aware, unless we held on to the context when creating the errgroup.

I could probably come up with a PR if you think it'd be suitable.

Thanks for your time!

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

No branches or pull requests

1 participant