nginxtra 1.0.15.0 → 1.2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. data/VERSION +1 -1
  2. data/bin/nginxtra +1 -1
  3. data/lib/nginxtra/action.rb +10 -0
  4. data/lib/nginxtra/actions/compile.rb +16 -2
  5. data/lib/nginxtra/actions/start.rb +18 -2
  6. data/lib/nginxtra/actions/status.rb +1 -10
  7. data/lib/nginxtra/actions/stop.rb +18 -0
  8. data/lib/nginxtra/cli.rb +12 -3
  9. data/lib/nginxtra/config.rb +10 -0
  10. data/src/nginx/CHANGES +311 -90
  11. data/src/nginx/CHANGES.ru +315 -88
  12. data/src/nginx/auto/lib/pcre/conf +22 -5
  13. data/src/nginx/auto/lib/pcre/make +1 -1
  14. data/src/nginx/auto/modules +14 -3
  15. data/src/nginx/auto/options +17 -3
  16. data/src/nginx/auto/os/freebsd +8 -0
  17. data/src/nginx/auto/os/linux +5 -4
  18. data/src/nginx/auto/os/solaris +2 -1
  19. data/src/nginx/auto/sources +10 -2
  20. data/src/nginx/auto/summary +2 -0
  21. data/src/nginx/auto/types/sizeof +2 -1
  22. data/src/nginx/auto/types/typedef +1 -1
  23. data/src/nginx/auto/types/uintptr_t +7 -4
  24. data/src/nginx/auto/unix +82 -21
  25. data/src/nginx/conf/fastcgi.conf +1 -0
  26. data/src/nginx/conf/fastcgi_params +1 -0
  27. data/src/nginx/conf/scgi_params +1 -0
  28. data/src/nginx/conf/uwsgi_params +1 -0
  29. data/src/nginx/man/nginx.8 +49 -49
  30. data/src/nginx/src/core/nginx.c +10 -12
  31. data/src/nginx/src/core/nginx.h +2 -2
  32. data/src/nginx/src/core/ngx_buf.c +9 -7
  33. data/src/nginx/src/core/ngx_buf.h +2 -2
  34. data/src/nginx/src/core/ngx_conf_file.c +4 -11
  35. data/src/nginx/src/core/ngx_conf_file.h +1 -1
  36. data/src/nginx/src/core/ngx_connection.c +52 -1
  37. data/src/nginx/src/core/ngx_connection.h +6 -0
  38. data/src/nginx/src/core/ngx_core.h +5 -0
  39. data/src/nginx/src/core/ngx_cycle.c +1 -1
  40. data/src/nginx/src/core/ngx_cycle.h +2 -2
  41. data/src/nginx/src/core/ngx_file.c +1 -1
  42. data/src/nginx/src/core/ngx_inet.c +11 -8
  43. data/src/nginx/src/core/ngx_murmurhash.h +1 -1
  44. data/src/nginx/src/core/ngx_open_file_cache.c +343 -38
  45. data/src/nginx/src/core/ngx_open_file_cache.h +10 -0
  46. data/src/nginx/src/core/ngx_output_chain.c +2 -1
  47. data/src/nginx/src/core/ngx_parse.h +0 -3
  48. data/src/nginx/src/core/ngx_rbtree.c +1 -2
  49. data/src/nginx/src/core/ngx_regex.c +263 -5
  50. data/src/nginx/src/core/ngx_regex.h +6 -2
  51. data/src/nginx/src/core/ngx_resolver.c +88 -21
  52. data/src/nginx/src/core/ngx_resolver.h +7 -8
  53. data/src/nginx/src/core/ngx_shmtx.c +69 -44
  54. data/src/nginx/src/core/ngx_shmtx.h +12 -1
  55. data/src/nginx/src/core/ngx_slab.c +3 -3
  56. data/src/nginx/src/core/ngx_slab.h +1 -1
  57. data/src/nginx/src/core/ngx_string.c +19 -16
  58. data/src/nginx/src/core/ngx_times.c +2 -2
  59. data/src/nginx/src/event/modules/ngx_epoll_module.c +2 -2
  60. data/src/nginx/src/event/modules/ngx_eventport_module.c +1 -1
  61. data/src/nginx/src/event/modules/ngx_kqueue_module.c +1 -1
  62. data/src/nginx/src/event/ngx_event.c +25 -17
  63. data/src/nginx/src/event/ngx_event_openssl.c +3 -1
  64. data/src/nginx/src/event/ngx_event_pipe.c +108 -85
  65. data/src/nginx/src/event/ngx_event_pipe.h +1 -2
  66. data/src/nginx/src/event/ngx_event_timer.c +2 -3
  67. data/src/nginx/src/http/modules/ngx_http_access_module.c +9 -4
  68. data/src/nginx/src/http/modules/ngx_http_browser_module.c +5 -3
  69. data/src/nginx/src/http/modules/ngx_http_chunked_filter_module.c +1 -1
  70. data/src/nginx/src/http/modules/ngx_http_degradation_module.c +1 -1
  71. data/src/nginx/src/http/modules/ngx_http_fastcgi_module.c +144 -22
  72. data/src/nginx/src/http/modules/ngx_http_flv_module.c +8 -0
  73. data/src/nginx/src/http/modules/ngx_http_geo_module.c +3 -3
  74. data/src/nginx/src/http/modules/ngx_http_gzip_filter_module.c +20 -6
  75. data/src/nginx/src/http/modules/ngx_http_gzip_static_module.c +8 -0
  76. data/src/nginx/src/http/modules/ngx_http_headers_filter_module.c +23 -27
  77. data/src/nginx/src/http/modules/ngx_http_image_filter_module.c +1 -3
  78. data/src/nginx/src/http/modules/ngx_http_index_module.c +24 -0
  79. data/src/nginx/src/http/modules/ngx_http_limit_conn_module.c +747 -0
  80. data/src/nginx/src/http/modules/ngx_http_limit_req_module.c +289 -133
  81. data/src/nginx/src/http/modules/ngx_http_log_module.c +34 -6
  82. data/src/nginx/src/http/modules/ngx_http_memcached_module.c +19 -3
  83. data/src/nginx/src/http/modules/ngx_http_mp4_module.c +8 -0
  84. data/src/nginx/src/http/modules/ngx_http_proxy_module.c +1446 -239
  85. data/src/nginx/src/http/modules/ngx_http_realip_module.c +4 -10
  86. data/src/nginx/src/http/modules/ngx_http_scgi_module.c +90 -21
  87. data/src/nginx/src/http/modules/ngx_http_split_clients_module.c +8 -11
  88. data/src/nginx/src/http/modules/ngx_http_ssi_filter_module.c +16 -6
  89. data/src/nginx/src/http/modules/ngx_http_static_module.c +8 -0
  90. data/src/nginx/src/http/modules/ngx_http_upstream_ip_hash_module.c +2 -2
  91. data/src/nginx/src/http/modules/ngx_http_upstream_keepalive_module.c +570 -0
  92. data/src/nginx/src/http/modules/ngx_http_userid_filter_module.c +1 -5
  93. data/src/nginx/src/http/modules/ngx_http_uwsgi_module.c +77 -26
  94. data/src/nginx/src/http/modules/ngx_http_xslt_filter_module.c +171 -37
  95. data/src/nginx/src/http/modules/perl/nginx.pm +2 -1
  96. data/src/nginx/src/http/modules/perl/nginx.xs +4 -0
  97. data/src/nginx/src/http/ngx_http.c +8 -1
  98. data/src/nginx/src/http/ngx_http.h +1 -0
  99. data/src/nginx/src/http/ngx_http_busy_lock.c +2 -2
  100. data/src/nginx/src/http/ngx_http_cache.h +12 -1
  101. data/src/nginx/src/http/ngx_http_copy_filter_module.c +4 -3
  102. data/src/nginx/src/http/ngx_http_core_module.c +303 -37
  103. data/src/nginx/src/http/ngx_http_core_module.h +15 -0
  104. data/src/nginx/src/http/ngx_http_file_cache.c +226 -52
  105. data/src/nginx/src/http/ngx_http_parse.c +69 -3
  106. data/src/nginx/src/http/ngx_http_postpone_filter_module.c +4 -4
  107. data/src/nginx/src/http/ngx_http_request.c +61 -27
  108. data/src/nginx/src/http/ngx_http_request.h +3 -3
  109. data/src/nginx/src/http/ngx_http_request_body.c +1 -1
  110. data/src/nginx/src/http/ngx_http_script.c +6 -0
  111. data/src/nginx/src/http/ngx_http_upstream.c +200 -47
  112. data/src/nginx/src/http/ngx_http_upstream.h +20 -1
  113. data/src/nginx/src/http/ngx_http_upstream_round_robin.c +22 -6
  114. data/src/nginx/src/http/ngx_http_upstream_round_robin.h +1 -0
  115. data/src/nginx/src/http/ngx_http_variables.c +123 -4
  116. data/src/nginx/src/mail/ngx_mail.c +13 -0
  117. data/src/nginx/src/mail/ngx_mail.h +12 -0
  118. data/src/nginx/src/mail/ngx_mail_core_module.c +100 -15
  119. data/src/nginx/src/os/unix/ngx_daemon.c +2 -1
  120. data/src/nginx/src/os/unix/ngx_darwin.h +3 -0
  121. data/src/nginx/src/os/unix/ngx_darwin_config.h +1 -0
  122. data/src/nginx/src/os/unix/ngx_darwin_init.c +30 -0
  123. data/src/nginx/src/os/unix/ngx_darwin_sendfile_chain.c +11 -5
  124. data/src/nginx/src/os/unix/ngx_errno.h +5 -0
  125. data/src/nginx/src/os/unix/ngx_files.h +50 -1
  126. data/src/nginx/src/os/unix/ngx_freebsd.h +2 -1
  127. data/src/nginx/src/os/unix/ngx_freebsd_config.h +2 -0
  128. data/src/nginx/src/os/unix/ngx_freebsd_init.c +4 -3
  129. data/src/nginx/src/os/unix/ngx_freebsd_rfork_thread.c +2 -2
  130. data/src/nginx/src/os/unix/ngx_freebsd_sendfile_chain.c +12 -6
  131. data/src/nginx/src/os/unix/ngx_gcc_atomic_sparc64.h +1 -1
  132. data/src/nginx/src/os/unix/ngx_linux_config.h +1 -0
  133. data/src/nginx/src/os/unix/ngx_linux_sendfile_chain.c +6 -4
  134. data/src/nginx/src/os/unix/ngx_os.h +0 -1
  135. data/src/nginx/src/os/unix/ngx_posix_config.h +3 -0
  136. data/src/nginx/src/os/unix/ngx_process.c +50 -11
  137. data/src/nginx/src/os/unix/ngx_process.h +1 -0
  138. data/src/nginx/src/os/unix/ngx_process_cycle.c +6 -15
  139. data/src/nginx/src/os/unix/ngx_readv_chain.c +8 -0
  140. data/src/nginx/src/os/unix/ngx_setaffinity.c +69 -0
  141. data/src/nginx/src/os/unix/ngx_setaffinity.h +23 -0
  142. data/src/nginx/src/os/unix/ngx_setproctitle.c +1 -1
  143. data/src/nginx/src/os/unix/ngx_solaris_config.h +2 -0
  144. data/src/nginx/src/os/unix/ngx_solaris_sendfilev_chain.c +11 -3
  145. data/src/nginx/src/os/unix/ngx_writev_chain.c +7 -3
  146. metadata +7 -4
  147. data/src/nginx/src/http/modules/ngx_http_limit_zone_module.c +0 -553
