Limit 설정 구조체
Location별 Rate Limiting 정책 파라미터를 담는 구조체다. 하나의 limit_req 지시어에 대응하며, burst와 delay 값으로 트래픽 급증 시 처리 방식을 결정한다.
구조체 정의
src/http/modules/ngx_http_limit_req_module.c
typedef struct {
ngx_shm_zone_t *shm_zone;
/* integer value, 1 corresponds to 0.001 r/s */
ngx_uint_t burst;
ngx_uint_t delay;
} ngx_http_limit_req_limit_t;각 필드의 역할
shm_zone
컨텍스트(ngx_http_limit_req_ctx_t)가 저장된 공유 메모리 zone을 가리킨다. 이 포인터를 통해 zone 이름, 크기, 그리고 zone에 저장된 런타임 컨텍스트에 접근할 수 있다.
ctx = limit->shm_zone->data; // zone에서 컨텍스트 가져오기burst
순간적으로 허용할 수 있는 최대 초과 요청 수다. excess > burst이면 요청을 즉시 거부(503)한다. 값은 1000배 스케일링되어 저장된다 (예: burst=20 → 20000).
delay
지연 처리를 시작하는 임계값이다. excess > delay이면 요청을 지연시켜 부드럽게 처리한다. 값은 1000배 스케일링되어 저장된다 (예: delay=10 → 10000).
burst와 delay의 관계
src/http/modules/ngx_http_limit_req_module.c
static ngx_msec_t ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n, ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit) {
// ...
// 초기 delay 체크
if ((ngx_uint_t) excess <= (*limit)->delay) {
max_delay = 0;
}
while (n--) {
// ...
if ((ngx_uint_t) excess <= limits[n].delay) { // excess가 delay 이하이면 => 지연 없음
continue;
}
delay = (excess - limits[n].delay) * 1000 / ctx->rate; // excess가 delay 초과하면 => 지연 계산
}
}account 함수에서는 먼저 excess가 delay 이하인지 확인하고, 초과하는 경우에만 지연 시간을 계산한다.
참고 자료
Last updated on