Mercurial > hg > nginx-quic
comparison src/http/modules/ngx_http_upstream_ip_hash_module.c @ 4724:0141b4aec0e4 stable-1.2
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
*) Upstream: least_conn balancer module.
*) Upstream: weights and IPv6 support in ip_hash balancer.
*) Upstream keepalive: "single" parameter deprecated.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 02 Jul 2012 16:41:13 +0000 |
parents | d620f497c50f |
children |
comparison
equal
deleted
inserted
replaced
4723:68ac485abbba | 4724:0141b4aec0e4 |
---|---|
14 /* the round robin data must be first */ | 14 /* the round robin data must be first */ |
15 ngx_http_upstream_rr_peer_data_t rrp; | 15 ngx_http_upstream_rr_peer_data_t rrp; |
16 | 16 |
17 ngx_uint_t hash; | 17 ngx_uint_t hash; |
18 | 18 |
19 u_char addr[3]; | 19 u_char addrlen; |
20 u_char *addr; | |
20 | 21 |
21 u_char tries; | 22 u_char tries; |
22 | 23 |
23 ngx_event_get_peer_pt get_rr_peer; | 24 ngx_event_get_peer_pt get_rr_peer; |
24 } ngx_http_upstream_ip_hash_peer_data_t; | 25 } ngx_http_upstream_ip_hash_peer_data_t; |
74 NULL, /* exit master */ | 75 NULL, /* exit master */ |
75 NGX_MODULE_V1_PADDING | 76 NGX_MODULE_V1_PADDING |
76 }; | 77 }; |
77 | 78 |
78 | 79 |
79 ngx_int_t | 80 static u_char ngx_http_upstream_ip_hash_pseudo_addr[3]; |
81 | |
82 | |
83 static ngx_int_t | |
80 ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) | 84 ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) |
81 { | 85 { |
82 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) { | 86 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) { |
83 return NGX_ERROR; | 87 return NGX_ERROR; |
84 } | 88 } |
91 | 95 |
92 static ngx_int_t | 96 static ngx_int_t |
93 ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, | 97 ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, |
94 ngx_http_upstream_srv_conf_t *us) | 98 ngx_http_upstream_srv_conf_t *us) |
95 { | 99 { |
96 u_char *p; | |
97 struct sockaddr_in *sin; | 100 struct sockaddr_in *sin; |
101 #if (NGX_HAVE_INET6) | |
102 struct sockaddr_in6 *sin6; | |
103 #endif | |
98 ngx_http_upstream_ip_hash_peer_data_t *iphp; | 104 ngx_http_upstream_ip_hash_peer_data_t *iphp; |
99 | 105 |
100 iphp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t)); | 106 iphp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t)); |
101 if (iphp == NULL) { | 107 if (iphp == NULL) { |
102 return NGX_ERROR; | 108 return NGX_ERROR; |
108 return NGX_ERROR; | 114 return NGX_ERROR; |
109 } | 115 } |
110 | 116 |
111 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer; | 117 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer; |
112 | 118 |
113 /* AF_INET only */ | 119 switch (r->connection->sockaddr->sa_family) { |
114 | 120 |
115 if (r->connection->sockaddr->sa_family == AF_INET) { | 121 case AF_INET: |
116 | |
117 sin = (struct sockaddr_in *) r->connection->sockaddr; | 122 sin = (struct sockaddr_in *) r->connection->sockaddr; |
118 p = (u_char *) &sin->sin_addr.s_addr; | 123 iphp->addr = (u_char *) &sin->sin_addr.s_addr; |
119 iphp->addr[0] = p[0]; | 124 iphp->addrlen = 3; |
120 iphp->addr[1] = p[1]; | 125 break; |
121 iphp->addr[2] = p[2]; | 126 |
122 | 127 #if (NGX_HAVE_INET6) |
123 } else { | 128 case AF_INET6: |
124 iphp->addr[0] = 0; | 129 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; |
125 iphp->addr[1] = 0; | 130 iphp->addr = (u_char *) &sin6->sin6_addr.s6_addr; |
126 iphp->addr[2] = 0; | 131 iphp->addrlen = 16; |
132 break; | |
133 #endif | |
134 | |
135 default: | |
136 iphp->addr = ngx_http_upstream_ip_hash_pseudo_addr; | |
137 iphp->addrlen = 3; | |
127 } | 138 } |
128 | 139 |
129 iphp->hash = 89; | 140 iphp->hash = 89; |
130 iphp->tries = 0; | 141 iphp->tries = 0; |
131 iphp->get_rr_peer = ngx_http_upstream_get_round_robin_peer; | 142 iphp->get_rr_peer = ngx_http_upstream_get_round_robin_peer; |
138 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data) | 149 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data) |
139 { | 150 { |
140 ngx_http_upstream_ip_hash_peer_data_t *iphp = data; | 151 ngx_http_upstream_ip_hash_peer_data_t *iphp = data; |
141 | 152 |
142 time_t now; | 153 time_t now; |
154 ngx_int_t w; | |
143 uintptr_t m; | 155 uintptr_t m; |
144 ngx_uint_t i, n, p, hash; | 156 ngx_uint_t i, n, p, hash; |
145 ngx_http_upstream_rr_peer_t *peer; | 157 ngx_http_upstream_rr_peer_t *peer; |
146 | 158 |
147 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | 159 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
160 | 172 |
161 hash = iphp->hash; | 173 hash = iphp->hash; |
162 | 174 |
163 for ( ;; ) { | 175 for ( ;; ) { |
164 | 176 |
165 for (i = 0; i < 3; i++) { | 177 for (i = 0; i < iphp->addrlen; i++) { |
166 hash = (hash * 113 + iphp->addr[i]) % 6271; | 178 hash = (hash * 113 + iphp->addr[i]) % 6271; |
167 } | 179 } |
168 | 180 |
169 p = hash % iphp->rrp.peers->number; | 181 if (!iphp->rrp.peers->weighted) { |
182 p = hash % iphp->rrp.peers->number; | |
183 | |
184 } else { | |
185 w = hash % iphp->rrp.peers->total_weight; | |
186 | |
187 for (i = 0; i < iphp->rrp.peers->number; i++) { | |
188 w -= iphp->rrp.peers->peer[i].weight; | |
189 if (w < 0) { | |
190 break; | |
191 } | |
192 } | |
193 | |
194 p = i; | |
195 } | |
170 | 196 |
171 n = p / (8 * sizeof(uintptr_t)); | 197 n = p / (8 * sizeof(uintptr_t)); |
172 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); | 198 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); |
173 | 199 |
174 if (!(iphp->rrp.tried[n] & m)) { | 200 if (!(iphp->rrp.tried[n] & m)) { |
227 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); | 253 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); |
228 | 254 |
229 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash; | 255 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash; |
230 | 256 |
231 uscf->flags = NGX_HTTP_UPSTREAM_CREATE | 257 uscf->flags = NGX_HTTP_UPSTREAM_CREATE |
258 |NGX_HTTP_UPSTREAM_WEIGHT | |
232 |NGX_HTTP_UPSTREAM_MAX_FAILS | 259 |NGX_HTTP_UPSTREAM_MAX_FAILS |
233 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT | 260 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT |
234 |NGX_HTTP_UPSTREAM_DOWN; | 261 |NGX_HTTP_UPSTREAM_DOWN; |
235 | 262 |
236 return NGX_CONF_OK; | 263 return NGX_CONF_OK; |