Port the trex-orbit Redis token-cache pattern so the KIS access token
survives daemon restarts and is shared across processes, avoiding KIS
token-issuance rate limits. KISBroker._ensure_token now resolves L1
in-memory -> L2 cache -> issue, storing issued tokens back to the cache.
- adapters/broker/token_cache.py: TokenCache protocol + RedisTokenCache
(key trex:broker:kis:{account_ref}:token, token+expiry+app_key suffix
JSON, ex=ttl, best-effort with graceful fallback) + InMemoryTokenCache.
- kis.py: optional token_cache injection.
- runtime config/daemon: TREX_KIS_REDIS_URL wires a RedisTokenCache.
- pyproject: redis>=5.
Verification: 96 passed, ruff clean — cache hit avoids issuance, cache is
shared across instances (restart/multi-process survival), backend errors
fall back to issuance (no regression), and app_key-suffix mismatch
invalidates a stale cache. Tested deterministically with an in-memory
double (no Redis server required). Processed via the minor-change fast
path: REQ-016 -> Implemented, BAT-005 (batch-lite) -> release-candidate.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1.7 KiB
1.7 KiB
BAT-005 Implementation
- Status: completed
- Batch: BAT-005
Plan Summary
- REQ-016: KISBroker 에 선택적 Redis 토큰 캐시(L1 인메모리 → L2 캐시 → 발급) + graceful fallback. orbit Redis 패턴 이식.
Changed Files
- trex_core/adapters/broker/token_cache.py (신규):
TokenCacheProtocol,RedisTokenCache(redis.from_url, 키trex:broker:kis:{account_ref}:token, JSON 토큰+만료+app_key suffix, ex=ttl, 예외 graceful),InMemoryTokenCache(더블/폴백). - trex_core/adapters/broker/kis.py:
token_cache주입,_ensure_token3-레벨(L1/L2/issue) + 발급 시 store. - trex_core/runtime/config.py:
KISConfig.redis_url+TREX_KIS_REDIS_URL. - trex_core/runtime/daemon.py: redis_url 있으면
RedisTokenCache주입. - pyproject.toml:
redis>=5의존. - tests/adapters/broker/test_token_cache.py (신규).
Execution Log
- TokenCache 추상화 + Redis/InMemory 구현(app_key suffix 안전, 만료 검증, 예외 graceful).
- KISBroker _ensure_token 3-레벨 + store.
- config/daemon wiring + pyproject redis.
- 테스트 작성, redis 라이브러리 설치 후 실행.
Validation
pytest tests/adapters/broker/test_token_cache.py: 4 passed (캐시 히트 HTTP 0 / 인스턴스 간 공유 / graceful fallback / app_key suffix 무효화).- 전체
pytest: 96 passed (기존 92 + 신규 4), ruff clean. - 실 Redis 서버 불요: 인메모리 캐시 더블 + httpx.post 스텁으로 결정적 검증. RedisTokenCache 의 redis.from_url 경로는 운영 시 사용(라이브러리 import OK 확인).
Remaining Risks
- 없음(저위험). Redis 장애 시 graceful fallback(발급 진행). 실 Redis 서버 통합 검증은 운영 환경에서(라이브러리/와이어링 확인 완료).