From 66537f4e651352f942d29f228b2d790fb442581f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Wed, 25 Nov 2026 22:58:54 +0100 Subject: [PATCH] fix: keep Redis timeouts below a second exact A checker's long poll ends with BLPOP waiting the rest of it, often below a second, which go-redis rounded up to one second with a warning in the log; the poll then ran up to a second too long. Such waits now pass BLPOP a fraction of a second, which Redis takes since 6.0. A session's sliding expiry, which can be below a second near its absolute end, uses PEXPIRE. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/auth/session.go | 3 ++- internal/schedule/schedule.go | 11 ++++++++++- internal/schedule/schedule_test.go | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/internal/auth/session.go b/internal/auth/session.go index f283914..b1be3ca 100644 --- a/internal/auth/session.go +++ b/internal/auth/session.go @@ -152,7 +152,8 @@ func (s *Sessions) Get(ctx context.Context, token string) (*Session, error) { } // Sliding expiry, but never beyond the absolute lifetime. remaining := s.absolute - s.now().Sub(sess.CreatedAt) - if err := s.rdb.Expire(ctx, k, min(s.idle, remaining)).Err(); err != nil { + // PEXPIRE: the rest of the lifetime may be below a second. + if err := s.rdb.PExpire(ctx, k, min(s.idle, remaining)).Err(); err != nil { return nil, fmt.Errorf("auth: touch session: %w", err) } return sess, nil diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index be11e75..e54d358 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -281,7 +281,7 @@ func (s *Scheduler) Take(ctx context.Context, instance uuid.UUID, max int, wait return nil, nil } // BLPOP blocks server side and ignores ctx, so wait in slices. - first, err := s.rdb.BLPop(ctx, min(left, time.Second), q).Result() + first, err := s.blpop(ctx, min(left, time.Second), q) if errors.Is(err, redis.Nil) { continue } @@ -306,6 +306,15 @@ func (s *Scheduler) Take(ctx context.Context, instance uuid.UUID, max int, wait return out, nil } +// blpop is BLPOP with a timeout below a second, which go-redis would round +// up to one (Redis takes fractions of seconds since 6.0). +func (s *Scheduler) blpop(ctx context.Context, timeout time.Duration, key string) ([]string, error) { + if timeout >= time.Second { + return s.rdb.BLPop(ctx, timeout, key).Result() + } + return s.rdb.Do(ctx, "BLPOP", key, strconv.FormatFloat(timeout.Seconds(), 'f', 3, 64)).StringSlice() +} + // Release returns instance's queued runs to its region and forgets it. func (s *Scheduler) Release(ctx context.Context, region string, instance uuid.UUID) error { // Marking the instance stale lets the dispatch script do the handover, diff --git a/internal/schedule/schedule_test.go b/internal/schedule/schedule_test.go index 590d071..77b6dff 100644 --- a/internal/schedule/schedule_test.go +++ b/internal/schedule/schedule_test.go @@ -188,11 +188,26 @@ func TestTakeWaits(t *testing.T) { if err != nil || len(got) != 0 { t.Fatalf("Take() on empty queue = %v, %v", got, err) } - if waited := time.Since(start); waited < 250*time.Millisecond { + // Not rounded up to the second that BLPOP counts in. + if waited := time.Since(start); waited < 250*time.Millisecond || waited > 800*time.Millisecond { t.Errorf("Take() returned after %v, want it to wait about 300ms", waited) } } +func TestTakeGetsAJobDuringAShortWait(t *testing.T) { + s, _ := newTest(t) + ctx := context.Background() + in, monitor := uuid.New(), uuid.New() + go func() { + time.Sleep(100 * time.Millisecond) + s.rdb.RPush(ctx, jobsPrefix+in.String(), monitor.String()+"|1800000000000") + }() + got, err := s.Take(ctx, in, 10, 600*time.Millisecond) + if err != nil || len(got) != 1 || got[0].MonitorID != monitor { + t.Fatalf("Take() = %v, %v; want the job pushed while waiting", got, err) + } +} + func TestTakeReturnsWhenCancelled(t *testing.T) { s, _ := newTest(t) ctx, cancel := context.WithCancel(context.Background())