-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
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.
19aae29
to
da8de9b
Compare
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)} |
There was a problem hiding this comment.
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?...
There was a problem hiding this comment.
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.
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.