@@ -18,6 +18,7 @@ typedef struct {
18
18
  ngx_msec_t last;
19
19
  /* integer value, 1 corresponds to 0.001 r/s */
20
20
  ngx_uint_t excess;
21
+ ngx_uint_t count;
21
22
  u_char data[1];
22
23
  } ngx_http_limit_req_node_t;
23
24
 
@@ -36,6 +37,7 @@ typedef struct {
36
37
  ngx_uint_t rate;
37
38
  ngx_int_t index;
38
39
  ngx_str_t var;
40
+ ngx_http_limit_req_node_t *node;
39
41
  } ngx_http_limit_req_ctx_t;
40
42
 
41
43
 
@@ -43,16 +45,23 @@ typedef struct {
43
45
  ngx_shm_zone_t *shm_zone;
44
46
  /* integer value, 1 corresponds to 0.001 r/s */
45
47
  ngx_uint_t burst;
48
+ ngx_uint_t nodelay; /* unsigned nodelay:1 */
49
+ } ngx_http_limit_req_limit_t;
50
+
51
+
52
+ typedef struct {
53
+ ngx_array_t limits;
46
54
  ngx_uint_t limit_log_level;
47
55
  ngx_uint_t delay_log_level;
48
-
49
- ngx_uint_t nodelay; /* unsigned nodelay:1 */
50
56
  } ngx_http_limit_req_conf_t;
51
57
 
52
58
 
53
59
  static void ngx_http_limit_req_delay(ngx_http_request_t *r);
54
- static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf,
55
- ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep);
60
+ static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit,
61
+ ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep,
62
+ ngx_uint_t account);
63
+ static ngx_msec_t ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits,
64
+ ngx_uint_t n, ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit);
56
65
  static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
57
66
  ngx_uint_t n);
58
67
 
@@ -136,123 +145,124 @@ ngx_module_t ngx_http_limit_req_module = {
136
145
  static ngx_int_t
137
146
  ngx_http_limit_req_handler(ngx_http_request_t *r)
138
147
  {
139
- size_t len, n;
140
- uint32_t hash;
141
- ngx_int_t rc;
142
- ngx_uint_t excess;
143
- ngx_time_t *tp;
144
- ngx_rbtree_node_t *node;
145
- ngx_http_variable_value_t *vv;
146
- ngx_http_limit_req_ctx_t *ctx;
147
- ngx_http_limit_req_node_t *lr;
148
- ngx_http_limit_req_conf_t *lrcf;
148
+ size_t len;
149
+ uint32_t hash;
150
+ ngx_int_t rc;
151
+ ngx_uint_t n, excess;
152
+ ngx_msec_t delay;
153
+ ngx_http_variable_value_t *vv;
154
+ ngx_http_limit_req_ctx_t *ctx;
155
+ ngx_http_limit_req_conf_t *lrcf;
156
+ ngx_http_limit_req_limit_t *limit, *limits;
149
157
 
150
158
  if (r->main->limit_req_set) {
151
159
  return NGX_DECLINED;
152
160
  }
153
161
 
154
162
  lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
163
+ limits = lrcf->limits.elts;
155
164
 
156
- if (lrcf->shm_zone == NULL) {
157
- return NGX_DECLINED;
158
- }
165
+ excess = 0;
159
166
 
160
- ctx = lrcf->shm_zone->data;
167
+ rc = NGX_DECLINED;
161
168
 
162
- vv = ngx_http_get_indexed_variable(r, ctx->index);
169
+ #if (NGX_SUPPRESS_WARN)
170
+ limit = NULL;
171
+ #endif
163
172
 
164
- if (vv == NULL || vv->not_found) {
165
- return NGX_DECLINED;
166
- }
173
+ for (n = 0; n < lrcf->limits.nelts; n++) {
167
174
 
168
- len = vv->len;
175
+ limit = &limits[n];
169
176
 
170
- if (len == 0) {
171
- return NGX_DECLINED;
172
- }
177
+ ctx = limit->shm_zone->data;
173
178
 
174
- if (len > 65535) {
175
- ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
176
- "the value of the \"%V\" variable "
177
- "is more than 65535 bytes: \"%v\"",
178
- &ctx->var, vv);
179
- return NGX_DECLINED;
180
- }
179
+ vv = ngx_http_get_indexed_variable(r, ctx->index);
181
180
 
182
- r->main->limit_req_set = 1;
183
-
184
- hash = ngx_crc32_short(vv->data, len);
181
+ if (vv == NULL || vv->not_found) {
182
+ continue;
183
+ }
185
184
 
186
- ngx_shmtx_lock(&ctx->shpool->mutex);
185
+ len = vv->len;
187
186
 
188
- ngx_http_limit_req_expire(ctx, 1);
187
+ if (len == 0) {
188
+ continue;
189
+ }
189
190
 
190
- rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &excess);
191
+ if (len > 65535) {
192
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
193
+ "the value of the \"%V\" variable "
194
+ "is more than 65535 bytes: \"%v\"",
195
+ &ctx->var, vv);
196
+ continue;
197
+ }
191
198
 
