-
Notifications
You must be signed in to change notification settings - Fork 99
/
ngx_stream_upstream_check_module.c
2237 lines (1702 loc) · 64.8 KB
/
ngx_stream_upstream_check_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
/*
* Copyright (C) 2017- Changxun Zhou([email protected])
* desc: stream upstream server health check.
*/
#include "common.h.in"
#include "ngx_stream_upstream_check_module.h"
#define LOG_LEVEL NGX_LOG_DEBUG_STREAM
#define MODULE_NAME "[ngx_healthcheck:stream]"
ngx_int_t ngx_upstream_check_http_body_regex(
ngx_conf_t *cf, ngx_upstream_check_srv_conf_t *ucscf,
ngx_str_t *regex, ngx_uint_t caseless);
ngx_int_t ngx_upstream_check_http_parse_status_line(
ngx_buf_t *b, ngx_uint_t *pstate, ngx_http_status_t *status);
typedef struct {
ngx_buf_t send;
ngx_buf_t recv;
ngx_uint_t state;
ngx_http_status_t status;
size_t padding;
size_t length;
} ngx_stream_upstream_check_ctx_t;
#define NGX_HTTP_CHECK_CONNECT_DONE 0x0001
#define NGX_HTTP_CHECK_SEND_DONE 0x0002
#define NGX_HTTP_CHECK_RECV_DONE 0x0004
#define NGX_HTTP_CHECK_ALL_DONE 0x0008
#define NGX_CHECK_TYPE_TCP 0x0001
#define NGX_CHECK_TYPE_HTTP 0x0002
#define NGX_CHECK_TYPE_UDP 0x0004
#define NGX_CHECK_HTTP_2XX 0x0002
#define NGX_CHECK_HTTP_3XX 0x0004
#define NGX_CHECK_HTTP_4XX 0x0008
#define NGX_CHECK_HTTP_5XX 0x0010
#define NGX_CHECK_HTTP_ERR 0x8000
typedef void (*ngx_stream_upstream_check_status_format_pt) (ngx_buf_t *b,
ngx_upstream_check_peers_t *peers, ngx_uint_t flag);
typedef struct {
ngx_str_t format;
ngx_str_t content_type;
ngx_stream_upstream_check_status_format_pt output;
} ngx_check_status_conf_t;
#define NGX_CHECK_STATUS_DOWN 0x0001
#define NGX_CHECK_STATUS_UP 0x0002
typedef struct {
ngx_check_status_conf_t *format;
ngx_flag_t flag;
} ngx_stream_upstream_check_status_ctx_t;
typedef ngx_int_t (*ngx_stream_upstream_check_status_command_pt)
(ngx_stream_upstream_check_status_ctx_t *ctx, ngx_str_t *value);
typedef struct {
ngx_str_t name;
ngx_stream_upstream_check_status_command_pt handler;
} ngx_check_status_command_t;
//zhocux stream check module main config data.
typedef struct {
ngx_uint_t check_shm_size;
ngx_upstream_check_peers_t *peers;
} ngx_stream_upstream_check_main_conf_t;
typedef struct {
ngx_check_status_conf_t *format;
} ngx_stream_upstream_check_loc_conf_t;
static ngx_int_t ngx_stream_upstream_check_add_timers(ngx_cycle_t *cycle);
static ngx_int_t ngx_stream_upstream_check_peek_one_byte(
ngx_connection_t *c, ngx_upstream_check_peer_t *peer);
static void ngx_stream_upstream_check_begin_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_connect_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_peek_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_send_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_recv_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_discard_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_dummy_handler(ngx_event_t *event);
static ngx_int_t ngx_stream_upstream_check_http_init(
ngx_upstream_check_peer_t *peer);
static ngx_int_t ngx_stream_upstream_check_http_parse(
ngx_upstream_check_peer_t *peer);
static void ngx_stream_upstream_check_http_reinit(
ngx_upstream_check_peer_t *peer);
static void ngx_stream_upstream_check_status_update(
ngx_upstream_check_peer_t *peer,
ngx_int_t result);
static void ngx_stream_upstream_check_clean_event(
ngx_upstream_check_peer_t *peer);
static void ngx_stream_upstream_check_timeout_handler(ngx_event_t *event);
static void ngx_stream_upstream_check_finish_handler(ngx_event_t *event);
static ngx_int_t ngx_stream_upstream_check_need_exit();
static void ngx_stream_upstream_check_clear_all_events();
static ngx_int_t ngx_stream_upstream_check_addr_change_port(ngx_pool_t *pool,
ngx_addr_t *dst, ngx_addr_t *src, ngx_uint_t port);
static ngx_check_conf_t *ngx_http_get_check_type_conf(ngx_str_t *str);
static char *ngx_stream_upstream_check(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_stream_upstream_check_keepalive_requests(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_stream_upstream_check_http_send(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_stream_upstream_check_http_expect_alive(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_upstream_check_http_body(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_stream_upstream_check_shm_size(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static void *ngx_stream_upstream_check_create_main_conf(ngx_conf_t *cf);
static char *ngx_stream_upstream_check_init_main_conf(ngx_conf_t *cf,
void *conf);
static void *ngx_stream_upstream_check_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_upstream_check_init_srv_conf(ngx_conf_t *cf, void *conf);
#define SHM_NAME_LEN 256
static char *ngx_stream_upstream_check_init_shm(ngx_conf_t *cf, void *conf);
static ngx_int_t ngx_stream_upstream_check_get_shm_name(ngx_str_t *shm_name,
ngx_pool_t *pool, ngx_uint_t generation);
static ngx_shm_zone_t *ngx_shared_memory_find(ngx_cycle_t *cycle,
ngx_str_t *name, void *tag);
static ngx_upstream_check_peer_shm_t *
ngx_stream_upstream_check_find_shm_peer(ngx_upstream_check_peers_shm_t *peers_shm,
ngx_addr_t *addr, ngx_str_t *upstream_name);
static ngx_int_t ngx_stream_upstream_check_init_shm_peer(
ngx_upstream_check_peer_shm_t *peer_shm,
ngx_upstream_check_peer_shm_t *opeer_shm,
ngx_uint_t init_down, ngx_pool_t *pool, ngx_str_t *peer_name, ngx_str_t *upstream_name);
static ngx_int_t ngx_stream_upstream_check_init_shm_zone(
ngx_shm_zone_t *shm_zone, void *data);
static ngx_int_t ngx_stream_upstream_check_init_process(ngx_cycle_t *cycle);
static ngx_conf_bitmask_t ngx_check_http_expect_alive_masks[] = {
{ ngx_string("http_2xx"), NGX_CHECK_HTTP_2XX },
{ ngx_string("http_3xx"), NGX_CHECK_HTTP_3XX },
{ ngx_string("http_4xx"), NGX_CHECK_HTTP_4XX },
{ ngx_string("http_5xx"), NGX_CHECK_HTTP_5XX },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_stream_upstream_check_commands[] = {
{ ngx_string("check"),
NGX_STREAM_UPS_CONF|NGX_CONF_1MORE,
ngx_stream_upstream_check,
0,
0,
NULL },
{ ngx_string("check_keepalive_requests"),
NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1,
ngx_stream_upstream_check_keepalive_requests,
0,
0,
NULL },
{ ngx_string("check_http_send"),
NGX_STREAM_UPS_CONF|NGX_CONF_TAKE1,
ngx_stream_upstream_check_http_send,
0,
0,
NULL },
{ ngx_string("check_http_expect_alive"),
NGX_STREAM_UPS_CONF|NGX_CONF_1MORE,
ngx_stream_upstream_check_http_expect_alive,
0,
0,
NULL },
{ ngx_string("check_http_expect_body"),
NGX_STREAM_UPS_CONF|NGX_CONF_TAKE2,
ngx_upstream_check_http_body,
0,
0,
NULL },
{ ngx_string("check_shm_size"),
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
ngx_stream_upstream_check_shm_size,
0,
0,
NULL },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_upstream_check_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
ngx_stream_upstream_check_create_main_conf,/* create main configuration */
ngx_stream_upstream_check_init_main_conf, /* init main configuration */
ngx_stream_upstream_check_create_srv_conf, /* create server configuration */
NULL /* merge server configuration */
};
ngx_module_t ngx_stream_upstream_check_module = {
NGX_MODULE_V1,
&ngx_stream_upstream_check_module_ctx, /* module context */
ngx_stream_upstream_check_commands, /* module directives */
NGX_STREAM_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_stream_upstream_check_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_check_conf_t ngx_check_types[] = {
{ NGX_CHECK_TYPE_TCP,
ngx_string("tcp"),
ngx_null_string,
0,
ngx_stream_upstream_check_peek_handler,
ngx_stream_upstream_check_peek_handler,
NULL,
NULL,
NULL,
0, //zhoucx: need_pool ? no, we just connect peer.
0 }, //zhoucx: need_keepalive ? i change it to no
{ NGX_CHECK_TYPE_UDP,
ngx_string("udp"),
ngx_string("NGX_UDP_CHECKER"),/* zhoucx: default send data.
(we must send some data and then call recv to trigger icmp error response).*/
0,
ngx_stream_upstream_check_send_handler,
ngx_stream_upstream_check_peek_handler,
ngx_stream_upstream_check_http_init,
NULL,
ngx_stream_upstream_check_http_reinit,
1, // (changxun): when send data, we need pool
0 },
{ NGX_CHECK_TYPE_HTTP,
ngx_string("http"),
ngx_string("GET / HTTP/1.0\r\n\r\n"),
NGX_CONF_BITMASK_SET | NGX_CHECK_HTTP_2XX | NGX_CHECK_HTTP_3XX,
ngx_stream_upstream_check_send_handler,
ngx_stream_upstream_check_recv_handler,
ngx_stream_upstream_check_http_init,
ngx_stream_upstream_check_http_parse,
ngx_stream_upstream_check_http_reinit,
1,
1 },
{ 0,
ngx_null_string,
ngx_null_string,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
0,
0 }
};
/* external glabal var*/
ngx_uint_t ngx_stream_upstream_check_shm_generation = 0; //reload counter
ngx_upstream_check_peers_t *stream_peers_ctx = NULL; //need by check status module
ngx_uint_t
ngx_stream_upstream_check_add_peer(ngx_conf_t *cf,
ngx_stream_upstream_srv_conf_t *us, ngx_addr_t *peer_addr)
{
ngx_upstream_check_peer_t *peer;
ngx_upstream_check_peers_t *peers;
ngx_upstream_check_srv_conf_t *ucscf;
ngx_stream_upstream_check_main_conf_t *ucmcf;
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0,
"[api] add a peer:( %V ) to health check list.",
&peer_addr->name);
if (us->srv_conf == NULL) {
return NGX_ERROR;
}
ucscf = ngx_stream_conf_upstream_srv_conf(us, ngx_stream_upstream_check_module);
if(ucscf->check_interval == 0) {
return NGX_ERROR;
}
ucmcf = ngx_stream_conf_get_module_main_conf(cf,
ngx_stream_upstream_check_module);
peers = ucmcf->peers;
peer = ngx_array_push(&peers->peers);
if (peer == NULL) {
return NGX_ERROR;
}
ngx_memzero(peer, sizeof(ngx_upstream_check_peer_t));
peer->index = peers->peers.nelts - 1;
peer->conf = ucscf;
peer->upstream_name = &us->host;
peer->peer_addr = peer_addr;
if (ucscf->port) {
peer->check_peer_addr = ngx_pcalloc(cf->pool, sizeof(ngx_addr_t));
if (peer->check_peer_addr == NULL) {
return NGX_ERROR;
}
if (ngx_stream_upstream_check_addr_change_port(cf->pool,
peer->check_peer_addr, peer_addr, ucscf->port)
!= NGX_OK) {
return NGX_ERROR;
}
} else {
peer->check_peer_addr = peer->peer_addr;
}
peers->checksum +=
ngx_murmur_hash2(peer_addr->name.data, peer_addr->name.len);
return peer->index;
}
static ngx_int_t
ngx_stream_upstream_check_addr_change_port(ngx_pool_t *pool, ngx_addr_t *dst,
ngx_addr_t *src, ngx_uint_t port)
{
size_t len;
u_char *p;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
dst->socklen = src->socklen;
dst->sockaddr = ngx_palloc(pool, dst->socklen);
if (dst->sockaddr == NULL) {
return NGX_ERROR;
}
ngx_memcpy(dst->sockaddr, src->sockaddr, dst->socklen);
switch (dst->sockaddr->sa_family) {
case AF_INET:
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
sin = (struct sockaddr_in *) dst->sockaddr;
sin->sin_port = htons(port);
break;
#if (NGX_HAVE_INET6)
case AF_INET6:
len = NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1;
sin6 = (struct sockaddr_in6 *) dst->sockaddr;
sin6->sin6_port = htons(port);
break;
#endif
default:
return NGX_ERROR;
}
p = ngx_pnalloc(pool, len);
if (p == NULL) {
return NGX_ERROR;
}
#if (nginx_version >= 1005012)
len = ngx_sock_ntop(dst->sockaddr, dst->socklen, p, len, 1);
#else
len = ngx_sock_ntop(dst->sockaddr, p, len, 1);
#endif
dst->name.len = len;
dst->name.data = p;
return NGX_OK;
}
ngx_uint_t
ngx_stream_upstream_check_peer_down(ngx_uint_t index)
{
ngx_upstream_check_peer_t *peer;
if (stream_peers_ctx == NULL || index >= stream_peers_ctx->peers.nelts) {
return 0;
}
peer = stream_peers_ctx->peers.elts;
return (peer[index].shm->down);
}
/* TODO: this interface can count each peer's busyness */
void
ngx_stream_upstream_check_get_peer(ngx_uint_t index)
{
ngx_upstream_check_peer_t *peer;
if (stream_peers_ctx == NULL || index >= stream_peers_ctx->peers.nelts) {
return;
}
peer = stream_peers_ctx->peers.elts;
ngx_shmtx_lock(&peer[index].shm->mutex);
peer[index].shm->busyness++;
peer[index].shm->access_count++;
ngx_shmtx_unlock(&peer[index].shm->mutex);
}
void
ngx_stream_upstream_check_free_peer(ngx_uint_t index)
{
ngx_upstream_check_peer_t *peer;
if (stream_peers_ctx == NULL || index >= stream_peers_ctx->peers.nelts) {
return;
}
peer = stream_peers_ctx->peers.elts;
ngx_shmtx_lock(&peer[index].shm->mutex);
if (peer[index].shm->busyness > 0) {
peer[index].shm->busyness--;
}
ngx_shmtx_unlock(&peer[index].shm->mutex);
}
static ngx_int_t
ngx_stream_upstream_check_add_timers(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_msec_t t, delay;
ngx_check_conf_t *cf;
ngx_upstream_check_peer_t *peer;
ngx_upstream_check_peers_t *peers;
ngx_upstream_check_srv_conf_t *ucscf;
ngx_upstream_check_peer_shm_t *peer_shm;
ngx_upstream_check_peers_shm_t *peers_shm;
peers = stream_peers_ctx;
if (peers == NULL) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"[timers] no stream section, skip init");
return NGX_OK;
}
peers_shm = peers->peers_shm;
if (peers_shm == NULL) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"[timers][when init process]"
"no peers, so skip add timers");
return NGX_OK;
}
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"[timers][when init process]"
"start check-timer for %ud peers",
peers->peers.nelts);
srandom(ngx_pid);
peer = peers->peers.elts;
peer_shm = peers_shm->peers;
for (i = 0; i < peers->peers.nelts; i++) {
peer[i].shm = &peer_shm[i];
peer[i].check_ev.handler =
ngx_stream_upstream_check_begin_handler;
peer[i].check_ev.log = cycle->log;
peer[i].check_ev.data = &peer[i];
peer[i].check_ev.timer_set = 0;
peer[i].check_timeout_ev.handler =
ngx_stream_upstream_check_timeout_handler;
peer[i].check_timeout_ev.log = cycle->log;
peer[i].check_timeout_ev.data = &peer[i];
peer[i].check_timeout_ev.timer_set = 0;
ucscf = peer[i].conf;
cf = ucscf->check_type_conf;
if (cf->need_pool) {
peer[i].pool = ngx_create_pool(ngx_pagesize, cycle->log);
if (peer[i].pool == NULL) {
return NGX_ERROR;
}
}
peer[i].send_handler = cf->send_handler;
peer[i].recv_handler = cf->recv_handler;
peer[i].init = cf->init;
peer[i].parse = cf->parse;
peer[i].reinit = cf->reinit;
/*
* We add a random start time here, since we don't want to trigger
* the check events too close to each other at the beginning.
*/
// ensure interval >= 1000ms.
delay = ucscf->check_interval > 1000 ? ucscf->check_interval : 1000;
t = ngx_random() % delay; // t in range(0~999 ms)
ngx_add_timer(&peer[i].check_ev, t);
}
return NGX_OK;
}
static void
ngx_stream_upstream_check_begin_handler(ngx_event_t *event)
{
ngx_msec_t interval;
ngx_upstream_check_peer_t *peer;
ngx_upstream_check_peers_t *peers;
ngx_upstream_check_srv_conf_t *ucscf;
ngx_upstream_check_peers_shm_t *peers_shm;
peers = stream_peers_ctx;
if (peers == NULL) {
return;
}
peers_shm = peers->peers_shm;
if (peers_shm == NULL) {
return;
}
peer = event->data;
ucscf = peer->conf;
if (ngx_stream_upstream_check_need_exit()) {
ngx_log_error(NGX_LOG_NOTICE, event->log, 0, MODULE_NAME
"[check-handler][when begin check]"
"recv exit signal, skip current check for peer:(%V)",
&peer->check_peer_addr->name);
return;
}
ngx_add_timer(event, ucscf->check_interval / 2);//zhoucx: what's mean?
/* This process is processing this peer now. */
if ((peer->shm->owner == ngx_pid ||
peer->check_timeout_ev.timer_set)) {
return;
}
interval = ngx_current_msec - peer->shm->access_time;
ngx_log_debug6(LOG_LEVEL, event->log, 0, MODULE_NAME
"check begin handler for (%V): index: %ui, owner: %P, "
"ngx_pid: %P, current interval: %M, check_interval: %M",
&peer->check_peer_addr->name, peer->index, peer->shm->owner,
ngx_pid, interval,
ucscf->check_interval);
ngx_shmtx_lock(&peer->shm->mutex);
if (peers_shm->generation != ngx_stream_upstream_check_shm_generation) {
ngx_shmtx_unlock(&peer->shm->mutex);
return;
}
if ((interval >= ucscf->check_interval)
&& (peer->shm->owner == NGX_INVALID_PID))
{
peer->shm->owner = ngx_pid;
} else if (interval >= (ucscf->check_interval << 4)) {
/*
* If the check peer has been untouched for 2^4 times of
* the check interval, activate the current timer.
* Sometimes, the checking process may disappear
* in some circumstances, and the clean event will never
* be triggered.
*/
peer->shm->owner = ngx_pid;
peer->shm->access_time = ngx_current_msec;
}
ngx_shmtx_unlock(&peer->shm->mutex);
if (peer->shm->owner == ngx_pid) {
ngx_log_debug(LOG_LEVEL, event->log, 0, MODULE_NAME
"[check-event][when begin check]"
"restart a check for peer:(%V)",
&peer->check_peer_addr->name);
ngx_stream_upstream_check_connect_handler(event);
}
}
static void
ngx_stream_upstream_check_connect_handler(ngx_event_t *event)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_upstream_check_peer_t *peer;
ngx_upstream_check_srv_conf_t *ucscf;
peer = event->data;
ucscf = peer->conf;
if (ngx_stream_upstream_check_need_exit()) {
ngx_log_error(NGX_LOG_NOTICE, event->log, 0, MODULE_NAME
"[check-handler][when connect peer]"
"recv exit signal, skip current check for peer:(%V)",
&peer->check_peer_addr->name);
return;
}
if (peer->pc.connection != NULL) {
c = peer->pc.connection;
if ((rc = ngx_stream_upstream_check_peek_one_byte(c, peer)) == NGX_OK) {
goto upstream_check_connect_done;
} else {
ngx_close_connection(c);
peer->pc.connection = NULL;
}
}
ngx_memzero(&peer->pc, sizeof(ngx_peer_connection_t));
peer->pc.sockaddr = peer->check_peer_addr->sockaddr;
peer->pc.socklen = peer->check_peer_addr->socklen;
peer->pc.name = &peer->check_peer_addr->name;
peer->pc.type = (ucscf->check_type_conf->type==NGX_CHECK_TYPE_UDP)?SOCK_DGRAM:SOCK_STREAM;
peer->pc.get = ngx_event_get_peer;
peer->pc.log = event->log;
peer->pc.log_error = NGX_ERROR_ERR;
peer->pc.cached = 0;
peer->pc.connection = NULL;
rc = ngx_event_connect_peer(&peer->pc); // (changxun): noblocking .
if (rc == NGX_ERROR || rc == NGX_DECLINED) {
ngx_stream_upstream_check_status_update(peer, 0); //set down
return;
}
/* NGX_OK or NGX_AGAIN */
c = peer->pc.connection;
c->data = peer;
c->log = peer->pc.log;
c->sendfile = 0;
c->read->log = c->log;
c->write->log = c->log;
c->pool = peer->pool;
/* (changxun): set sock opt "IP_RECVERR" in order to recv icmp error like host/port unreachable. */
/* note: we have invoke 'ngx_event_connect_peer() above. so the code we comment is not required.
int val = 1;
if( setsockopt( c->fd, SOL_IP, IP_RECVERR, &val, sizeof(val) ) == -1 ){
ngx_log_error(NGX_LOG_ERR, event->log, 0,
"setsockopt(IP_RECVERR) failed with peer: %V ",
&peer->check_peer_addr->name);
}
*/
upstream_check_connect_done:
peer->state = NGX_HTTP_CHECK_CONNECT_DONE;
c->write->handler = peer->send_handler;
c->read->handler = peer->recv_handler;
ngx_add_timer(&peer->check_timeout_ev, ucscf->check_timeout);//zhoucx: start connect timeout timer.
/* The kqueue's loop interface needs it. */
if (rc == NGX_OK) {
c->write->handler(c->write); //zhoucx: it is check_type->send_handler.
}
}
static ngx_int_t
ngx_stream_upstream_check_peek_one_byte(ngx_connection_t *c,
ngx_upstream_check_peer_t *peer)
{
char buf[1];
ngx_int_t n;
ngx_err_t err;
n = recv(c->fd, buf, 1, MSG_PEEK);
err = ngx_socket_errno;
if (n == 1 || (n == -1 && err == NGX_EAGAIN)) {
return NGX_OK;
} else {
ngx_log_error(NGX_LOG_WARN, c->log, err, MODULE_NAME
"when peek one byte, recv(): %i, fd: %d. peer: %V",
n, c->fd, &peer->check_peer_addr->name);
return NGX_ERROR;
}
}
static void
ngx_stream_upstream_check_peek_handler(ngx_event_t *event)
{
ngx_connection_t *c;
ngx_upstream_check_peer_t *peer;
if (ngx_stream_upstream_check_need_exit()) {
return;
}
c = event->data;
peer = c->data;
if (ngx_stream_upstream_check_peek_one_byte(c, peer) == NGX_OK) {
ngx_stream_upstream_check_status_update(peer, 1); //up
} else {
c->error = 1;
ngx_stream_upstream_check_status_update(peer, 0); //down
}
ngx_stream_upstream_check_clean_event(peer);
ngx_stream_upstream_check_finish_handler(event);
}
static void
ngx_stream_upstream_check_discard_handler(ngx_event_t *event)
{
u_char buf[4096];
ssize_t size;
ngx_connection_t *c;
ngx_upstream_check_peer_t *peer;
c = event->data;
ngx_log_debug0(LOG_LEVEL, c->log, 0, MODULE_NAME
"upstream check discard handler");
if (ngx_stream_upstream_check_need_exit()) {
return;
}
peer = c->data;
while (1) {
size = c->recv(c, buf, 4096);
if (size > 0) {
continue;
} else if (size == NGX_AGAIN) {
break;
} else {
if (size == 0) {
ngx_log_debug0(LOG_LEVEL, c->log, 0, MODULE_NAME
"peer closed its half side of the connection");
}
goto check_discard_fail;
}
}
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
goto check_discard_fail;
}
return;
check_discard_fail:
c->error = 1;
ngx_stream_upstream_check_clean_event(peer);
}
static void
ngx_stream_upstream_check_dummy_handler(ngx_event_t *event)
{
return;
}
static void
ngx_stream_upstream_check_send_handler(ngx_event_t *event)
{
ssize_t size;
ngx_connection_t *c;
ngx_stream_upstream_check_ctx_t *ctx;
ngx_upstream_check_peer_t *peer;
if (ngx_stream_upstream_check_need_exit()) {
return;
}
c = event->data;
peer = c->data;
ngx_log_debug(LOG_LEVEL, c->log, 0, MODULE_NAME
"[send-handler] for peer:%V",
&peer->check_peer_addr->name);
if (c->pool == NULL) {
ngx_log_error(NGX_LOG_ERR, event->log, 0, MODULE_NAME
"check pool NULL with peer: %V ",
&peer->check_peer_addr->name);
goto check_send_fail;
}
if (peer->state != NGX_HTTP_CHECK_CONNECT_DONE) {
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, event->log, 0, MODULE_NAME
"check handle write event error with peer: %V ",
&peer->check_peer_addr->name);
goto check_send_fail;
}
return; // (changxun): wait future connect event.
}
if (peer->check_data == NULL) {
peer->check_data = ngx_pcalloc(peer->pool,
sizeof(ngx_stream_upstream_check_ctx_t));
if (peer->check_data == NULL) {
goto check_send_fail;
}
if (peer->init == NULL || peer->init(peer) != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, event->log, 0, MODULE_NAME
"check init error with peer: %V ",
&peer->check_peer_addr->name);
goto check_send_fail;
}
}
ctx = peer->check_data;
while (ctx->send.pos < ctx->send.last) {
/* send check data */
size = c->send(c, ctx->send.pos, ctx->send.last - ctx->send.pos);
{
ngx_err_t err;
err = (size >=0) ? 0 : ngx_socket_errno;
ngx_log_debug(LOG_LEVEL, ngx_cycle->log, err, MODULE_NAME
"[send-handler]"
"send check data size: %z, total: %z for peer:%V",
size, ctx->send.last - ctx->send.pos, &peer->check_peer_addr->name);
}
if (size > 0) {
ctx->send.pos += size;
} else if (size == 0 || size == NGX_AGAIN) {
return;
} else {
c->error = 1;
goto check_send_fail;
}
}
if (ctx->send.pos == ctx->send.last) {
ngx_log_debug(LOG_LEVEL, c->log, 0, MODULE_NAME
"[send-handler] send finish for peer:%V",
&peer->check_peer_addr->name);
peer->state = NGX_HTTP_CHECK_SEND_DONE;
c->requests++;
}
return; // normal return;
check_send_fail:
ngx_stream_upstream_check_status_update(peer, 0);
ngx_stream_upstream_check_clean_event(peer);
}
static void
ngx_stream_upstream_check_recv_handler(ngx_event_t *event)
{
u_char *new_buf;
ssize_t size, n;
ngx_int_t rc;
ngx_connection_t *c;
ngx_stream_upstream_check_ctx_t *ctx;
ngx_upstream_check_peer_t *peer;
if (ngx_stream_upstream_check_need_exit()) {
return;
}
c = event->data;
peer = c->data;
if (peer->state != NGX_HTTP_CHECK_SEND_DONE) {
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
goto check_recv_fail;
}
return;
}
ctx = peer->check_data;
if (ctx->recv.start == NULL) {
/* 1/2 of the page_size, is it enough? */
ctx->recv.start = ngx_palloc(c->pool, ngx_pagesize / 2);
if (ctx->recv.start == NULL) {
goto check_recv_fail;
}
ctx->recv.last = ctx->recv.pos = ctx->recv.start;
ctx->recv.end = ctx->recv.start + ngx_pagesize / 2;
}
while (1) {
n = ctx->recv.end - ctx->recv.last;
/* buffer not big enough? enlarge it by twice */
if (n == 0) {
size = ctx->recv.end - ctx->recv.start;