Skip to content

fix(ratelimit): keep the counter ttl when the reset header is missing - #296

Merged
juninmd merged 1 commit into
masterfrom
claude/ratelimit-keep-ttl
Nov 22, 2026
Merged

juninmd merged 1 commit into
masterfrom
claude/ratelimit-keep-ttl

Conversation

@juninmd

@juninmd juninmd commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Requested by Antonio · project thread

Summary

Por quê: GitHubRateLimiter.observe() reconcilia o contador no Redis com SET, e o SET do Redis (sem KEEPTTL) remove o TTL da chave. O TTL só era restaurado a partir de x-ratelimit-reset. Se uma resposta trouxer x-ratelimit-remaining sem x-ratelimit-reset, o contador vira permanente: quando passa do orçamento, acquire() passa a lançar GitHubRateBudgetExhausted ("resets in ~-1s") em toda chamada, até alguém apagar a chave à mão. O GitHub costuma mandar os dois headers, então é latente, mas o efeito é a frota toda parada sem recuperação automática.

O que mudou:

  • ts/src/github/ratelimit.ts: quando o contador foi regravado e não há x-ratelimit-reset, reaplica expire(key, window) (janela configurada, 3600 s por padrão). Com o header, nada muda.
  • ts/tests/lote1.test.ts: teste com um Redis falso fiel a esse comportamento (SET apaga o TTL): depois de observe() sem o header de reset, o TTL precisa ser positivo.

Quality Checklist

  • User-facing behavior is described clearly.
  • Security impact was considered, including secrets, auth, and data exposure. (nenhum)
  • Performance impact was considered for startup, runtime, bundle size, or query cost. (um EXPIRE extra só nesse caso)
  • Logs, errors, and operational visibility are useful enough to debug failures.
  • UI changes were checked for keyboard access, responsive layout, and readable states. (N/A)
  • Tests, smoke checks, or manual verification are listed below.

Verification

Como foi verificado

Antes da correção:

$ cd ts && bun test tests/lote1.test.ts -t "reset header"
Expected: > 0
Received: -1
 0 pass
 1 fail

Depois:

$ bun test tests/lote1.test.ts
 18 pass
 0 fail
$ bun test
 293 pass
 0 fail
$ bun run typecheck   -> tsc --noEmit sem erros

Generated by Claude Code

observe() reconciles the Redis counter with SET, which drops the key's
TTL. Expiry was only restored from x-ratelimit-reset, so a response
without that header left the counter permanent: once it passed the
budget, acquire() threw GitHubRateBudgetExhausted on every call until
someone deleted the key. Fall back to the configured window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn6JQLtmA8NV75CtdV373W
@juninmd juninmd self-assigned this Nov 22, 2026
@juninmd
juninmd merged commit e7f0552 into master Nov 22, 2026
3 checks passed
@juninmd
juninmd deleted the claude/ratelimit-keep-ttl branch November 22, 2026 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants