comparison src/stream/ngx_stream_upstream_hash_module.c @ 6148:bf8b6534db3a

Upstream hash: consistency across little/big endianness.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 18 May 2015 16:05:44 +0300
parents b6047abf5f30
children 68c106e6fa0a
comparison
equal deleted inserted replaced
6147:74b6ef56ea56 6148:bf8b6534db3a
269 ngx_stream_upstream_init_chash(ngx_conf_t *cf, 269 ngx_stream_upstream_init_chash(ngx_conf_t *cf,
270 ngx_stream_upstream_srv_conf_t *us) 270 ngx_stream_upstream_srv_conf_t *us)
271 { 271 {
272 u_char *host, *port, c; 272 u_char *host, *port, c;
273 size_t host_len, port_len, size; 273 size_t host_len, port_len, size;
274 uint32_t hash, base_hash, prev_hash; 274 uint32_t hash, base_hash;
275 ngx_str_t *server; 275 ngx_str_t *server;
276 ngx_uint_t npoints, i, j; 276 ngx_uint_t npoints, i, j;
277 ngx_stream_upstream_rr_peer_t *peer; 277 ngx_stream_upstream_rr_peer_t *peer;
278 ngx_stream_upstream_rr_peers_t *peers; 278 ngx_stream_upstream_rr_peers_t *peers;
279 ngx_stream_upstream_chash_points_t *points; 279 ngx_stream_upstream_chash_points_t *points;
280 ngx_stream_upstream_hash_srv_conf_t *hcf; 280 ngx_stream_upstream_hash_srv_conf_t *hcf;
281 union {
282 uint32_t value;
283 u_char byte[4];
284 } prev_hash;
281 285
282 if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) { 286 if (ngx_stream_upstream_init_round_robin(cf, us) != NGX_OK) {
283 return NGX_ERROR; 287 return NGX_ERROR;
284 } 288 }
285 289
342 ngx_crc32_init(base_hash); 346 ngx_crc32_init(base_hash);
343 ngx_crc32_update(&base_hash, host, host_len); 347 ngx_crc32_update(&base_hash, host, host_len);
344 ngx_crc32_update(&base_hash, (u_char *) "", 1); 348 ngx_crc32_update(&base_hash, (u_char *) "", 1);
345 ngx_crc32_update(&base_hash, port, port_len); 349 ngx_crc32_update(&base_hash, port, port_len);
346 350
347 prev_hash = 0; 351 prev_hash.value = 0;
348 npoints = peer->weight * 160; 352 npoints = peer->weight * 160;
349 353
350 for (j = 0; j < npoints; j++) { 354 for (j = 0; j < npoints; j++) {
351 hash = base_hash; 355 hash = base_hash;
352 356
353 ngx_crc32_update(&hash, (u_char *) &prev_hash, sizeof(uint32_t)); 357 ngx_crc32_update(&hash, prev_hash.byte, 4);
354 ngx_crc32_final(hash); 358 ngx_crc32_final(hash);
355 359
356 points->point[points->number].hash = hash; 360 points->point[points->number].hash = hash;
357 points->point[points->number].server = server; 361 points->point[points->number].server = server;
358 points->number++; 362 points->number++;
359 363
360 prev_hash = hash; 364 #if (NGX_HAVE_LITTLE_ENDIAN)
365 prev_hash.value = hash;
366 #else
367 prev_hash.byte[0] = (u_char) (hash & 0xff);
368 prev_hash.byte[1] = (u_char) ((hash >> 8) & 0xff);
369 prev_hash.byte[2] = (u_char) ((hash >> 16) & 0xff);
370 prev_hash.byte[3] = (u_char) ((hash >> 24) & 0xff);
371 #endif
361 } 372 }
362 } 373 }
363 374
364 ngx_qsort(points->point, 375 ngx_qsort(points->point,
365 points->number, 376 points->number,