192
- ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
193
- "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000);
199
+ hash = ngx_crc32_short(vv->data, len);
194
200
 
195
- if (rc == NGX_DECLINED) {
201
+ ngx_shmtx_lock(&ctx->shpool->mutex);
196
202
 
197
- n = offsetof(ngx_rbtree_node_t, color)
198
- + offsetof(ngx_http_limit_req_node_t, data)
199
- + len;
203
+ rc = ngx_http_limit_req_lookup(limit, hash, vv->data, len, &excess,
204
+ (n == lrcf->limits.nelts - 1));
200
205
 
201
- node = ngx_slab_alloc_locked(ctx->shpool, n);
202
- if (node == NULL) {
206
+ ngx_shmtx_unlock(&ctx->shpool->mutex);
203
207
 
204
- ngx_http_limit_req_expire(ctx, 0);
208
+ ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
209
+ "limit_req[%ui]: %i %ui.%03ui",
210
+ n, rc, excess / 1000, excess % 1000);
205
211
 
206
- node = ngx_slab_alloc_locked(ctx->shpool, n);
207
- if (node == NULL) {
208
- ngx_shmtx_unlock(&ctx->shpool->mutex);
209
- return NGX_HTTP_SERVICE_UNAVAILABLE;
210
- }
212
+ if (rc != NGX_AGAIN) {
213
+ break;
211
214
  }
215
+ }
212
216
 
