comparison src/http/modules/ngx_http_upstream_ip_hash_module.c @ 4655:382c523d253a

Upstream: weights support in ip_hash balancer.
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 03 Jun 2012 23:22:41 +0000
parents d620f497c50f
children 441b2941a506
comparison
equal deleted inserted replaced
4654:a2ca3a6ee680 4655:382c523d253a
138 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data) 138 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
139 { 139 {
140 ngx_http_upstream_ip_hash_peer_data_t *iphp = data; 140 ngx_http_upstream_ip_hash_peer_data_t *iphp = data;
141 141
142 time_t now; 142 time_t now;
143 ngx_int_t w;
143 uintptr_t m; 144 uintptr_t m;
144 ngx_uint_t i, n, p, hash; 145 ngx_uint_t i, n, p, hash;
145 ngx_http_upstream_rr_peer_t *peer; 146 ngx_http_upstream_rr_peer_t *peer;
146 147
147 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, 148 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
164 165
165 for (i = 0; i < 3; i++) { 166 for (i = 0; i < 3; i++) {
166 hash = (hash * 113 + iphp->addr[i]) % 6271; 167 hash = (hash * 113 + iphp->addr[i]) % 6271;
167 } 168 }
168 169
169 p = hash % iphp->rrp.peers->number; 170 if (!iphp->rrp.peers->weighted) {
171 p = hash % iphp->rrp.peers->number;
172
173 } else {
174 w = hash % iphp->rrp.peers->total_weight;
175
176 for (i = 0; i < iphp->rrp.peers->number; i++) {
177 w -= iphp->rrp.peers->peer[i].weight;
178 if (w < 0) {
179 break;
180 }
181 }
182
183 p = i;
184 }
170 185
171 n = p / (8 * sizeof(uintptr_t)); 186 n = p / (8 * sizeof(uintptr_t));
172 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); 187 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
173 188
174 if (!(iphp->rrp.tried[n] & m)) { 189 if (!(iphp->rrp.tried[n] & m)) {
227 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); 242 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
228 243
229 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash; 244 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;
230 245
231 uscf->flags = NGX_HTTP_UPSTREAM_CREATE 246 uscf->flags = NGX_HTTP_UPSTREAM_CREATE
247 |NGX_HTTP_UPSTREAM_WEIGHT
232 |NGX_HTTP_UPSTREAM_MAX_FAILS 248 |NGX_HTTP_UPSTREAM_MAX_FAILS
233 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT 249 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
234 |NGX_HTTP_UPSTREAM_DOWN; 250 |NGX_HTTP_UPSTREAM_DOWN;
235 251
236 return NGX_CONF_OK; 252 return NGX_CONF_OK;