fix: gate scheduler task concurrency
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package taskscheduler
|
||||
|
||||
import "context"
|
||||
|
||||
// SingleFlightGate keeps at most one fetched task in flight. Release is
|
||||
// idempotent so repeated terminal updates cannot increase capacity.
|
||||
type SingleFlightGate struct {
|
||||
token chan struct{}
|
||||
}
|
||||
|
||||
func NewSingleFlightGate() *SingleFlightGate {
|
||||
gate := &SingleFlightGate{token: make(chan struct{}, 1)}
|
||||
gate.token <- struct{}{}
|
||||
return gate
|
||||
}
|
||||
|
||||
func (g *SingleFlightGate) Acquire(ctx context.Context) error {
|
||||
select {
|
||||
case <-g.token:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (g *SingleFlightGate) Release() {
|
||||
select {
|
||||
case g.token <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user