Skip to content

Conversation

@minhyeokkkim
Copy link

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을 보내 동작하는지 확인했슴니다

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Cloud User seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jit-ci
Copy link

jit-ci bot commented Nov 24, 2025

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

Copy link

@tkdwl05 tkdwl05 left a comment

Choose a reason for hiding this comment

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

확인함

@sundb
Copy link
Collaborator

sundb commented Nov 25, 2025

@minhyeokkkim please use english.

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.

4 participants