Skip to content

proposal: x/sync/errgroup: improve api for context values #39312

@titpetric

Description

@titpetric

I propose extending the x/errgroup API with a few convenience functions,

  • Context() context.Context - return the error group cancelable context,
  • GoContext(func(context.Context) error) - pass the produced ctx on to functions,

This requires:

  1. Add the produced context.Context value to Group{},
  2. Have the ability to read it afterwards with Context(),
  3. Pass context to functions with GoContext(func(context.Context) error) convenience

The convenience function can be easily implemented as a wrapper:

func (g *Group) GoContext(fn func(context.Context) error) {
        g.Go(func() error {
                return fn(g.Context())
        }
}

The suggested naming is in line with http.Request Context(), and database/sql function signatures like ExecContext(...). The impact on code readability on codebases should be positive, as the wrapping for context-aware functions is eliminated on the consumer implementation side. There are no backwards compatibility issues that I can see.

Are PRs for this area accepted, any concerns? I'm more than willing to submit a PR for extending the functionality of this package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions