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

taskgroup: add an explicit Throttle type #6

Merged
merged 2 commits into from
Oct 5, 2024
Merged

Conversation

creachadair
Copy link
Owner

This generalizes the functionality of the Group.Limit method, which has been
rewritten to use the new Throttle type internally. A caller wishing to share
capacity among multiple Groups can explicitly construct a throttle and use its
Limit method to produce start functions for each Group separately.

This generalizes the functionality of the Group.Limit method, which has been
rewritten to use the new Throttle type internally. A caller wishing to share
capacity among multiple Groups can explicitly construct a throttle and use its
Limit method to produce start functions for each Group separately.
Copy link
Collaborator

@danderson danderson left a comment

Choose a reason for hiding this comment

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

Some mild thoughts, but just spitballing, not objections.


// Enter blocks until a slot is available in t, then returns a [Leaver] that
// the caller must execute to return the slot when it is no longer in use.
func (t Throttle) Enter() Leaver {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This signature almost looks like context.WithCancel... I don't know if there's anything in that, just noticed and wondering if there's some opportunity to move the two closer together. Probably not, but sharing in case it causes any flash of insight.

Copy link
Owner Author

Choose a reason for hiding this comment

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

It kind of does, but I'm not sure I see the rhyme: WithCancel returns the new context and the CancelFunc, on the principle you ought to be using both. But here you kind of already have to have the Throttle in-hand. Am I missing something?

if n <= 0 {
return Throttle{}
}
return Throttle{adm: make(chan struct{}, n)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Extremely mild thought: for large limiters, a mutex+cond+uint may be more efficient? More irritating to program, but it's all hidden away neatly, so maybe?...

Copy link
Owner Author

Choose a reason for hiding this comment

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

I did some testing on it some while back (before I started using this pattern), and it's actually not—I think the compiler may have some special cases for channel-of-zero-width-values or something.

throttle.go Outdated Show resolved Hide resolved
@creachadair creachadair merged commit 3dbd8f6 into main Oct 5, 2024
1 check passed
@creachadair creachadair deleted the mjf/limiter branch October 5, 2024 21:30
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.

2 participants