fix: add token refresh cooldown to prevent EGW00133 cascading failures (issue #54) #55

Merged
jihoson merged 1 commits from feature/issue-54-token-refresh-cooldown into main 2026-02-05 00:46:06 +09:00
Collaborator

Summary

  • Prevents cascading failures when token refresh hits KIS API's 1/minute limit
  • Adds 60-second cooldown between refresh attempts
  • Clear error messages tell caller how long to wait

Problem

When token refresh fails with EGW00133 (1/minute limit), every subsequent API call triggers another refresh attempt, causing:

  • Cascading failures across all API calls
  • System completely unusable
  • No clear indication of what's wrong

Solution

Enforce cooldown period between refresh attempts:

  • Track last refresh attempt time
  • Block new attempts within 60s cooldown
  • Provide clear error message with remaining wait time

Changes

  • src/broker/kis_api.py:58-61 - Add cooldown tracking variables
  • src/broker/kis_api.py:102-111 - Enforce cooldown logic
  • tests/test_broker.py - Add 2 new cooldown tests

Before/After

Before:

INFO: Refreshing KIS access token
WARNING: Token refresh failed (403): {"error_code":"EGW00133"}
INFO: Refreshing KIS access token  # ❌ Immediate retry
WARNING: Token refresh failed (403): {"error_code":"EGW00133"}
INFO: Refreshing KIS access token  # ❌ Another retry
WARNING: Token refresh failed (403): {"error_code":"EGW00133"}
... (repeats for every API call)

After:

INFO: Refreshing KIS access token
WARNING: Token refresh failed (403): {"error_code":"EGW00133"}
WARNING: Token refresh on cooldown. Retry in 58.3s (KIS allows 1/minute)  # ✅ Clear message
... (system waits, no cascading failures)

Test Results

  • All 285 tests pass
  • test_token_refresh_cooldown_prevents_rapid_retries - Verifies cooldown blocks rapid retries
  • test_token_refresh_allowed_after_cooldown - Verifies refresh works after cooldown expires
  • Existing token management tests still pass

Implementation Details

  • Cooldown starts on attempt, not just failures (prevents hitting limit)
  • 60s matches KIS API limit (1 refresh per minute)
  • Compatible with existing logic: Works with token expiry checking and concurrent refresh lock
  • Clear error messages: Tells caller exactly how long to wait

Benefits

  1. Prevents cascading failures - System doesn't repeatedly hit rate limit
  2. Clear diagnostics - Error messages show exactly what's wrong
  3. Graceful degradation - System can recover after cooldown expires
  4. No breaking changes - Fully compatible with existing code

Closes #54

🤖 Generated with Claude Code

## Summary - Prevents cascading failures when token refresh hits KIS API's 1/minute limit - Adds 60-second cooldown between refresh attempts - Clear error messages tell caller how long to wait ## Problem When token refresh fails with `EGW00133` (1/minute limit), every subsequent API call triggers another refresh attempt, causing: - Cascading failures across all API calls - System completely unusable - No clear indication of what's wrong ## Solution Enforce cooldown period between refresh attempts: - Track last refresh attempt time - Block new attempts within 60s cooldown - Provide clear error message with remaining wait time ## Changes - **src/broker/kis_api.py:58-61** - Add cooldown tracking variables - **src/broker/kis_api.py:102-111** - Enforce cooldown logic - **tests/test_broker.py** - Add 2 new cooldown tests ## Before/After **Before:** ``` INFO: Refreshing KIS access token WARNING: Token refresh failed (403): {"error_code":"EGW00133"} INFO: Refreshing KIS access token # ❌ Immediate retry WARNING: Token refresh failed (403): {"error_code":"EGW00133"} INFO: Refreshing KIS access token # ❌ Another retry WARNING: Token refresh failed (403): {"error_code":"EGW00133"} ... (repeats for every API call) ``` **After:** ``` INFO: Refreshing KIS access token WARNING: Token refresh failed (403): {"error_code":"EGW00133"} WARNING: Token refresh on cooldown. Retry in 58.3s (KIS allows 1/minute) # ✅ Clear message ... (system waits, no cascading failures) ``` ## Test Results - ✅ All 285 tests pass - ✅ `test_token_refresh_cooldown_prevents_rapid_retries` - Verifies cooldown blocks rapid retries - ✅ `test_token_refresh_allowed_after_cooldown` - Verifies refresh works after cooldown expires - ✅ Existing token management tests still pass ## Implementation Details - **Cooldown starts on attempt**, not just failures (prevents hitting limit) - **60s matches KIS API limit** (1 refresh per minute) - **Compatible with existing logic**: Works with token expiry checking and concurrent refresh lock - **Clear error messages**: Tells caller exactly how long to wait ## Benefits 1. **Prevents cascading failures** - System doesn't repeatedly hit rate limit 2. **Clear diagnostics** - Error messages show exactly what's wrong 3. **Graceful degradation** - System can recover after cooldown expires 4. **No breaking changes** - Fully compatible with existing code Closes #54 🤖 Generated with [Claude Code](https://claude.com/claude-code)
agentson added 1 commit 2026-02-05 00:37:49 +09:00
fix: add token refresh cooldown to prevent EGW00133 cascading failures (issue #54)
Some checks failed
CI / test (pull_request) Has been cancelled
a56adcd342
Prevents rapid retry attempts when token refresh hits KIS API's
1-per-minute rate limit (EGW00133: 접근토큰 발급 잠시 후 다시 시도하세요).

Changes:
- src/broker/kis_api.py:58-61 - Add cooldown tracking variables
- src/broker/kis_api.py:102-111 - Enforce 60s cooldown between refresh attempts
- tests/test_broker.py - Add cooldown behavior tests

Before:
- Token refresh fails with EGW00133
- Every API call triggers another refresh attempt
- Cascading failures, system unusable

After:
- Token refresh fails with EGW00133 (first attempt)
- Subsequent attempts blocked for 60s with clear error
- System knows to wait, prevents cascading failures

Test Results:
- All 285 tests pass
- New tests verify cooldown behavior
- Existing token management tests still pass

Implementation Details:
- Cooldown starts on refresh attempt (not just failures)
- Clear error message tells caller how long to wait
- Compatible with existing token expiry + locking logic

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
jihoson merged commit 10b6e34d44 into main 2026-02-05 00:46:06 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#55