Skip to content

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

Open
@CAFxX

Description

@CAFxX

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)

Activity

added this to the Proposal milestone on Aug 16, 2024
gabyhelp

gabyhelp commented on Aug 16, 2024

@gabyhelp
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[/+] on Aug 16, 2024
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[/+] on Aug 16, 2024
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[/+] on Aug 16, 2024
moved this to Incoming in Proposalson Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @CAFxX@gopherbot@gabyhelp

        Issue actions

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