213
- lr = (ngx_http_limit_req_node_t *) &node->color;
217
+ if (rc == NGX_DECLINED) {
218
+ return NGX_DECLINED;
219
+ }
214
220
 
215
- node->key = hash;
216
- lr->len = (u_char) len;
221
+ r->main->limit_req_set = 1;
217
222
 
218
- tp = ngx_timeofday();
219
- lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
223
+ if (rc == NGX_BUSY || rc == NGX_ERROR) {
220
224
 
221
- lr->excess = 0;
222
- ngx_memcpy(lr->data, vv->data, len);
225
+ if (rc == NGX_BUSY) {
226
+ ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
227
+ "limiting requests, excess: %ui.%03ui by zone \"%V\"",
228
+ excess / 1000, excess % 1000,
229
+ &limit->shm_zone->shm.name);
230
+ }
223
231
 
224
- ngx_rbtree_insert(&ctx->sh->rbtree, node);
232
+ while (n--) {
233
+ ctx = limits[n].shm_zone->data;
225
234
 
226
- ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
235
+ if (ctx->node == NULL) {
236
+ continue;
237
+ }
227
238
 
228
- ngx_shmtx_unlock(&ctx->shpool->mutex);
239
+ ngx_shmtx_lock(&ctx->shpool->mutex);
229
240
 
230
- return NGX_DECLINED;
231
- }
241
+ ctx->node->count--;
232
242
 
233
- ngx_shmtx_unlock(&ctx->shpool->mutex);
243
+ ngx_shmtx_unlock(&ctx->shpool->mutex);
234
244
 
235
- if (rc == NGX_OK) {
236
- return NGX_DECLINED;
245
+ ctx->node = NULL;
246
+ }
247
+
248
+ return NGX_HTTP_SERVICE_UNAVAILABLE;
237
249
  }
238
250
 
239
- if (rc == NGX_BUSY) {
240
- ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
241
- "limiting requests, excess: %ui.%03ui by zone \"%V\"",
242
- excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
251
+ /* rc == NGX_AGAIN || rc == NGX_OK */
243
252
 
244
- return NGX_HTTP_SERVICE_UNAVAILABLE;
253
+ if (rc == NGX_AGAIN) {
254
+ excess = 0;
245
255
  }
246
256
 
247
- /* rc == NGX_AGAIN */
257
+ delay = ngx_http_limit_req_account(limits, n, &excess, &limit);
248
258
 
249
- if (lrcf->nodelay) {
259
+ if (!delay) {
250
260
  return NGX_DECLINED;
251
261
  }
252
262
 
253
263
  ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
254
264
  "delaying request, excess: %ui.%03ui, by zone \"%V\"",
255
- excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
265
+ excess / 1000, excess % 1000, &limit->shm_zone->shm.name);
256
266
 
257
267
  if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
258
268
  return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -260,8 +270,7 @@ ngx_http_limit_req_handler(ngx_http_request_t *r)
260
270
 
261
271
  r->read_event_handler = ngx_http_test_reading;
262
272
  r->write_event_handler = ngx_http_limit_req_delay;
263
- ngx_add_timer(r->connection->write,
264
- (ngx_msec_t) excess * 1000 / ctx->rate);
273
+ ngx_add_timer(r->connection->write, delay);
265
274
 
266
275
  return NGX_AGAIN;
267
276
  }
@@ -342,9 +351,10 @@ ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
342
351
 
343
352
 
344
353
  static ngx_int_t
