fix: gate scheduler task concurrency

This commit is contained in:
2026-09-20 20:53:58 +00:00
parent bca8096491
commit cbbe14d6b5
7 changed files with 111 additions and 3 deletions
+31
View File
@@ -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:
}
}
+29
View File
@@ -0,0 +1,29 @@
package taskscheduler
import (
"context"
"testing"
"time"
)
func TestSingleFlightGateBlocksUntilTerminalRelease(t *testing.T) {
gate := NewSingleFlightGate()
if err := gate.Acquire(context.Background()); err != nil {
t.Fatal(err)
}
blocked, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
if err := gate.Acquire(blocked); err == nil {
t.Fatal("second task acquired capacity before release")
}
gate.Release()
gate.Release()
if err := gate.Acquire(context.Background()); err != nil {
t.Fatal(err)
}
blockedAgain, cancelAgain := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancelAgain()
if err := gate.Acquire(blockedAgain); err == nil {
t.Fatal("duplicate terminal release increased capacity")
}
}
+13
View File
@@ -26,6 +26,7 @@ type PollerConfig struct {
type Poller struct {
Client PollClient
Scheduler *Scheduler
Gate *SingleFlightGate
Config PollerConfig
OnError func(error)
}
@@ -50,7 +51,14 @@ func (p Poller) Run(ctx context.Context) error {
errorBackoff = 5 * time.Second
}
var tasksVersion int64
haveLease := false
for {
if p.Gate != nil && !haveLease {
if err := p.Gate.Acquire(ctx); err != nil {
return nil
}
haveLease = true
}
response, err := p.Client.FetchTask(ctx, tasksVersion)
if err != nil {
if ctx.Err() != nil {
@@ -72,6 +80,10 @@ func (p Poller) Run(ctx context.Context) error {
tasksVersion = response.GetTasksVersion()
task := response.GetTask()
if task == nil {
if p.Gate != nil {
p.Gate.Release()
haveLease = false
}
if !wait(ctx, emptyBackoff) {
return nil
}
@@ -79,6 +91,7 @@ func (p Poller) Run(ctx context.Context) error {
}
for {
if err := p.Scheduler.Run(ctx, task); err == nil {
haveLease = false
break
} else {
if ctx.Err() != nil {