-
Notifications
You must be signed in to change notification settings - Fork 99
/
nginx_healthcheck_for_nginx_1.19+.patch
457 lines (419 loc) · 15.5 KB
/
nginx_healthcheck_for_nginx_1.19+.patch
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
diff --git a/src/http/modules/ngx_http_upstream_hash_module.c b/src/http/modules/ngx_http_upstream_hash_module.c
index 6c247b5..1ae9cce 100644
--- a/src/http/modules/ngx_http_upstream_hash_module.c
+++ b/src/http/modules/ngx_http_upstream_hash_module.c
@@ -9,6 +9,9 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_UPSTREAM_CHECK)
+#include "ngx_http_upstream_check_module.h"
+#endif
typedef struct {
uint32_t hash;
@@ -238,6 +241,14 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
goto next;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get hash peer, check_index: %ui", peer->check_index);
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ goto next;
+ }
+#endif
+
if (peer->max_fails
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
@@ -560,6 +571,15 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get consistent_hash peer, check_index: %ui",
+ peer->check_index);
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
+
if (peer->server.len != server->len
|| ngx_strncmp(peer->server.data, server->data, server->len)
!= 0)
diff --git a/src/http/modules/ngx_http_upstream_ip_hash_module.c b/src/http/modules/ngx_http_upstream_ip_hash_module.c
index 1fa01d9..366aca9 100644
--- a/src/http/modules/ngx_http_upstream_ip_hash_module.c
+++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c
@@ -9,6 +9,9 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_UPSTREAM_CHECK)
+#include "ngx_http_upstream_check_module.h"
+#endif
typedef struct {
/* the round robin data must be first */
@@ -208,6 +211,15 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
goto next;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get ip_hash peer, check_index: %ui",
+ peer->check_index);
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ goto next;
+ }
+#endif
+
if (peer->max_fails
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
diff --git a/src/http/modules/ngx_http_upstream_least_conn_module.c b/src/http/modules/ngx_http_upstream_least_conn_module.c
index ebe0627..94f1883 100644
--- a/src/http/modules/ngx_http_upstream_least_conn_module.c
+++ b/src/http/modules/ngx_http_upstream_least_conn_module.c
@@ -9,6 +9,10 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_UPSTREAM_CHECK)
+#include "ngx_http_upstream_check_module.h"
+#endif
+
static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us);
@@ -147,6 +151,16 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get least_conn peer, check_index: %ui",
+ peer->check_index);
+
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
+
if (peer->max_fails
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
@@ -202,6 +216,16 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "get least_conn peer, check_index: %ui",
+ peer->check_index);
+
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
+
if (peer->conns * best->weight != best->conns * peer->weight) {
continue;
}
diff --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c
index f72de3e..601de8d 100644
--- a/src/http/ngx_http_upstream_round_robin.c
+++ b/src/http/ngx_http_upstream_round_robin.c
@@ -9,6 +9,10 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_UPSTREAM_CHECK)
+#include "ngx_http_upstream_check_module.h"
+#endif
+
#define ngx_http_upstream_tries(p) ((p)->tries \
+ ((p)->next ? (p)->next->tries : 0))
@@ -98,6 +102,15 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].down = server[i].down;
peer[n].server = server[i].name;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ if (!server[i].down) {
+ peer[n].check_index =
+ ngx_http_upstream_check_add_peer(cf, us, &server[i].addrs[j]);
+ } else {
+ peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ }
+#endif
+
*peerp = &peer[n];
peerp = &peer[n].next;
n++;
@@ -162,6 +175,16 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].down = server[i].down;
peer[n].server = server[i].name;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ if (!server[i].down) {
+ peer[n].check_index =
+ ngx_http_upstream_check_add_peer(cf, us, &server[i].addrs[j]);
+ }
+ else {
+ peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ }
+#endif
+
*peerp = &peer[n];
peerp = &peer[n].next;
n++;
@@ -228,6 +251,9 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
peer[i].max_conns = 0;
peer[i].max_fails = 1;
peer[i].fail_timeout = 10;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
*peerp = &peer[i];
peerp = &peer[i].next;
}
@@ -344,6 +370,9 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
peer[0].max_conns = 0;
peer[0].max_fails = 1;
peer[0].fail_timeout = 10;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ peer[0].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
peers->peer = peer;
} else {
@@ -378,6 +407,9 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
peer[i].max_conns = 0;
peer[i].max_fails = 1;
peer[i].fail_timeout = 10;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
*peerp = &peer[i];
peerp = &peer[i].next;
}
@@ -443,6 +475,12 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
goto failed;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ goto failed;
+ }
+#endif
+
rrp->current = peer;
} else {
@@ -537,6 +575,12 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
continue;
}
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ if (ngx_http_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
+
if (peer->max_fails
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
diff --git a/src/http/ngx_http_upstream_round_robin.h b/src/http/ngx_http_upstream_round_robin.h
index 45f258d..dee91d0 100644
--- a/src/http/ngx_http_upstream_round_robin.h
+++ b/src/http/ngx_http_upstream_round_robin.h
@@ -38,6 +38,10 @@ struct ngx_http_upstream_rr_peer_s {
ngx_msec_t slow_start;
ngx_msec_t start_time;
+#if (NGX_HTTP_UPSTREAM_CHECK)
+ ngx_uint_t check_index;
+#endif
+
ngx_uint_t down;
#if (NGX_HTTP_SSL || NGX_COMPAT)
diff --git a/src/stream/ngx_stream_upstream_hash_module.c b/src/stream/ngx_stream_upstream_hash_module.c
index 4fa9a2d..96724a1 100644
--- a/src/stream/ngx_stream_upstream_hash_module.c
+++ b/src/stream/ngx_stream_upstream_hash_module.c
@@ -8,6 +8,9 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
+#if (NGX_STREAM_UPSTREAM_CHECK)
+#include "ngx_stream_upstream_check_module.h"
+#endif
typedef struct {
@@ -236,6 +239,16 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
ngx_stream_upstream_rr_peer_unlock(hp->rrp.peers, peer);
goto next;
}
+
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
+ "(stream_module)get least_conn peer, check_index: %ui",
+ peer->check_index);
+
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ goto next;
+ }
+#endif
if (peer->max_fails
&& peer->fails >= peer->max_fails
@@ -550,6 +563,16 @@ ngx_stream_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
+ "(stream_module)get_chash_peer, check_index: %ui",
+ peer->check_index);
+
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
+
if (peer->max_fails
&& peer->fails >= peer->max_fails
&& now - peer->checked <= peer->fail_timeout)
diff --git a/src/stream/ngx_stream_upstream_least_conn_module.c b/src/stream/ngx_stream_upstream_least_conn_module.c
index 739b20a..b805110 100644
--- a/src/stream/ngx_stream_upstream_least_conn_module.c
+++ b/src/stream/ngx_stream_upstream_least_conn_module.c
@@ -8,6 +8,9 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
+#if (NGX_STREAM_UPSTREAM_CHECK)
+#include "ngx_stream_upstream_check_module.h"
+#endif
static ngx_int_t ngx_stream_upstream_init_least_conn_peer(
@@ -142,6 +145,15 @@ ngx_stream_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
if (peer->down) {
continue;
}
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
+ "(stream_module)get least_conn peer, check_index: %ui",
+ peer->check_index);
+
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
if (peer->max_fails
&& peer->fails >= peer->max_fails
@@ -197,7 +209,15 @@ ngx_stream_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
if (peer->down) {
continue;
}
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
+ "(stream_module)get least_conn peer, check_index: %ui",
+ peer->check_index);
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
if (peer->conns * best->weight != best->conns * peer->weight) {
continue;
}
diff --git a/src/stream/ngx_stream_upstream_round_robin.c b/src/stream/ngx_stream_upstream_round_robin.c
index 36e2ec5..f3e5ea7 100644
--- a/src/stream/ngx_stream_upstream_round_robin.c
+++ b/src/stream/ngx_stream_upstream_round_robin.c
@@ -9,6 +9,10 @@
#include <ngx_core.h>
#include <ngx_stream.h>
+#if (NGX_STREAM_UPSTREAM_CHECK)
+#include "ngx_stream_upstream_check_module.h"
+#endif
+
#define ngx_stream_upstream_tries(p) ((p)->tries \
+ ((p)->next ? (p)->next->tries : 0))
@@ -104,6 +108,16 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].down = server[i].down;
peer[n].server = server[i].name;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ if (!server[i].down) {
+ peer[n].check_index =
+ ngx_stream_upstream_check_add_peer(cf, us, &server[i].addrs[j]);
+ }
+ else {
+ peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ }
+#endif
+
*peerp = &peer[n];
peerp = &peer[n].next;
n++;
@@ -168,6 +182,16 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].down = server[i].down;
peer[n].server = server[i].name;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ if (!server[i].down) {
+ peer[n].check_index =
+ ngx_stream_upstream_check_add_peer(cf, us, &server[i].addrs[j]);
+ }
+ else {
+ peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+ }
+#endif
+
*peerp = &peer[n];
peerp = &peer[n].next;
n++;
@@ -234,6 +258,9 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[i].max_conns = 0;
peer[i].max_fails = 1;
peer[i].fail_timeout = 10;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
*peerp = &peer[i];
peerp = &peer[i].next;
}
@@ -354,6 +381,9 @@ ngx_stream_upstream_create_round_robin_peer(ngx_stream_session_t *s,
peer[0].max_conns = 0;
peer[0].max_fails = 1;
peer[0].fail_timeout = 10;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ peer[0].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
peers->peer = peer;
} else {
@@ -388,6 +418,9 @@ ngx_stream_upstream_create_round_robin_peer(ngx_stream_session_t *s,
peer[i].max_conns = 0;
peer[i].max_fails = 1;
peer[i].fail_timeout = 10;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
*peerp = &peer[i];
peerp = &peer[i].next;
}
@@ -451,7 +484,11 @@ ngx_stream_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
if (peer->max_conns && peer->conns >= peer->max_conns) {
goto failed;
}
-
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ goto failed;
+ }
+#endif
rrp->current = peer;
} else {
@@ -556,7 +593,11 @@ ngx_stream_upstream_get_peer(ngx_stream_upstream_rr_peer_data_t *rrp)
if (peer->max_conns && peer->conns >= peer->max_conns) {
continue;
}
-
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ if (ngx_stream_upstream_check_peer_down(peer->check_index)) {
+ continue;
+ }
+#endif
peer->current_weight += peer->effective_weight;
total += peer->effective_weight;
diff --git a/src/stream/ngx_stream_upstream_round_robin.h b/src/stream/ngx_stream_upstream_round_robin.h
index 35d9fce..ff6398e 100644
--- a/src/stream/ngx_stream_upstream_round_robin.h
+++ b/src/stream/ngx_stream_upstream_round_robin.h
@@ -49,6 +49,10 @@ struct ngx_stream_upstream_rr_peer_s {
ngx_stream_upstream_rr_peer_t *next;
+#if (NGX_STREAM_UPSTREAM_CHECK)
+ ngx_uint_t check_index;
+#endif
+
NGX_COMPAT_BEGIN(25)
NGX_COMPAT_END
};