Files
gitea-dynamic-runner/internal/taskscheduler/gate.go
T

32 lines
627 B
Go

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:
}
}