DOCS/troubleshooting/250915_rate_limiting_미구현.md
happybell80 72d36f01cb docs: Rate Limiting 해결 완료 문서 업데이트
- 상태:  해결 완료로 변경
- 최종 테스트 결과 추가 (429 정상 반환)
- Critical 이슈 목록에서 제거
- 2025-09-15 해결 완료

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-15 15:05:14 +09:00

4.0 KiB

Rate Limiting 미구현

작성일: 2025-09-15

작성자: 51123 서버 관리자

상태: 해결 완료


1. 현재 상태

확인된 사실

  • Rate Limiting 없음: Gateway /api/chat 엔드포인트 무제한 호출 가능
  • 사용 모델: GPT-4 (/home/admin/ivada_project/rb8001/app/core/config.py Line 31)
  • API 호출: openai_handler.py Line 16에서 OpenAI API 클라이언트 초기화

확인된 플로우

Frontend → Gateway(51123:8100) → rb8001(51124:8001) → OpenAI API
  • Gateway main.py Line 169-244: /api/chat 프록시
  • Gateway Line 172: JWT 검증만 있음, Rate Limiting 없음

2. 보안 이슈

Rate Limiting 필요성

  • 무제한 API 호출로 인한 서비스 부하 가능
  • DDoS 공격 취약점 존재
  • 사용자별 요청 제한 없음

3. 관련 파일

파일 위치 역할
openai_handler.py 51124:/home/admin/ivada_project/rb8001/app/llm/ OpenAI API 호출
main.py 51123:/home/admin/robeing-gateway/app/ 프록시, Rate Limiting 구현 위치
config.py 51124:/home/admin/ivada_project/rb8001/app/core/ GPT-4 모델 설정

4. 사용 가능한 인프라

  • Redis 실행 중: auth-redis 컨테이너 (docker ps 확인)
  • 포트: 6379/tcp

5. 해결 방법

수정 필요 파일

  1. requirements.txt

    • 위치: /home/admin/robeing-gateway/requirements.txt
    • 추가 필요 패키지:
      • redis==5.0.1
      • slowapi==0.1.9
  2. main.py

    • 위치: /home/admin/robeing-gateway/app/main.py
    • 수정 대상 엔드포인트: /api/chat (Line 169)
    • 현재 상태: JWT 검증만 있음 (Line 172, get_verified_user 함수)

Rate Limiting 구현 방안

  • 제한 정책: 분당 20회 (사용자별)
  • 구분 기준: JWT 토큰 (Authorization 헤더)
  • Redis 활용: 기존 auth-redis 컨테이너 사용
  • 라이브러리: slowapi (FastAPI용 rate limiter)

구현 방법

1. 새 파일 생성

  • 파일명: /home/admin/robeing-gateway/app/rate_limiter.py
  • 역할: Rate Limiting 로직 전체 관리
  • 내용: Redis 연결, Limiter 초기화, 예외 처리

2. main.py 수정 (총 4줄 추가)

# Import 섹션 (Line 14 아래)
from app.rate_limiter import limiter, rate_limit_handler  # 1줄

# App 초기화 후 (Line 50-60 사이)
app.state.limiter = limiter  # 1줄
app.add_exception_handler(RateLimitExceeded, rate_limit_handler)  # 1줄

# /api/chat 엔드포인트 (Line 169)
@limiter.limit("20 per minute")  # 1줄 추가

확인 완료 사항

  • 패키지 버전: redis==5.0.1, slowapi==0.1.9
  • Redis 연결:
    • 호스트명: auth-redis
    • 포트: 6379
    • 네트워크: appnet (robeing-gateway와 같은 네트워크)
    • IP: 172.21.0.2

배포

  • Docker 재빌드: docker compose down && docker compose up -d --build
  • 위치: /home/admin/robeing-gateway/

6. 테스트 결과 (2025-09-15 14:40)

구현 상태

  • 커밋: d8efa5a
  • 배포: Gitea Actions 자동 배포 완료

테스트 결과

  • Rate Limiting 작동: 분당 20회 제한 정상 작동
  • 1-20번 요청: 200 OK
  • 21번 이후: 500 Internal Server Error

발견된 문제

  • 에러 코드 문제: Rate Limit 초과 시 500 에러 반환
  • 정상 동작: 429 Too Many Requests 반환해야 함
  • 원인: rate_limit_handler 예외 처리 미작동

최종 해결 (2025-09-15 14:50)

  • 커밋: 9a9d7df
  • rate_limiter.py 에러 핸들러 수정 완료
  • 429 상태 코드 정상 반환 확인
  • Retry-After 헤더 60초 설정 확인

7. 최종 테스트 결과

테스트 완료

  • 1-20번 요청: 200 OK
  • 21번 이후: 429 Too Many Requests
  • 에러 메시지: "Rate limit exceeded: 429: 20 per 1 minute"
  • Retry-After: 60초 헤더 포함

구현 완료 사항

  • IP 기반 Rate Limiting (get_remote_address)
  • 분당 20회 제한 정상 작동
  • 적절한 HTTP 상태 코드 반환
  • 프로덕션 배포 완료