fix(rate_limiter): enforce official KIS limit, lock-free sleep, safer defaults#40
Merged
Merged
Conversation
… defaults
API rate limiting이 KIS 공식 한도(20 RPS / 1000 RPM)에 닿지 않도록 안전 마진을
확보하고, 동시 호출 시에도 sliding window 위반이 발생하지 않도록 수정.
핵심 변경:
1. 기본값 강화 (75~80% 마진)
- RPS: 18 → 15 (공식 20의 75%)
- RPM: 900 → 800 (공식 1000의 80%)
- min_interval_ms: 55 → 70 (15 RPS 이론적 67ms + 3ms jitter)
- burst_size: 10 → 3 (priority>=1 시 effective RPS=18로 공식 20 안전 보장)
2. 공식 한도 절대 위반 방지
- 사용자가 RPS>20 또는 RPM>1000 전달 시 자동 클램프
- KIS_OFFICIAL_RPS_LIMIT(20), KIS_OFFICIAL_RPM_LIMIT(1000) 상수 도입
- 이전 priority>=1 시 RPS + burst까지 허용해 공식 한도 초과 가능 버그 수정
- effective RPS = min(RPS + burst, KIS_OFFICIAL_RPS_LIMIT)로 강제
3. 동시성 안전 (lock-free sleep)
- acquire() 내부에서 time.sleep()을 lock 바깥에서 실행
- 이전 구현은 sleep을 lock 안에서 호출해 한 스레드의 대기가 다른 스레드를
완전히 블록 → 동시 N개 호출 시 부하 폭증
- 슬롯 예약(reserved_until) 방식으로 동시 호출 시 각 호출에 서로 다른 미래
슬롯을 분배
4. Sliding window 한도 정합성
- min_interval에 (1.0/RPS + 0.001s) floor 강제
- 슬롯 간격이 정확히 1/RPS면 윈도우 경계와 겹쳐 N+1개가 1초 안에 들어올 수
있는 corner case 차단
- 윈도우 끝에 20ms safety padding (OS sleep undersleep 대비)
5. 중복 설정 제거 (DRY)
- client.py와 agent.py에 하드코딩된 동일한 18/900/55ms 기본값을 제거
- 두 곳 모두 get_global_rate_limiter() 호출 시 모듈 상수 DEFAULT_* 사용
검증 (12개 신규 단위 테스트):
- 공식 한도 클램프 (RateLimiter, set_limits)
- 동시 12개 호출 시 1초 윈도우에 5개 이하 (5 RPS 케이스)
- 동시 30개 호출 시 1초 윈도우에 15개 이하 (기본 15 RPS)
- priority>=1 + 큰 burst여도 공식 RPS 한도(20) 초과 안 함
- 슬립이 lock 바깥에서 일어나 get_current_rate() 호출이 즉시 반환
- 전역 싱글턴이 동시 20개 스레드에서도 단일 인스턴스 보장
문서: docs/RATE_LIMITER_GUIDE.md 갱신.
📊 Coverage Analysis Report
✅ All files meet the target coverage of 70%! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
API rate limiting이 KIS 공식 한도(20 RPS / 1000 RPM)에 닿지 않도록 안전 마진 확보, 동시 호출 시 sliding window 위반 방지, lock-free sleep으로 동시 부하 차단.
Why
Changes
Test plan