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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
db36d918fe
commit
66537f4e65
3 files changed
+28
-3
No files matched your search
@@ -152,7 +152,8 @@ func (s *Sessions) Get(ctx context.Context, token string) (*Session, error) {
|
|||||||
}
|
}
|
||||||
// Sliding expiry, but never beyond the absolute lifetime.
|
// Sliding expiry, but never beyond the absolute lifetime.
|
||||||
remaining := s.absolute - s.now().Sub(sess.CreatedAt)
|
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 nil, fmt.Errorf("auth: touch session: %w", err)
|
||||||
}
|
}
|
||||||
return sess, nil
|
return sess, nil
|
||||||
|
|||||||
@@ -281,7 +281,7 @@ func (s *Scheduler) Take(ctx context.Context, instance uuid.UUID, max int, wait
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
// BLPOP blocks server side and ignores ctx, so wait in slices.
|
// 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) {
|
if errors.Is(err, redis.Nil) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -306,6 +306,15 @@ func (s *Scheduler) Take(ctx context.Context, instance uuid.UUID, max int, wait
|
|||||||
return out, nil
|
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.
|
// 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 {
|
func (s *Scheduler) Release(ctx context.Context, region string, instance uuid.UUID) error {
|
||||||
// Marking the instance stale lets the dispatch script do the handover,
|
// Marking the instance stale lets the dispatch script do the handover,
|
||||||
|
|||||||
@@ -188,11 +188,26 @@ func TestTakeWaits(t *testing.T) {
|
|||||||
if err != nil || len(got) != 0 {
|
if err != nil || len(got) != 0 {
|
||||||
t.Fatalf("Take() on empty queue = %v, %v", got, err)
|
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)
|
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) {
|
func TestTakeReturnsWhenCancelled(t *testing.T) {
|
||||||
s, _ := newTest(t)
|
s, _ := newTest(t)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|||||||
Reference in new issue
Block a user