Skip to content

Fix HTTP 429 rate limiting in usage API polling - #6

Merged
stevensouza merged 1 commit into
mainfrom
stevensouza/fix-build-errors
Mar 2, 2026
Merged

stevensouza merged 1 commit into
mainfrom
stevensouza/fix-build-errors

Conversation

@stevensouza

Copy link
Copy Markdown
Owner

Summary

Fixed the HTTP 429 rate-limiting errors shown in Event Log by addressing two root causes:

  1. Session key feedback loop: API-driven session key rotations were triggering the Combine observer that restarted polling, causing 9-18+ requests/minute in bursts. Now only user-initiated credential changes restart polling.

  2. No rate-limit handling: Added exponential backoff for 429 and network errors, with the next poll delayed by min(interval * 2^consecutive_errors, 10 min). Backoff resets on success.

  3. Post-sleep connection errors: Added sleep/wake observers to pause polling before sleep and resume after a 15-second delay, preventing "connection offline" errors.

Changes

  • Sources/PingClaude/UsageService.swift: Added isUpdatingSessionKey flag, consecutiveErrors tracking, replaced repeating timer with self-rescheduling one-shot timer, added explicit 429 handling, added sleep/wake observers
  • Sources/PingClaude/Constants.swift: Added usageMaxBackoffSeconds constant

Testing

  • make build ✓
  • make lint ✓
  • make bundle ✓

Manual verification recommended: launch app and monitor Event Log for steady 60s polling without restart bursts.

🤖 Generated with Claude Code

Replace the repeating timer with a self-rescheduling one-shot timer to enable adaptive backoff. When a 429 (Too Many Requests) error or network failure occurs, the next poll is delayed with exponential backoff (capped at 10 minutes). Success resets backoff to normal.

Also prevent the session key update feedback loop: API-driven session key rotations no longer trigger the Combine observer that restarts polling. Only user-initiated credential changes restart polling.

Additionally, add sleep/wake handling: polling stops before system sleep and resumes after a 15-second delay on wake, preventing "connection offline" errors.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@stevensouza
stevensouza merged commit d04b8b6 into main Mar 2, 2026
2 checks passed
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.

1 participant