-
Notifications
You must be signed in to change notification settings - Fork 49
/
ngx_stream_limit_upstream_module.c
1212 lines (904 loc) · 33.1 KB
/
ngx_stream_limit_upstream_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Author: cfsego
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
static ngx_rbtree_t ngx_stream_limit_ups_loc_rbtree;
static ngx_rbtree_node_t ngx_stream_limit_ups_sentinel;
typedef struct {
ngx_uint_t limit_conn;
ngx_uint_t backlog;
ngx_msec_t timeout;
ngx_shm_zone_t *shm_zone;
ngx_stream_upstream_init_peer_pt init;
ngx_uint_t log_level;
unsigned hooked:1;
} ngx_stream_limit_ups_conf_t;
typedef struct {
ngx_rbtree_t *rbtree;
ngx_queue_t *queue;
} ngx_stream_limit_ups_zone_t;
typedef struct {
u_char color;
unsigned short port;
ngx_uint_t counter;
} ngx_stream_limit_ups_node_t;
typedef struct {
volatile ngx_uint_t counter;
ngx_msec_t last;
ngx_queue_t queue;
} ngx_stream_limit_ups_shm_t;
typedef struct {
ngx_uint_t counter;
ngx_uint_t qlen;
ngx_uint_t work;
ngx_queue_t wait;
} ngx_stream_limit_ups_loc_t;
typedef struct {
ngx_stream_session_t *s;
ngx_stream_limit_ups_conf_t *lucf;
ngx_stream_limit_ups_loc_t *lnode;
void *data;
void *wait;
unsigned in_proc:1;
unsigned cln:1;
ngx_event_get_peer_pt get;
ngx_event_free_peer_pt free;
ngx_event_notify_peer_pt notify;
#if (NGX_HTTP_SSL)
ngx_event_set_peer_session_pt set_session;
ngx_event_save_peer_session_pt save_session;
#endif
struct sockaddr *sockaddr;
} ngx_stream_limit_ups_ctx_t;
typedef struct {
ngx_queue_t queue;
ngx_stream_limit_ups_ctx_t *ctx;
ngx_event_handler_pt read_event_handler;
ngx_event_handler_pt write_event_handler;
unsigned r_timer_set:1;
unsigned w_timer_set:1;
} ngx_stream_limit_ups_wait_t;
typedef struct {
ngx_msec_t connect_timeout;
ngx_msec_t timeout;
} ngx_stream_proxy_srv_conf_t;
static ngx_conf_enum_t ngx_stream_limit_ups_log_levels[] = {
{ ngx_string("info"), NGX_LOG_INFO },
{ ngx_string("notice"), NGX_LOG_NOTICE },
{ ngx_string("warn"), NGX_LOG_WARN },
{ ngx_string("error"), NGX_LOG_ERR },
{ ngx_null_string, 0 }
};
static void ngx_stream_limit_ups_block_reading(ngx_event_t *ev);
static void ngx_stream_limit_ups_timeout(ngx_event_t *ev);
static void ngx_stream_limit_ups_cleanup(void *data);
static void ngx_stream_limit_ups_notify_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
static void ngx_stream_limit_ups_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
static ngx_int_t ngx_stream_limit_ups_get_peer(ngx_peer_connection_t *pc,
void *data);
#if (NGX_HTTP_SSL)
static ngx_int_t ngx_stream_limit_ups_set_peer_session(
ngx_peer_connection_t *pc, void *data);
static void ngx_stream_limit_ups_save_peer_session(ngx_peer_connection_t *pc,
void *data);
#endif
static ngx_int_t ngx_stream_limit_ups_init_peer(ngx_stream_session_t *s,
ngx_stream_upstream_srv_conf_t *us);
static void *ngx_stream_limit_ups_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_limit_ups_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_stream_limit_ups_zone(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_stream_limit_ups_conn(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_stream_limit_ups_init(ngx_conf_t *cf);
static ngx_int_t ngx_stream_limit_ups_init_zone(ngx_shm_zone_t *shm_zone,
void *data);
static void ngx_stream_limit_ups_zone_expire(ngx_shm_zone_t *shm_zone);
static void
ngx_stream_limit_ups_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
static ngx_rbtree_node_t *
ngx_stream_limit_ups_rbtree_lookup(ngx_rbtree_t *rbtree,
unsigned long addr, unsigned short port);
static ngx_command_t ngx_stream_limit_ups_commands[] = {
{ ngx_string("limit_upstream_zone"),
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE2,
ngx_stream_limit_ups_zone,
0,
0,
NULL },
{ ngx_string("limit_upstream_conn"),
NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1234|NGX_CONF_TAKE5,
ngx_stream_limit_ups_conn,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("limit_upstream_log_level"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1,
ngx_conf_set_enum_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_limit_ups_conf_t, log_level),
&ngx_stream_limit_ups_log_levels },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_limit_ups_module_ctx = {
NULL,
ngx_stream_limit_ups_init,
NULL,
NULL,
ngx_stream_limit_ups_create_srv_conf,
ngx_stream_limit_ups_merge_srv_conf,
NULL,
NULL
};
ngx_module_t ngx_stream_limit_upstream_module = {
NGX_MODULE_V1,
&ngx_stream_limit_ups_module_ctx,
ngx_stream_limit_ups_commands,
NGX_STREAM_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
extern void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_uint_t rc);
extern void ngx_stream_proxy_connect(ngx_stream_session_t *s);
extern ngx_module_t ngx_stream_proxy_module;
static void
ngx_stream_limit_ups_block_reading(ngx_event_t *ev)
{
ngx_connection_t *c;
ngx_stream_session_t *s;
c = ev->data;
s = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
"limit upstream: stream reading blocked");
/* aio does not call this handler */
if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && c->read->active)
{
if (ngx_del_event(c->read, NGX_READ_EVENT, 0) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
}
}
}
static void
ngx_stream_limit_ups_timeout(ngx_event_t *ev)
{
ngx_connection_t *c;
ngx_stream_session_t *s;
ngx_stream_upstream_t *u;
ngx_stream_limit_ups_wait_t *w;
ngx_stream_limit_ups_loc_t *l;
ngx_stream_limit_ups_ctx_t *ctx;
c = ev->data;
s = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
"limit upstream: into timeout");
if (!ev->timedout) {
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
"limit upstream: wev ignored");
return;
}
u = s->upstream;
ctx = (ngx_stream_limit_ups_ctx_t *) u->peer.data;
ngx_log_error(ctx->lucf->log_level, c->log, 0,
"limit upstream: session[%p] is timeout", s);
ev->timedout = 0;
w = ctx->wait;
ngx_queue_remove(&w->queue);
l = ctx->lnode;
l->qlen--;
ngx_stream_proxy_finalize(s, NGX_STREAM_SERVICE_UNAVAILABLE);
}
static void
ngx_stream_limit_ups_cleanup(void *data)
{
ngx_queue_t *q;
ngx_rbtree_node_t *node_s, *node_l;
struct sockaddr_in *sin;
#if NGX_DEBUG
ngx_peer_connection_t *pc;
#endif
ngx_stream_limit_ups_ctx_t *ctx;
ngx_stream_limit_ups_loc_t *lnode;
ngx_stream_limit_ups_shm_t *snode;
ngx_stream_limit_ups_node_t *cnode;
ngx_stream_limit_ups_wait_t *wnode;
ngx_stream_limit_ups_zone_t *shmctx;
ctx = (ngx_stream_limit_ups_ctx_t *) data;
shmctx = ctx->lucf->shm_zone->data;
if (!ctx->in_proc) {
return;
}
#if NGX_DEBUG
pc = &ctx->s->upstream->peer;
#endif
sin = (struct sockaddr_in *) ctx->sockaddr;
node_s = ngx_stream_limit_ups_rbtree_lookup(shmctx->rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
node_l = ngx_stream_limit_ups_rbtree_lookup(
&ngx_stream_limit_ups_loc_rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
if (node_l == NULL && node_s == NULL) {
return;
}
#if 1
if ((node_l == NULL && node_s) || (node_s == NULL && node_l)) {
ngx_log_error(NGX_LOG_EMERG, ctx->s->connection->log, 0,
"limit upstream: only local or shm node exists");
return;
}
#endif
cnode = (ngx_stream_limit_ups_node_t *) &node_s->color;
snode = (ngx_stream_limit_ups_shm_t *) &cnode->counter;
cnode = (ngx_stream_limit_ups_node_t *) &node_l->color;
lnode = (ngx_stream_limit_ups_loc_t *) &cnode->counter;
lnode->work--;
if ((snode->counter > ctx->lucf->limit_conn && lnode->work)
|| lnode->qlen == 0)
{
ngx_log_debug4(NGX_LOG_DEBUG_STREAM, ctx->s->connection->log, 0,
"limit upstream: will not resume session for %V "
"(counter: %d, active: %d, wait queue: %d)",
pc->name, snode->counter, lnode->work, lnode->qlen);
snode->counter--;
lnode->counter--;
return;
}
do {
q = ngx_queue_last(&lnode->wait);
wnode = ngx_queue_data(q, ngx_stream_limit_ups_wait_t, queue);
ngx_log_debug5(NGX_LOG_DEBUG_STREAM, ctx->s->connection->log, 0,
"limit upstream: remove queue node: %p for %V "
"(counter: %d, active: %d, wait queue: %d)",
q, pc->name, snode->counter,
lnode->work, lnode->qlen);
ngx_queue_remove(q);
lnode->qlen--;
/* resume a request in wait queue */
ctx = wnode->ctx;
ngx_log_error(ctx->lucf->log_level, ctx->s->connection->log, 0,
"limit upstream: session[%p] is resumed", ctx->s);
ctx->s->connection->read->handler = wnode->read_event_handler;
ctx->s->connection->write->handler = wnode->write_event_handler;
if (wnode->r_timer_set) {
if (ctx->s->connection->read->timedout) {
ctx->s->connection->read->handler(ctx->s->connection->read);
return;
}
if (ngx_handle_read_event(ctx->s->connection->read, 0)
!= NGX_OK)
{
ngx_stream_proxy_finalize(ctx->s,
NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
ngx_add_timer(ctx->s->connection->read,
ctx->s->connection->read->timer.key);
}
ngx_del_timer(ctx->s->connection->write);
if (wnode->w_timer_set) {
if (ctx->s->connection->write->timedout) {
ctx->s->connection->write->handler(ctx->s->connection->write);
return;
}
if (ngx_handle_write_event(ctx->s->connection->write, 0)
!= NGX_OK)
{
ngx_stream_proxy_finalize(ctx->s,
NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
ngx_add_timer(ctx->s->connection->write,
ctx->s->connection->write->timer.key);
}
lnode->work++;
ngx_stream_proxy_connect(ctx->s);
} while ((ngx_uint_t) ngx_atomic_fetch_add(&snode->counter, 1)
< ctx->lucf->limit_conn && lnode->qlen);
snode->counter--;
}
static void
ngx_stream_limit_ups_notify_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state)
{
ngx_stream_limit_ups_ctx_t *ctx;
ctx = (ngx_stream_limit_ups_ctx_t *) data;
if (ctx->notify) {
ctx->notify(pc, ctx->data, state);
}
}
static void
ngx_stream_limit_ups_free_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state)
{
ngx_stream_limit_ups_ctx_t *ctx;
ctx = (ngx_stream_limit_ups_ctx_t *) data;
ctx->free(pc, ctx->data, state);
}
static ngx_int_t
ngx_stream_limit_ups_get_peer(ngx_peer_connection_t *pc, void *data)
{
size_t n;
ngx_int_t rc;
#if (NGX_DEBUG)
ngx_uint_t active;
#endif
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node_s, *node_l;
struct sockaddr_in *sin;
ngx_pool_cleanup_t *cln;
ngx_rbtree_key_int_t t;
ngx_stream_limit_ups_ctx_t *ctx;
ngx_stream_limit_ups_loc_t *lnode;
ngx_stream_limit_ups_shm_t *snode;
ngx_stream_limit_ups_node_t *cnode;
ngx_stream_limit_ups_wait_t *wnode;
ngx_stream_limit_ups_zone_t *shmctx;
ngx_stream_proxy_srv_conf_t *pscf;
ctx = (ngx_stream_limit_ups_ctx_t *) data;
shpool = (ngx_slab_pool_t *) ctx->lucf->shm_zone->shm.addr;
shmctx = ctx->lucf->shm_zone->data;
if (ctx->s->upstream->blocked) {
rc = NGX_OK;
ctx->s->upstream->blocked = 0;
goto set_and_ret;
} else {
if (ctx->in_proc) {
sin = (struct sockaddr_in *) ctx->sockaddr;
node_s = ngx_stream_limit_ups_rbtree_lookup(shmctx->rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
node_l = ngx_stream_limit_ups_rbtree_lookup(
&ngx_stream_limit_ups_loc_rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
if (node_l && node_s) {
cnode = (ngx_stream_limit_ups_node_t *) &node_s->color;
snode = (ngx_stream_limit_ups_shm_t *) &cnode->counter;
cnode = (ngx_stream_limit_ups_node_t *) &node_l->color;
lnode = (ngx_stream_limit_ups_loc_t *) &cnode->counter;
lnode->work--;
snode->counter--;
lnode->counter--;
}
ctx->in_proc = 0;
}
rc = ctx->get(pc, ctx->data);
if (rc != NGX_OK && rc != NGX_DONE) {
return rc;
}
}
ctx->sockaddr = ngx_palloc(ctx->s->connection->pool, pc->socklen);
if (ctx->sockaddr == NULL) {
return NGX_ERROR;
}
ngx_memcpy(ctx->sockaddr, pc->sockaddr, pc->socklen);
sin = (struct sockaddr_in *) ctx->sockaddr;
node_s = ngx_stream_limit_ups_rbtree_lookup(shmctx->rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
node_l = ngx_stream_limit_ups_rbtree_lookup(
&ngx_stream_limit_ups_loc_rbtree,
sin->sin_addr.s_addr,
sin->sin_port);
if (node_l == NULL) {
n = offsetof(ngx_rbtree_node_t, color)
+ offsetof(ngx_stream_limit_ups_node_t, counter)
+ sizeof(ngx_stream_limit_ups_loc_t);
node_l = ngx_pcalloc(ngx_cycle->pool, n);
if (node_l == NULL) {
return NGX_ERROR;
}
cnode = (ngx_stream_limit_ups_node_t *) &node_l->color;
lnode = (ngx_stream_limit_ups_loc_t *) &cnode->counter;
node_l->key = sin->sin_addr.s_addr;
cnode->port = sin->sin_port;
ngx_queue_init(&lnode->wait);
ngx_rbtree_insert(&ngx_stream_limit_ups_loc_rbtree, node_l);
} else {
cnode = (ngx_stream_limit_ups_node_t *) &node_l->color;
lnode = (ngx_stream_limit_ups_loc_t *) &cnode->counter;
}
if (node_s == NULL) {
ngx_shmtx_lock(&shpool->mutex);
n = offsetof(ngx_rbtree_node_t, color)
+ offsetof(ngx_stream_limit_ups_node_t, counter)
+ sizeof(ngx_stream_limit_ups_shm_t);
node_s = ngx_slab_alloc_locked(shpool, n);
if (node_s == NULL) {
return NGX_ERROR;
}
cnode = (ngx_stream_limit_ups_node_t *) &node_s->color;
snode = (ngx_stream_limit_ups_shm_t *) &cnode->counter;
node_s->key = sin->sin_addr.s_addr;
cnode->port = sin->sin_port;
ngx_queue_insert_head(shmctx->queue, &snode->queue);
ngx_rbtree_insert(shmctx->rbtree, node_s);
ngx_shmtx_unlock(&shpool->mutex);
} else {
cnode = (ngx_stream_limit_ups_node_t *) &node_s->color;
snode = (ngx_stream_limit_ups_shm_t *) &cnode->counter;
}
snode->last = ngx_current_msec;
if (rc == NGX_DONE) {
snode->counter++;
lnode->counter++;
lnode->work++;
goto set_and_ret;
}
ngx_log_debug4(NGX_LOG_DEBUG_STREAM, ctx->s->connection->log, 0,
"limit upstream: status for %V "
"(counter: %d, active: %d, wait queue: %d)",
pc->name, snode->counter, lnode->work, lnode->qlen);
#if (NGX_DEBUG)
active = ngx_atomic_fetch_add(&snode->counter, 1);
if (active >= ctx->lucf->limit_conn && lnode->work) {
#else
if ((ngx_uint_t) ngx_atomic_fetch_add(&snode->counter, 1)
>= ctx->lucf->limit_conn && lnode->work)
{
#endif
snode->counter--;
if (lnode->qlen >= ctx->lucf->backlog) {
ngx_log_error(ctx->lucf->log_level, ctx->s->connection->log, 0,
"limit upstream: session[%p] is dropped", ctx->s);
return NGX_DECLINED;
}
wnode = ngx_pcalloc(ctx->s->connection->pool,
sizeof(ngx_stream_limit_ups_wait_t));
if (wnode == NULL) {
return NGX_ERROR;
}
ngx_queue_insert_head(&lnode->wait, &wnode->queue);
lnode->qlen++;
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, ctx->s->connection->log, 0,
"limit upstream: add queue node: %p", &wnode->queue);
ngx_log_error(ctx->lucf->log_level, ctx->s->connection->log, 0,
"limit upstream: session[%p] is blocked", ctx->s);
wnode->ctx = ctx;
ctx->wait = wnode;
ctx->lnode = lnode;
wnode->read_event_handler = ctx->s->connection->read->handler;
wnode->r_timer_set = ctx->s->connection->read->timer_set;
ctx->s->connection->read->handler = ngx_stream_limit_ups_block_reading;
if (wnode->r_timer_set) {
ngx_del_timer(ctx->s->connection->read);
t = ctx->s->connection->read->timer.key;
t -= (ngx_rbtree_key_int_t) ngx_current_msec;
if (t > 0) {
ctx->s->connection->read->timer.key = t;
} else {
ctx->s->connection->read->timedout = 1;
}
}
wnode->write_event_handler = ctx->s->connection->write->handler;
wnode->w_timer_set = ctx->s->connection->write->timer_set;
ctx->s->connection->write->handler = ngx_stream_limit_ups_timeout;
if (wnode->w_timer_set) {
ngx_del_timer(ctx->s->connection->write);
t = ctx->s->connection->write->timer.key;
t -= (ngx_rbtree_key_int_t) ngx_current_msec;
if (t > 0) {
ctx->s->connection->write->timer.key = t;
} else {
ctx->s->connection->write->timedout = 1;
}
}
if (ctx->lucf->timeout == 0) {
pscf = ngx_stream_get_module_srv_conf(ctx->s,
ngx_stream_proxy_module);
ngx_add_timer(ctx->s->connection->write, pscf->timeout);
} else {
ngx_add_timer(ctx->s->connection->write, ctx->lucf->timeout);
}
ctx->s->upstream->blocked = 1;
return NGX_BLOCK;
}
#if (NGX_DEBUG)
if (active >= ctx->lucf->limit_conn) {
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, ctx->s->connection->log, 0,
"limit upstream: force continue request");
}
#endif
lnode->counter++;
lnode->work++;
set_and_ret:
if (!ctx->cln) {
cln = ngx_pool_cleanup_add(ctx->s->connection->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_stream_limit_ups_cleanup;
cln->data = ctx;
ctx->cln = 1;
}
ctx->in_proc = 1;
return rc;
}
static ngx_int_t
ngx_stream_limit_ups_init_peer(ngx_stream_session_t *s,
ngx_stream_upstream_srv_conf_t *us)
{
ngx_int_t rc;
ngx_stream_limit_ups_ctx_t *ctx;
ngx_stream_limit_ups_conf_t *lucf;
ctx = ngx_pcalloc(s->connection->pool,
sizeof(ngx_stream_limit_ups_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
}
lucf = us->srv_conf[ngx_stream_limit_upstream_module.ctx_index];
rc = lucf->init(s, us);
if (rc != NGX_OK) {
return rc;
}
ctx->data = s->upstream->peer.data;
s->upstream->peer.data = ctx;
ctx->get = s->upstream->peer.get;
s->upstream->peer.get = ngx_stream_limit_ups_get_peer;
ctx->free = s->upstream->peer.free;
s->upstream->peer.free = ngx_stream_limit_ups_free_peer;
ctx->notify = s->upstream->peer.notify;
s->upstream->peer.notify = ngx_stream_limit_ups_notify_peer;
#if (NGX_HTTP_SSL)
ctx->set_session = s->upstream->peer.set_session;
s->upstream->peer.set_session = ngx_stream_limit_ups_set_peer_session;
ctx->save_session = s->upstream->peer.save_session;
s->upstream->peer.save_session = ngx_stream_limit_ups_save_peer_session;
#endif
ctx->s = s;
ctx->lucf = lucf;
s->upstream->blocked = 0;
return NGX_OK;
}
static ngx_int_t
ngx_stream_limit_ups_init(ngx_conf_t *cf)
{
ngx_uint_t i;
ngx_stream_limit_ups_conf_t *lucf, *mlucf;
ngx_stream_upstream_srv_conf_t **uscfp;
ngx_stream_upstream_main_conf_t *umcf;
umcf = ngx_stream_conf_get_module_main_conf(cf,
ngx_stream_upstream_module);
mlucf = ngx_stream_conf_get_module_srv_conf(cf,
ngx_stream_limit_upstream_module);
uscfp = umcf->upstreams.elts;
ngx_rbtree_init(&ngx_stream_limit_ups_loc_rbtree,
&ngx_stream_limit_ups_sentinel,
ngx_stream_limit_ups_rbtree_insert_value);
for (i = 0; i < umcf->upstreams.nelts; i++) {
if (uscfp[i]->srv_conf) {
lucf = uscfp[i]->
srv_conf[ngx_stream_limit_upstream_module.ctx_index];
if (lucf->limit_conn != NGX_CONF_UNSET_UINT && !lucf->hooked) {
lucf->init = uscfp[i]->peer.init;
uscfp[i]->peer.init = ngx_stream_limit_ups_init_peer;
if (lucf->backlog == NGX_CONF_UNSET_UINT) {
lucf->backlog = 1000;
}
if (lucf->timeout == NGX_CONF_UNSET_MSEC) {
lucf->timeout = 0;
}
if (ngx_stream_limit_ups_merge_srv_conf(cf, mlucf, lucf)
!= NGX_CONF_OK)
{
return NGX_ERROR;
}
}
}
}
return NGX_OK;
}
static void *
ngx_stream_limit_ups_create_srv_conf(ngx_conf_t *cf)
{
ngx_stream_limit_ups_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_limit_ups_conf_t));
if (conf == NULL) {
return NULL;
}
/*
* set by ngx_pcalloc();
*
* conf->init = NULL;
* conf->shm_zone = NULL;
* conf->hooked = 0;
*/
conf->limit_conn = NGX_CONF_UNSET_UINT;
conf->backlog = NGX_CONF_UNSET_UINT;
conf->timeout = NGX_CONF_UNSET_MSEC;
conf->log_level = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_stream_limit_ups_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child)
{
ngx_stream_limit_ups_conf_t *conf = child;
ngx_stream_limit_ups_conf_t *prev = parent;
ngx_conf_merge_uint_value(conf->log_level,
prev->log_level, NGX_LOG_NOTICE);
return NGX_CONF_OK;
}
static char *
ngx_stream_limit_ups_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ssize_t n;
ngx_str_t *value;
ngx_shm_zone_t *shm_zone;
ngx_stream_limit_ups_zone_t *ctx;
value = cf->args->elts;
n = ngx_parse_size(&value[2]);
if (n == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid size of limit_upstream_zone \"%V\"",
&value[2]);
return NGX_CONF_ERROR;
}
if (n < (ngx_int_t) (8 * ngx_pagesize)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"limit_upstream_zone \"%V\" is too small",
&value[1]);
return NGX_CONF_ERROR;
}
shm_zone = ngx_shared_memory_add(cf, &value[1], n,
&ngx_stream_limit_upstream_module);
if (shm_zone == NULL) {
return NGX_CONF_ERROR;
}
if (shm_zone->data) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"limit_upstream_zone \"%V\" is duplicate",
&value[1]);
return NGX_CONF_ERROR;
}
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_limit_ups_zone_t));
if (ctx == NULL) {
return NGX_CONF_ERROR;
}
shm_zone->data = ctx;
shm_zone->init = ngx_stream_limit_ups_init_zone;
return NGX_CONF_OK;
}
static char *
ngx_stream_limit_ups_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value, s;
ngx_uint_t i;
ngx_stream_upstream_srv_conf_t *uscf;
ngx_stream_limit_ups_conf_t *lucf = conf;
if (lucf->shm_zone) {
return "is duplicate";
}
uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module);
value = cf->args->elts;
for (i = 1; i < cf->args->nelts; i++) {
if (ngx_strncmp("zone=", value[i].data, 5) == 0) {
s.len = value[i].len - 5;
s.data = value[i].data + 5;
lucf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
&ngx_stream_limit_upstream_module);
if (lucf->shm_zone == NULL) {
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp("limit=", value[i].data, 6) == 0) {
lucf->limit_conn = ngx_atoi(value[i].data + 6, value[i].len - 6);
if (lucf->limit_conn <= 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid limit \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp("backlog=", value[i].data, 8) == 0) {
lucf->backlog = ngx_atoi(value[i].data + 8, value[i].len - 8);
if (lucf->backlog <= 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid backlog \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp("nodelay", value[i].data, 7) == 0) {
lucf->backlog = 0;
continue;
}
if (ngx_strncmp("timeout=", value[i].data, 8) == 0) {
s.len = value[i].len - 8;
s.data = value[i].data + 8;
lucf->timeout = ngx_parse_time(&s, 0);
if (lucf->timeout == (ngx_msec_t) NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid timeout \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp("instant_hook", value[i].data, 12) == 0) {
lucf->init = uscf->peer.init;
uscf->peer.init = ngx_stream_limit_ups_init_peer;
lucf->hooked = 1;
continue;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[i]);
return NGX_CONF_ERROR;
}
if (lucf->limit_conn == NGX_CONF_UNSET_UINT) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"%V\" must have \"limit\" parameter",
&cmd->name);
return NGX_CONF_ERROR;
}
if (lucf->shm_zone == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"%V\" must have \"zone\" parameter",
&cmd->name);
return NGX_CONF_ERROR;
}