345
- ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
346
- u_char *data, size_t len, ngx_uint_t *ep)
354
+ ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
355
+ u_char *data, size_t len, ngx_uint_t *ep, ngx_uint_t account)
347
356
  {
357
+ size_t size;
348
358
  ngx_int_t rc, excess;
349
359
  ngx_time_t *tp;
350
360
  ngx_msec_t now;
@@ -353,7 +363,10 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
353
363
  ngx_http_limit_req_ctx_t *ctx;
354
364
  ngx_http_limit_req_node_t *lr;
355
365
 
356
- ctx = lrcf->shm_zone->data;
366
+ tp = ngx_timeofday();
367
+ now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
368
+
369
+ ctx = limit->shm_zone->data;
357
370
 
358
371
  node = ctx->sh->rbtree.root;
359
372
  sentinel = ctx->sh->rbtree.sentinel;
@@ -380,9 +393,6 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
380
393
  ngx_queue_remove(&lr->queue);
381
394
  ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
382
395
 
383
- tp = ngx_timeofday();
384
-
385
- now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
386
396
  ms = (ngx_msec_int_t) (now - lr->last);
387
397
 
388
398
  excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
@@ -393,18 +403,21 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
393
403
 
394
404
  *ep = excess;
395
405
 
396
- if ((ngx_uint_t) excess > lrcf->burst) {
406
+ if ((ngx_uint_t) excess > limit->burst) {
397
407
  return NGX_BUSY;
398
408
  }
399
409
 
400
- lr->excess = excess;
401
- lr->last = now;
402
-
403
- if (excess) {
404
- return NGX_AGAIN;
410
+ if (account) {
411
+ lr->excess = excess;
412
+ lr->last = now;
413
+ return NGX_OK;
405
414
  }
406
415
 
407
- return NGX_OK;
416
+ lr->count++;
417
+
418
+ ctx->node = lr;
419
+
420
+ return NGX_AGAIN;
408
421
  }
409
422
 
410
423
  node = (rc < 0) ? node->left : node->right;
@@ -412,7 +425,115 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
412
425
 
413
426
  *ep = 0;
414
427
 
415
- return NGX_DECLINED;
428
+ size = offsetof(ngx_rbtree_node_t, color)
429
+ + offsetof(ngx_http_limit_req_node_t, data)
430
+ + len;
431
+
432
+ ngx_http_limit_req_expire(ctx, 1);
433
+
434
+ node = ngx_slab_alloc_locked(ctx->shpool, size);
435
+
436
+ if (node == NULL) {
437
+ ngx_http_limit_req_expire(ctx, 0);
438
+
439
+ node = ngx_slab_alloc_locked(ctx->shpool, size);
440
+ if (node == NULL) {
441
+ return NGX_ERROR;
442
+ }
443
+ }
444
+
445
+ node->key = hash;
446
+
447
+ ngx_rbtree_insert(&ctx->sh->rbtree, node);
448
+
449
+ lr = (ngx_http_limit_req_node_t *) &node->color;
450
+
451
+ ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
452
+
453
+ lr->len = (u_char) len;
454
+ lr->excess = 0;
455
+
456
+ ngx_memcpy(lr->data, data, len);
457
+
458
+ if (account) {
459
+ lr->last = now;
460
+ lr->count = 0;
461
+ return NGX_OK;
462
+ }
463
+
464
+ lr->last = 0;
465
+ lr->count = 1;
466
+
467
+ ctx->node = lr;
468
+
469
+ return NGX_AGAIN;
470
+ }
471
+
472
+
473
+ static ngx_msec_t
474
+ ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n,
475
+ ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit)
476
+ {
477
+ ngx_int_t excess;
478
+ ngx_time_t *tp;
479
+ ngx_msec_t now, delay, max_delay;
480
+ ngx_msec_int_t ms;
481
+ ngx_http_limit_req_ctx_t *ctx;
482
+ ngx_http_limit_req_node_t *lr;
483
+
484
+ excess = *ep;
485
+
486
+ if (excess == 0 || (*limit)->nodelay) {
487
+ max_delay = 0;
488
+
489
+ } else {
490
+ ctx = (*limit)->shm_zone->data;
491
+ max_delay = excess * 1000 / ctx->rate;
492
+ }
493
+
494
+ while (n--) {
495
+ ctx = limits[n].shm_zone->data;
496
+ lr = ctx->node;
497
+
498
+ if (lr == NULL) {
499
+ continue;
500
+ }
501
+
502
+ ngx_shmtx_lock(&ctx->shpool->mutex);
503
+
504
+ tp = ngx_timeofday();
505
+
506
+ now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
507
+ ms = (ngx_msec_int_t) (now - lr->last);
508
+
509
+ excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
510
+
511
+ if (excess < 0) {
512
+ excess = 0;
513
+ }
514
+
515
+ lr->last = now;
516
+ lr->excess = excess;
517
+ lr->count--;
518
+
519
+ ngx_shmtx_unlock(&ctx->shpool->mutex);
520
+
521
+ ctx->node = NULL;
522
+
523
+ if (limits[n].nodelay) {
524
+ continue;
525
+ }
526
+
527
+ delay = excess * 1000 / ctx->rate;
528
+
529
+ if (delay > max_delay) {
530
+ max_delay = delay;
531
+ *ep = excess;
532
+ *limit = &limits[n];
533
+ }
534
+ }
535
+
536
+ return max_delay;
416
537
  }
