Klaus Demo nginx / 61591d1
rename "lz" to "lr" in variable names Igor Sysoev 13 years ago
1 changed file(s) with 64 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
3737
3838
3939 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
40 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lzcf,
41 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lzp);
40 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf,
41 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lrp);
4242 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
4343 ngx_uint_t n);
4444
114114 ngx_rbtree_node_t *node;
115115 ngx_http_variable_value_t *vv;
116116 ngx_http_limit_req_ctx_t *ctx;
117 ngx_http_limit_req_node_t *lz;
118 ngx_http_limit_req_conf_t *lzcf;
117 ngx_http_limit_req_node_t *lr;
118 ngx_http_limit_req_conf_t *lrcf;
119119
120120 if (r->main->limit_req_set) {
121121 return NGX_DECLINED;
122122 }
123123
124 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
125
126 if (lzcf->shm_zone == NULL) {
124 lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
125
126 if (lrcf->shm_zone == NULL) {
127127 return NGX_DECLINED;
128128 }
129129
130 ctx = lzcf->shm_zone->data;
130 ctx = lrcf->shm_zone->data;
131131
132132 vv = ngx_http_get_indexed_variable(r, ctx->index);
133133
157157
158158 ngx_http_limit_req_expire(ctx, 1);
159159
160 rc = ngx_http_limit_req_lookup(lzcf, hash, vv->data, len, &lz);
161
162 if (lz) {
163 ngx_queue_remove(&lz->queue);
164
165 ngx_queue_insert_head(ctx->queue, &lz->queue);
166
167 excess = lz->excess;
160 rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &lr);
161
162 if (lr) {
163 ngx_queue_remove(&lr->queue);
164
165 ngx_queue_insert_head(ctx->queue, &lr->queue);
166
167 excess = lr->excess;
168168
169169 } else {
170170 excess = 0;
186186 if (rc == NGX_AGAIN) {
187187 ngx_shmtx_unlock(&ctx->shpool->mutex);
188188
189 if (lzcf->nodelay) {
189 if (lrcf->nodelay) {
190190 return NGX_DECLINED;
191191 }
192192
227227 }
228228 }
229229
230 lz = (ngx_http_limit_req_node_t *) &node->color;
230 lr = (ngx_http_limit_req_node_t *) &node->color;
231231
232232 node->key = hash;
233 lz->len = (u_char) len;
233 lr->len = (u_char) len;
234234
235235 tp = ngx_timeofday();
236 lz->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
237
238 lz->excess = 0;
239 ngx_memcpy(lz->data, vv->data, len);
236 lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
237
238 lr->excess = 0;
239 ngx_memcpy(lr->data, vv->data, len);
240240
241241 ngx_rbtree_insert(ctx->rbtree, node);
242242
243 ngx_queue_insert_head(ctx->queue, &lz->queue);
243 ngx_queue_insert_head(ctx->queue, &lr->queue);
244244
245245 done:
246246
273273 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
274274 {
275275 ngx_rbtree_node_t **p;
276 ngx_http_limit_req_node_t *lzn, *lznt;
276 ngx_http_limit_req_node_t *lrn, *lrnt;
277277
278278 for ( ;; ) {
279279
287287
288288 } else { /* node->key == temp->key */
289289
290 lzn = (ngx_http_limit_req_node_t *) &node->color;
291 lznt = (ngx_http_limit_req_node_t *) &temp->color;
292
293 p = (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0)
290 lrn = (ngx_http_limit_req_node_t *) &node->color;
291 lrnt = (ngx_http_limit_req_node_t *) &temp->color;
292
293 p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)
294294 ? &temp->left : &temp->right;
295295 }
296296
310310
311311
312312 static ngx_int_t
313 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lzcf, ngx_uint_t hash,
314 u_char *data, size_t len, ngx_http_limit_req_node_t **lzp)
313 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
314 u_char *data, size_t len, ngx_http_limit_req_node_t **lrp)
315315 {
316316 ngx_int_t rc, excess;
317317 ngx_time_t *tp;
319319 ngx_msec_int_t ms;
320320 ngx_rbtree_node_t *node, *sentinel;
321321 ngx_http_limit_req_ctx_t *ctx;
322 ngx_http_limit_req_node_t *lz;
323
324 ctx = lzcf->shm_zone->data;
322 ngx_http_limit_req_node_t *lr;
323
324 ctx = lrcf->shm_zone->data;
325325
326326 node = ctx->rbtree->root;
327327 sentinel = ctx->rbtree->sentinel;
341341 /* hash == node->key */
342342
343343 do {
344 lz = (ngx_http_limit_req_node_t *) &node->color;
345
346 rc = ngx_memn2cmp(data, lz->data, len, (size_t) lz->len);
344 lr = (ngx_http_limit_req_node_t *) &node->color;
345
346 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
347347
348348 if (rc == 0) {
349349
350350 tp = ngx_timeofday();
351351
352352 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
353 ms = (ngx_msec_int_t) (now - lz->last);
354
355 excess = lz->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
353 ms = (ngx_msec_int_t) (now - lr->last);
354
355 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
356356
357357 if (excess < 0) {
358358 excess = 0;
359359 }
360360
361 lz->excess = excess;
362 lz->last = now;
363
364 *lzp = lz;
365
366 if ((ngx_uint_t) excess > lzcf->burst) {
361 lr->excess = excess;
362 lr->last = now;
363
364 *lrp = lr;
365
366 if ((ngx_uint_t) excess > lrcf->burst) {
367367 return NGX_BUSY;
368368 }
369369
381381 break;
382382 }
383383
384 *lzp = NULL;
384 *lrp = NULL;
385385
386386 return NGX_DECLINED;
387387 }
396396 ngx_queue_t *q;
397397 ngx_msec_int_t ms;
398398 ngx_rbtree_node_t *node;
399 ngx_http_limit_req_node_t *lz;
399 ngx_http_limit_req_node_t *lr;
400400
401401 tp = ngx_timeofday();
402402
416416
417417 q = ngx_queue_last(ctx->queue);
418418
419 lz = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
419 lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
420420
421421 if (n++ != 0) {
422422
423 ms = (ngx_msec_int_t) (now - lz->last);
423 ms = (ngx_msec_int_t) (now - lr->last);
424424 ms = ngx_abs(ms);
425425
426426 if (ms < 60000) {
427427 return;
428428 }
429429
430 excess = lz->excess - ctx->rate * ms / 1000;
430 excess = lr->excess - ctx->rate * ms / 1000;
431431
432432 if (excess > 0) {
433433 return;
437437 ngx_queue_remove(q);
438438
439439 node = (ngx_rbtree_node_t *)
440 ((u_char *) lz - offsetof(ngx_rbtree_node_t, color));
440 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
441441
442442 ngx_rbtree_delete(ctx->rbtree, node);
443443
670670 static char *
671671 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
672672 {
673 ngx_http_limit_req_conf_t *lzcf = conf;
673 ngx_http_limit_req_conf_t *lrcf = conf;
674674
675675 ngx_int_t burst;
676676 ngx_str_t *value, s;
677677 ngx_uint_t i;
678678
679 if (lzcf->shm_zone) {
679 if (lrcf->shm_zone) {
680680 return "is duplicate";
681681 }
682682
691691 s.len = value[i].len - 5;
692692 s.data = value[i].data + 5;
693693
694 lzcf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
694 lrcf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
695695 &ngx_http_limit_req_module);
696 if (lzcf->shm_zone == NULL) {
696 if (lrcf->shm_zone == NULL) {
697697 return NGX_CONF_ERROR;
698698 }
699699
713713 }
714714
715715 if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
716 lzcf->nodelay = 1;
716 lrcf->nodelay = 1;
717717 continue;
718718 }
719719
722722 return NGX_CONF_ERROR;
723723 }
724724
725 if (lzcf->shm_zone == NULL) {
725 if (lrcf->shm_zone == NULL) {
726726 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
727727 "\"%V\" must have \"zone\" parameter",
728728 &cmd->name);
729729 return NGX_CONF_ERROR;
730730 }
731731
732 if (lzcf->shm_zone->data == NULL) {
732 if (lrcf->shm_zone->data == NULL) {
733733 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
734734 "unknown limit_req_zone \"%V\"",
735 &lzcf->shm_zone->name);
736 return NGX_CONF_ERROR;
737 }
738
739 lzcf->burst = burst * 1000;
735 &lrcf->shm_zone->name);
736 return NGX_CONF_ERROR;
737 }
738
739 lrcf->burst = burst * 1000;
740740
741741 return NGX_CONF_OK;
742742 }