aboutsummaryrefslogtreecommitdiff
blob: 50405a5b84ae9b1872a25622d34aac879a6fa136 (plain)
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
commit 21116e1c0bba730ca59327ffba9320bc63f11462
Author: Johan Bergström <bugs@bergstroem.nu>
Date:   Thu Jun 19 10:17:22 2014 +1000

    Add support for upstream_check to nginx 1.7.2

diff --git src/http/modules/ngx_http_upstream_ip_hash_module.c src/http/modules/ngx_http_upstream_ip_hash_module.c
index 148d73a..be9e03d 100644
--- src/http/modules/ngx_http_upstream_ip_hash_module.c
+++ src/http/modules/ngx_http_upstream_ip_hash_module.c
@@ -9,6 +9,9 @@
 #include <ngx_core.h>
 #include <ngx_http.h>
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+#include "ngx_http_upstream_check_handler.h"
+#endif
 
 typedef struct {
     /* the round robin data must be first */
@@ -212,6 +215,15 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
             goto next_try;
         }
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+            "get ip_hash peer, check_index: %ui",
+             peer->check_index);
+        if (ngx_http_check_peer_down(peer->check_index)) {
+            goto next_try;
+        }
+#endif
+
         if (peer->max_fails
             && peer->fails >= peer->max_fails
             && now - peer->checked <= peer->fail_timeout)
diff --git src/http/modules/ngx_http_upstream_least_conn_module.c src/http/modules/ngx_http_upstream_least_conn_module.c
index dbef95d..14e9e40 100644
--- src/http/modules/ngx_http_upstream_least_conn_module.c
+++ src/http/modules/ngx_http_upstream_least_conn_module.c
@@ -9,6 +9,9 @@
 #include <ngx_core.h>
 #include <ngx_http.h>
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+#include "ngx_http_upstream_check_handler.h"
+#endif
 
 typedef struct {
     ngx_uint_t                        *conns;
@@ -203,6 +206,16 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
             continue;
         }
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+                "get least_conn peer, check_index: %ui",
+                peer->check_index);
+
+        if (ngx_http_check_peer_down(peer->check_index)) {
+            continue;
+        }
+#endif
+
         if (peer->max_fails
             && peer->fails >= peer->max_fails
             && now - peer->checked <= peer->fail_timeout)
@@ -256,6 +269,16 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
                 continue;
             }
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+                    "get least_conn peer, check_index: %ui",
+                    peer->check_index);
+
+            if (ngx_http_check_peer_down(peer->check_index)) {
+                continue;
+            }
+#endif
+
             if (lcp->conns[i] * best->weight != lcp->conns[p] * peer->weight) {
                 continue;
             }
diff --git src/http/ngx_http_upstream_round_robin.c src/http/ngx_http_upstream_round_robin.c
index 37c835c..43ccdcf 100644
--- src/http/ngx_http_upstream_round_robin.c
+++ src/http/ngx_http_upstream_round_robin.c
@@ -9,6 +9,9 @@
 #include <ngx_core.h>
 #include <ngx_http.h>
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+#include "ngx_http_upstream_check_handler.h"
+#endif
 
 static ngx_http_upstream_rr_peer_t *ngx_http_upstream_get_peer(
     ngx_http_upstream_rr_peer_data_t *rrp);
@@ -88,6 +91,14 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
                 peer[n].fail_timeout = server[i].fail_timeout;
                 peer[n].down = server[i].down;
                 peer[n].server = server[i].name;
+#if (NGX_UPSTREAM_CHECK_MODULE)
+                if (!server[i].down) {
+                    peers->peer[n].check_index =
+                        ngx_http_check_add_peer(cf, us, &server[i].addrs[j]);
+                } else {
+                    peers->peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+                }
+#endif
                 n++;
             }
         }
@@ -144,6 +155,15 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
                 peer[n].fail_timeout = server[i].fail_timeout;
                 peer[n].down = server[i].down;
                 peer[n].server = server[i].name;
+#if (NGX_UPSTREAM_CHECK_MODULE)
+                if (!server[i].down) {
+                    backup->peer[n].check_index =
+                        ngx_http_check_add_peer(cf, us, &server[i].addrs[j]);
+                }
+                else {
+                    backup->peer[n].check_index = (ngx_uint_t) NGX_ERROR;
+                }
+#endif
                 n++;
             }
         }
@@ -203,6 +223,9 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
         peer[i].current_weight = 0;
         peer[i].max_fails = 1;
         peer[i].fail_timeout = 10;
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        peers->peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
     }
 
     us->peer.data = peers;
@@ -312,7 +335,9 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
         peer[0].current_weight = 0;
         peer[0].max_fails = 1;
         peer[0].fail_timeout = 10;
-
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        peers->peer[0].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
     } else {
 
         for (i = 0; i < ur->naddrs; i++) {
@@ -352,6 +377,9 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
             peer[i].current_weight = 0;
             peer[i].max_fails = 1;
             peer[i].fail_timeout = 10;
+#if (NGX_UPSTREAM_CHECK_MODULE)
+            peers->peer[i].check_index = (ngx_uint_t) NGX_ERROR;
+#endif
         }
     }
 
@@ -411,6 +439,12 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
             goto failed;
         }
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        if (ngx_http_check_peer_down(peer->check_index)) {
+            goto failed;
+        }
+#endif
+
     } else {
 
         /* there are several peers */
@@ -508,6 +542,12 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
             continue;
         }
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+        if (ngx_http_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 src/http/ngx_http_upstream_round_robin.h src/http/ngx_http_upstream_round_robin.h
index 9db82a6..2fedd46 100644
--- src/http/ngx_http_upstream_round_robin.h
+++ src/http/ngx_http_upstream_round_robin.h
@@ -31,6 +31,10 @@ typedef struct {
     ngx_uint_t                      max_fails;
     time_t                          fail_timeout;
 
+#if (NGX_UPSTREAM_CHECK_MODULE)
+    ngx_uint_t                      check_index;
+#endif
+
     ngx_uint_t                      down;          /* unsigned  down:1; */
 
 #if (NGX_HTTP_SSL)