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

proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed after f terminates #68914

Open
CAFxX opened this issue Aug 16, 2024 · 1 comment
Labels
Milestone

Comments

@CAFxX
Copy link
Contributor

CAFxX commented Aug 16, 2024

Proposal Details

Add the following to errgroup.Group (note: I don't really like the names, bikeshedding is welcome):

// GoChan is like Go, but returns a channel that is closed after f has returned.
func (g *Group) GoChan(f func() error) <-chan struct{}

// TryGoChan is like TryGo but, if f was started, it returns a non-nil channel like GoChan.
func (g *Group) TryGoChan(f func() error) <-chan struct{}

The channel returned by these variants is closed after f terminates and, crucially, after cancel has been called on the context (if necessary).

I just sketched out the idea here (untested! it's just to discuss the potential semantics).

My main usecase for this is in the following (simplified) scenario:

ch := eg.GoChan(func() error {
  // ...
})

eg.Go(func() error {
  // do something ...

  select {
  case <-ctx.Done():
    return ctx.Err()
  case <-ch:
  }

  // do something else, but only after the first function has completed
})

(this example above is extremely simplified, in reality there are often many more calls, sometimes not even known statically, and with more complex relationships between functions, so manually finding an order becomes unwieldy fast - and especially maintaining that code becomes much harder)

@CAFxX CAFxX added the Proposal label Aug 16, 2024
@gopherbot gopherbot added this to the Proposal milestone Aug 16, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@CAFxX CAFxX changed the title proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed when f terminates proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed when f terminates Aug 16, 2024
@CAFxX CAFxX changed the title proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed when f terminates proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed when f terminates Aug 16, 2024
@CAFxX CAFxX changed the title proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed when f terminates proposal: x/sync/errgroup: add (Try?)Go variants that return channel closed after f terminates Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants