Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Keep our own generation counter instead of relying on ngx_cycle

  • Loading branch information...
commit 009ae97e4c6bc779605660803fc7f5ecf27c1290 1 parent fe9b808
Grzegorz Nosek authored

Showing 1 changed file with 19 additions and 13 deletions. Show diff stats Hide diff stats

  1. +19 13 ngx_http_upstream_fair_module.c
32 ngx_http_upstream_fair_module.c
@@ -21,8 +21,8 @@ typedef struct ngx_http_upstream_fair_peers_s ngx_http_upstream_fair_peers_t;
21 21
22 22 typedef struct {
23 23 ngx_rbtree_node_t node;
24   - uintptr_t cycle;
25   - uintptr_t peers; /* forms a unique cookie together with cycle */
  24 + ngx_uint_t generation;
  25 + uintptr_t peers; /* forms a unique cookie together with generation */
26 26 ngx_int_t refcount; /* accessed only under shmtx_lock */
27 27 ngx_uint_t total_requests;
28 28 ngx_atomic_t lock;
@@ -95,6 +95,7 @@ static char *ngx_http_upstream_fair(ngx_conf_t *cf, ngx_command_t *cmd,
95 95 void *conf);
96 96 static char *ngx_http_upstream_fair_set_shm_size(ngx_conf_t *cf,
97 97 ngx_command_t *cmd, void *conf);
  98 +static ngx_int_t ngx_http_upstream_fair_init_module(ngx_cycle_t *cycle);
98 99
99 100 #if (NGX_HTTP_EXTENDED_STATUS)
100 101 static ngx_chain_t *ngx_http_upstream_fair_report_status(ngx_http_request_t *r,
@@ -153,7 +154,7 @@ ngx_module_t ngx_http_upstream_fair_module = {
153 154 ngx_http_upstream_fair_commands, /* module directives */
154 155 NGX_HTTP_MODULE, /* module type */
155 156 NULL, /* init master */
156   - NULL, /* init module */
  157 + ngx_http_upstream_fair_init_module, /* init module */
157 158 NULL, /* init process */
158 159 NULL, /* init thread */
159 160 NULL, /* exit thread */
@@ -166,6 +167,7 @@ ngx_module_t ngx_http_upstream_fair_module = {
166 167 static ngx_uint_t ngx_http_upstream_fair_shm_size;
167 168 static ngx_shm_zone_t * ngx_http_upstream_fair_shm_zone;
168 169 static ngx_rbtree_t * ngx_http_upstream_fair_rbtree;
  170 +static ngx_uint_t ngx_http_upstream_fair_generation;
169 171
170 172 static int
171 173 ngx_http_upstream_fair_compare_rbtree_node(const ngx_rbtree_node_t *v_left,
@@ -176,11 +178,11 @@ ngx_http_upstream_fair_compare_rbtree_node(const ngx_rbtree_node_t *v_left,
176 178 left = (ngx_http_upstream_fair_shm_block_t *) v_left;
177 179 right = (ngx_http_upstream_fair_shm_block_t *) v_right;
178 180
179   - if (left->cycle < right->cycle) {
  181 + if (left->generation < right->generation) {
180 182 return -1;
181   - } else if (left->cycle > right->cycle) {
  183 + } else if (left->generation > right->generation) {
182 184 return 1;
183   - } else { /* left->cycle == right->cycle */
  185 + } else { /* left->generation == right->generation */
184 186 if (left->peers < right->peers) {
185 187 return -1;
186 188 } else if (left->peers > right->peers) {
@@ -287,6 +289,13 @@ ngx_bitvector_set(uintptr_t *bv, ngx_uint_t bit)
287 289 * generic functions end here
288 290 */
289 291
  292 +static ngx_int_t
  293 +ngx_http_upstream_fair_init_module(ngx_cycle_t *cycle)
  294 +{
  295 + ngx_http_upstream_fair_generation++;
  296 + return NGX_OK;
  297 +}
  298 +
290 299 static void
291 300 ngx_http_upstream_fair_rbtree_insert(ngx_rbtree_node_t *temp,
292 301 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) {
@@ -1009,7 +1018,7 @@ ngx_http_upstream_fair_walk_shm(
1009 1018
1010 1019 /* visit current node */
1011 1020 uf_node = (ngx_http_upstream_fair_shm_block_t *) node;
1012   - if (uf_node->cycle != (uintptr_t) ngx_cycle) {
  1021 + if (uf_node->generation != ngx_http_upstream_fair_generation) {
1013 1022 if (--uf_node->refcount == 0) {
1014 1023 ngx_rbtree_delete(ngx_http_upstream_fair_rbtree, node);
1015 1024 ngx_slab_free_locked(shpool, node);
@@ -1064,10 +1073,7 @@ ngx_http_upstream_fair_shm_alloc(ngx_http_upstream_fair_peers_t *usfp, ngx_log_t
1064 1073 usfp->shared->node.key = ngx_crc32_short((u_char *) &ngx_cycle, sizeof ngx_cycle) ^
1065 1074 ngx_crc32_short((u_char *) &usfp, sizeof(usfp));
1066 1075
1067   - /* we're casting away 'volatile' here which is fine because
1068   - * we don't expect any single value to ever change
1069   - */
1070   - usfp->shared->cycle = (uintptr_t) ngx_cycle;
  1076 + usfp->shared->generation = ngx_http_upstream_fair_generation;
1071 1077 usfp->shared->peers = (uintptr_t) usfp;
1072 1078 usfp->shared->refcount = 1;
1073 1079 usfp->shared->total_requests = 0;
@@ -1233,12 +1239,12 @@ ngx_http_upstream_fair_walk_status(ngx_pool_t *pool, ngx_chain_t *cl, ngx_int_t
1233 1239 }
1234 1240 }
1235 1241
1236   - if (s_node->cycle != (uintptr_t) ngx_cycle) {
  1242 + if (s_node->generation != ngx_http_upstream_fair_generation) {
1237 1243 goto next;
1238 1244 }
1239 1245
1240 1246 /* this is rather ugly (casting an uintptr_t back into a pointer
1241   - * but as long as the cycle is still the same (verified above),
  1247 + * but as long as the generation is still the same (verified above),
1242 1248 * it should be still safe
1243 1249 */
1244 1250 peers = (ngx_http_upstream_fair_peers_t *) s_node->peers;

0 comments on commit 009ae97

Please sign in to comment.
Something went wrong with that request. Please try again.