417
538
 
418
539
 
@@ -447,6 +568,16 @@ ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
447
568
 
448
569
  lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
449
570
 
571
+ if (lr->count) {
572
+
573
+ /*
574
+ * There is not much sense in looking further,
575
+ * because we bump nodes on the lookup stage.
576
+ */
577
+
578
+ return;
579
+ }
580
+
450
581
  if (n++ != 0) {
451
582
 
452
583
  ms = (ngx_msec_int_t) (now - lr->last);
@@ -547,9 +678,7 @@ ngx_http_limit_req_create_conf(ngx_conf_t *cf)
547
678
  /*
548
679
  * set by ngx_pcalloc():
549
680
  *
550
- * conf->shm_zone = NULL;
551
- * conf->burst = 0;
552
- * conf->nodelay = 0;
681
+ * conf->limits.elts = NULL;
553
682
  */
554
683
 
555
684
  conf->limit_log_level = NGX_CONF_UNSET_UINT;
@@ -564,10 +693,8 @@ ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
564
693
  ngx_http_limit_req_conf_t *prev = parent;
565
694
  ngx_http_limit_req_conf_t *conf = child;
566
695
 
567
- if (conf->shm_zone == NULL) {
568
- conf->shm_zone = prev->shm_zone;
569
- conf->burst = prev->burst;
570
- conf->nodelay = prev->nodelay;
696
+ if (conf->limits.elts == NULL) {
697
+ conf->limits = prev->limits;
571
698
  }
572
699
 
573
700
  ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
@@ -584,7 +711,8 @@ static char *
584
711
  ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
585
712
  {
586
713
  u_char *p;
587
- size_t size, len;
714
+ size_t len;
715
+ ssize_t size;
588
716
  ngx_str_t *value, name, s;
589
717
  ngx_int_t rate, scale;
590
718
  ngx_uint_t i;
@@ -607,25 +735,32 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
607
735
 
608
736
  p = (u_char *) ngx_strchr(name.data, ':');
609
737
 
610
- if (p) {
611
- *p = '\0';
738
+ if (p == NULL) {
739
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
740
+ "invalid zone size \"%V\"", &value[i]);
741
+ return NGX_CONF_ERROR;
742
+ }
612
743
 
613
- name.len = p - name.data;
744
+ name.len = p - name.data;
614
745
 
615
- p++;
746
+ s.data = p + 1;
747
+ s.len = value[i].data + value[i].len - s.data;
616
748
 
617
- s.len = value[i].data + value[i].len - p;
618
- s.data = p;
749
+ size = ngx_parse_size(&s);
619
750
 
620
- size = ngx_parse_size(&s);
621
- if (size > 8191) {
622
- continue;
623
- }
751
+ if (size == NGX_ERROR) {
752
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
753
+ "invalid zone size \"%V\"", &value[i]);
754
+ return NGX_CONF_ERROR;
624
755
  }
625
756
 
626
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
627
- "invalid zone size \"%V\"", &value[i]);
628
- return NGX_CONF_ERROR;
757
+ if (size < (ssize_t) (8 * ngx_pagesize)) {
758
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
759
+ "zone \"%V\" is too small", &value[i]);
760
+ return NGX_CONF_ERROR;
761
+ }
762
+
763
+ continue;
629
764
  }
630
765
 
631
766
  if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
@@ -677,7 +812,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
677
812
  return NGX_CONF_ERROR;
678
813
  }
679
814
 
