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

refactor: simplify the common case of ranging over a channel w/ cancellation #3831

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

alecthomas
Copy link
Collaborator

Simplifies this:

for {
  select {
    case <-ctx.Done():
      return
    case msg := <-events:
      // DO STUFF
  }
}

To this:

for msg := range channels.IterContext(ctx, events) {
  // DO STUFF
}

})
case <-ctx.Done():
return
case <-time.After(1 * time.Second):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to do anything - as there's nothing in the for loop outside the select, it will just drop straight back into the select.

@alecthomas alecthomas force-pushed the aat/simplify-channel-range branch 2 times, most recently from 883adac to 3c97e4e Compare December 19, 2024 23:20
@alecthomas alecthomas enabled auto-merge (squash) December 19, 2024 23:20
…llation

Simplifies this:

  for {
    select {
      case <-ctx.Done():
        return
      case msg := <-events:
        // DO STUFF
    }
  }

To this:

  for msg := range channels.IterContext(ctx, events) {
    // DO STUFF
  }
@alecthomas alecthomas force-pushed the aat/simplify-channel-range branch from 3c97e4e to 8089bea Compare December 19, 2024 23:27
@alecthomas alecthomas merged commit c896dc1 into main Dec 19, 2024
61 checks passed
@alecthomas alecthomas deleted the aat/simplify-channel-range branch December 19, 2024 23:34
@alecthomas alecthomas mentioned this pull request Dec 19, 2024
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

Successfully merging this pull request may close these issues.

1 participant