Skip to content

feat: Gemini API 호출 전 유저별 Rate Limit 검증 추가#187

Merged
chwwwon merged 6 commits intodevelopfrom
refactor/186-recipe-ratelimit
Mar 30, 2026
Merged

feat: Gemini API 호출 전 유저별 Rate Limit 검증 추가#187
chwwwon merged 6 commits intodevelopfrom
refactor/186-recipe-ratelimit

Conversation

@chwwwon
Copy link
Copy Markdown
Contributor

@chwwwon chwwwon commented Mar 28, 2026

Issue

#186

Description

  • Gemini API 429(Rate Limit) 오류를 서버 레벨에서 선제적으로 차단하기 위해 유저별 요청 제한 로직 추가
  • Gemini 호출 전에 유저별 요청 횟수를 검증하여, API quota 낭비 차단 가능
  • 유저 단위로 독립 카운팅 수행

Changes

AiRateLimitService.java

  • 유저별 요청 타임스탬프를 ConcurrentHashMap으로 관리
  • 오래된 요청 자동 제거
  • 제한 초과 시 AI_RATE_LIMIT_EXCEEDED 예외 발생
  • 기본 설정: 유저당 1분에 3회

AiRecipeService.java

  • AiRateLimitService 의존성 주입
  • generateInitialRecipe(): Gemini 호출 직전 rateLimitService.validate(userId) 삽입
  • regenerateRecipe(): 동일하게 Gemini 호출 직전 삽입

AiRecipeServiceTest.java

  • @mock AiRateLimitService 추가 (누락 시 NPE)
  • setUp()에 기본 통과 설정 추가
  • RateLimitIntegration 테스트 클래스 신규 추가 (6개 케이스)

Test Checklist

AiRateLimitService (단위)

  • 같은 유저가 1분 내 3회 이하 요청 시 정상 통과
  • 같은 유저가 1분 내 4회 요청 시 AI_RATE_LIMIT_EXCEEDED 예외 발생
  • 1분 경과 후 카운터 초기화 및 재요청 가능
  • 서로 다른 유저의 카운터는 독립적으로 동작

AiRecipeService + AiRateLimitService 연동 (Mockito)

  • regenerateRecipe() 호출 시 validate(userId) 1회 실행 확인
  • Rate Limit 초과 시 Gemini API 미호출 확인
  • 이미 완료된 세션 / 재시도 횟수 초과 시 Rate Limit 검증 전에 예외 발생 확인
  • 유저A 차단이 유저B에게 영향 없음 확인

기존 테스트

  • 기존 adoptRecipe 관련 테스트 전체 통과 확인
  • COOKING / USE_EXPIRING_INGREDIENT 목표 연동 테스트 영향 없음 확인

@chwwwon chwwwon self-assigned this Mar 28, 2026
@chwwwon chwwwon requested review from 0-x-14 and hyunj24 March 28, 2026 15:59
Copy link
Copy Markdown
Contributor

@hyunj24 hyunj24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!! 리뷰 확인해주세용

Copy link
Copy Markdown
Member

@0-x-14 0-x-14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다 충돌 난 거만 확인부탁드려요!

@chwwwon chwwwon merged commit d40e843 into develop Mar 30, 2026
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.

3 participants