680
- if (name.len == 0 || size == 0) {
815
+ if (name.len == 0) {
681
816
  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
682
817
  "\"%V\" must have \"zone\" parameter",
683
818
  &cmd->name);
@@ -686,8 +821,8 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
686
821
 
687
822
  if (ctx == NULL) {
688
823
  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
689
- "no variable is defined for limit_req_zone \"%V\"",
690
- &cmd->name);
824
+ "no variable is defined for %V \"%V\"",
825
+ &cmd->name, &name);
691
826
  return NGX_CONF_ERROR;
692
827
  }
693
828
 
@@ -703,8 +838,8 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
703
838
  ctx = shm_zone->data;
704
839
 
705
840
  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
706
- "limit_req_zone \"%V\" is already bound to variable \"%V\"",
707
- &value[1], &ctx->var);
841
+ "%V \"%V\" is already bound to variable \"%V\"",
842
+ &cmd->name, &name, &ctx->var);
708
843
  return NGX_CONF_ERROR;
709
844
  }
710
845
 
@@ -720,17 +855,17 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
720
855
  {
721
856
  ngx_http_limit_req_conf_t *lrcf = conf;
722
857
 
723
- ngx_int_t burst;
724
- ngx_str_t *value, s;
725
- ngx_uint_t i;
726
-
727
- if (lrcf->shm_zone) {
728
- return "is duplicate";
729
- }
858
+ ngx_int_t burst;
859
+ ngx_str_t *value, s;
860
+ ngx_uint_t i, nodelay;
861
+ ngx_shm_zone_t *shm_zone;
862
+ ngx_http_limit_req_limit_t *limit, *limits;
730
863
 
731
864
  value = cf->args->elts;
732
865
 
866
+ shm_zone = NULL;
733
867
  burst = 0;
868
+ nodelay = 0;
734
869
 
735
870
  for (i = 1; i < cf->args->nelts; i++) {
736
871
 
@@ -739,9 +874,9 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
739
874
  s.len = value[i].len - 5;
740
875
  s.data = value[i].data + 5;
741
876
 
742
- lrcf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
743
- &ngx_http_limit_req_module);
744
- if (lrcf->shm_zone == NULL) {
877
+ shm_zone = ngx_shared_memory_add(cf, &s, 0,
878
+ &ngx_http_limit_req_module);
879
+ if (shm_zone == NULL) {
745
880
  return NGX_CONF_ERROR;
746
881
  }
747
882
 
@@ -761,7 +896,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
761
896
  }
762
897
 
763
898
  if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
764
- lrcf->nodelay = 1;
899
+ nodelay = 1;
765
900
  continue;
766
901
  }
767
902
 
@@ -770,21 +905,42 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
770
905
  return NGX_CONF_ERROR;
771
906
  }
772
907
 
773
- if (lrcf->shm_zone == NULL) {
908
+ if (shm_zone == NULL) {
774
909
  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
775
910
  "\"%V\" must have \"zone\" parameter",
776
911
  &cmd->name);
777
912
  return NGX_CONF_ERROR;
778
913
  }
779
914
 
780
- if (lrcf->shm_zone->data == NULL) {
915
+ if (shm_zone->data == NULL) {
781
916
  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
782
917
  "unknown limit_req_zone \"%V\"",
783
- &lrcf->shm_zone->shm.name);
918
+ &shm_zone->shm.name);
784
919
  return NGX_CONF_ERROR;
785
920
  }
786
921
 
787
- lrcf->burst = burst * 1000;
922
+ limits = lrcf->limits.elts;
923
+
924
+ if (limits == NULL) {
925
+ if (ngx_array_init(&lrcf->limits, cf->pool, 1,
926
+ sizeof(ngx_http_limit_req_limit_t))
927
+ != NGX_OK)
928
+ {
929
+ return NGX_CONF_ERROR;
930
+ }
931
+ }
932
+
933
+ for (i = 0; i < lrcf->limits.nelts; i++) {
934
+ if (shm_zone == limits[i].shm_zone) {
935
+ return "is duplicate";
936
+ }
937
+ }
938
+
939
+ limit = ngx_array_push(&lrcf->limits);
940
+
941
+ limit->shm_zone = shm_zone;
942
+ limit->burst = burst * 1000;
943
+ limit->nodelay = nodelay;
788
944
 
789
945
  return NGX_CONF_OK;
790
946
  }