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.
REDIS 서버에 클라이언트 단위로 레이트 리미트 기능을 추가하고자 함.
확인을 위해 클라이언트 별로 1초당 최대 20개 명령만 허용 ( 수정하여 늘리거나 줄일 수 있음) , 초과 시 해당 초 동안은 명령을 거부하게끔 함
관련 로직을 모듈로 분리 해서 구현했어요. rate_limit.c, 헤더파일 등등
윈도우 리셋, 카운트 , 허용 거부 판단, 디버깅 로그 출력
서버에 레이트 리밑 윈도우 크기를 server.rlimit_window_sec 지정 기본값 1초,
윈도우 안에서 허용한 최대 요청 수 rlimit_max_requests 기본값 20
redis.conf나 config set을 통해 변경 가능
클라이언트 ID를 키로 사용하는 내부 해시 구조를 사용해서 클라이언트마다 window_start, count로 윈도우가 시작된 시간과 해당 윈도우동안 허용된 명령 수를 관리하고 현재 시간과 window_start의 차이가 rlimit_window_sce 이상이면 즉 1초를 넘어가면 남은 명령은 버림 큐잉을 안하기 때문. 새 윈도우로 간주해서 widow_start, count를 초기화
processCommand 함수에 clientRatelimitCheck를 호출해서 허용 시 카운트를 증가시키며 로직대로 명령을 실행하고 거부 시 명령을 실행하지 않고 에러를 반환하도록 처리했어요
디버깅은
RLDBG id= tag=processCommand count=<현재 카운트> max=<허용 최대치> win=<윈도우 유지 여부> -> 이렇게 뜨게끔
최초 초과 시 한 번만 남기는 경고 로그:
Rate limit exceeded: client-id=..., part="processCommand", info=..., count=<누적>, in sec (limit=)
이를 통해 실제 클라이언트가 얼마만큼의 시간동안 몇개의 요청만 허용되는지 서버 로그에 출력되어 확인할 수 있음
검증은 수많은 PING을 보내 동작하는지 확인했슴니다