-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package taskgroup | ||
|
||
import "sync/atomic" | ||
|
||
// A Throttle rate-limits the number of concurrent goroutines that can execute | ||
// in parallel to some fixed number. A zero Throttle is ready for use, but | ||
// imposes no limit on parallel execution. See [Throttle.Enter] for use. | ||
type Throttle struct { | ||
adm chan struct{} | ||
} | ||
|
||
// NewThrottle constructs a [Throttle] with a capacity of n goroutines. | ||
// If n ≤ 0, the resulting Throttle imposes no limit. | ||
func NewThrottle(n int) Throttle { | ||
if n <= 0 { | ||
return Throttle{} | ||
} | ||
return Throttle{adm: make(chan struct{}, n)} | ||
} | ||
|
||
// 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 commentThe 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 commentThe 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: |
||
if t.adm == nil { | ||
return func() {} | ||
} | ||
t.adm <- struct{}{} | ||
var done atomic.Bool | ||
return func() { | ||
if done.CompareAndSwap(false, true) { | ||
<-t.adm | ||
} | ||
} | ||
} | ||
|
||
// A Leaver returns an in-use throttle slot to its underlying [Throttle]. | ||
// It is safe to call a Leaver multiple times; the slot will only be returned | ||
// once. | ||
type Leaver func() | ||
|
||
// Leave returns the slot to its [Throttle]. This is a legibility alias for | ||
// calling f. | ||
func (f Leaver) Leave() { f() } | ||
|
||
// Limit returns a function that starts each [Task] passed to it in g, | ||
// respecting the rate limit imposed by t. Each call to Limit yields a fresh | ||
// start function, and all the functions returned share the capacity of t. | ||
func (t Throttle) Limit(g *Group) func(Task) { | ||
return func(task Task) { | ||
slot := t.Enter() | ||
g.Go(func() error { | ||
defer slot.Leave() | ||
return task() | ||
}) | ||
} | ||
} |
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.