Skip to content

feat(market): add retry with exponential backoff to all API calls#12

Merged
ShuhaoQing merged 1 commit intomainfrom
feat/api-retry
Apr 7, 2026
Merged

feat(market): add retry with exponential backoff to all API calls#12
ShuhaoQing merged 1 commit intomainfrom
feat/api-retry

Conversation

@ShuhaoQing
Copy link
Copy Markdown
Owner

@ShuhaoQing ShuhaoQing commented Apr 7, 2026

Summary

  • Add tenacity==9.1.4 for retry logic with exponential backoff + jitter
  • New haoinvest/http_retry.py module with shared @api_retry decorator (3 attempts, 1s→2s→4s, jitter ±0.5s)
  • Applied to all external API calls: Sina, Tencent, Eastmoney, CoinGecko, exchangerate-api
  • Only retries transient errors (connection errors, timeouts, 5xx); 4xx are not retried
  • For functions with existing try/except (tencent get_valuation, eastmoney get_financial_indicators), extracted HTTP calls into separate retry-wrapped helpers to preserve graceful fallback behavior

Test plan

  • All 290 existing unit tests pass
  • New tests/test_http_retry.py with 15 tests covering:
    • _is_retryable predicate for all exception types (requests + httpx)
    • Retry-then-succeed behavior
    • No retry on non-retryable errors (ValueError, 4xx)
    • Give-up after max attempts
  • Manual verification: run haoinvest market quote 600519 with network enabled

🤖 Generated with Claude Code

External API calls (Sina, Tencent, Eastmoney, CoinGecko, exchangerate)
had no retry logic, causing failures on transient network errors.
Add tenacity-based retry decorator with exponential backoff + jitter
(3 attempts, 1s→2s→4s) for connection errors, timeouts, and 5xx.
4xx client errors are not retried.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ShuhaoQing ShuhaoQing merged commit 66d1732 into main Apr 7, 2026
1 check passed
@ShuhaoQing ShuhaoQing deleted the feat/api-retry branch April 7, 2026 04:05
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