Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Track upstream_fair's allocated SHM segments in a private list

Unfortunately, nginx's shared memory management seems pretty convoluted
an inflexible. Before I start digging into its internals, track
shared memory allocations with a simple linked list.

Also, change sponsorship information (per sponsor's request)
  • Loading branch information...
commit e20d707440d1b28bff4f0c53e1dc55b5b0eb9da3 1 parent 5d5ad28
Grzegorz Nosek authored

Showing 1 changed file with 45 additions and 9 deletions. Show diff stats Hide diff stats

  1. +45 9 ngx_http_upstream_fair_module.c
54 ngx_http_upstream_fair_module.c
... ... @@ -1,6 +1,6 @@
1 1 /*
2 2 * Copyright (C) 2007 Grzegorz Nosek
3   - * Work sponsored by Ezra Zygmuntowicz
  3 + * Work sponsored by Ezra Zygmuntowicz & EngineYard.com
4 4 *
5 5 * Based on nginx source (C) Igor Sysoev
6 6 */
@@ -31,6 +31,13 @@ typedef struct {
31 31 } ngx_http_upstream_fair_peer_data_t;
32 32
33 33
  34 +typedef struct ngx_http_upstream_fair_shm_link_s {
  35 + ngx_shm_t shm;
  36 + struct ngx_http_upstream_fair_shm_link_s *next;
  37 +} ngx_http_upstream_fair_shm_link_t;
  38 +
  39 +
  40 +static ngx_int_t ngx_http_upstream_fair_init_module(ngx_cycle_t *cycle);
34 41 static ngx_int_t ngx_http_upstream_init_fair(ngx_conf_t *cf,
35 42 ngx_http_upstream_srv_conf_t *us);
36 43 static ngx_int_t ngx_http_upstream_get_fair_peer(ngx_peer_connection_t *pc,
@@ -77,7 +84,7 @@ ngx_module_t ngx_http_upstream_fair_module = {
77 84 ngx_http_upstream_fair_commands, /* module directives */
78 85 NGX_HTTP_MODULE, /* module type */
79 86 NULL, /* init master */
80   - NULL, /* init module */
  87 + ngx_http_upstream_fair_init_module, /* init module */
81 88 NULL, /* init process */
82 89 NULL, /* init thread */
83 90 NULL, /* exit thread */
@@ -87,6 +94,25 @@ ngx_module_t ngx_http_upstream_fair_module = {
87 94 };
88 95
89 96
  97 +/* this isn't very pretty, but nginx's shm_zones aren't, either */
  98 +static ngx_http_upstream_fair_shm_link_t *shm_list;
  99 +
  100 +static ngx_int_t
  101 +ngx_http_upstream_fair_init_module(ngx_cycle_t *cycle)
  102 +{
  103 + ngx_http_upstream_fair_shm_link_t *link = shm_list, *prev;
  104 +
  105 + while (link) {
  106 + prev = link;
  107 + link = link->next;
  108 + ngx_shm_free(&prev->shm);
  109 + free(prev);
  110 + }
  111 +
  112 + shm_list = NULL;
  113 + return NGX_OK;
  114 +}
  115 +
90 116 static char *
91 117 ngx_http_upstream_fair(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
92 118 {
@@ -111,7 +137,7 @@ ngx_http_upstream_init_fair(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
111 137 {
112 138 ngx_http_upstream_fair_peers_t *peers;
113 139 ngx_uint_t n;
114   - ngx_shm_t shm;
  140 + ngx_http_upstream_fair_shm_link_t *shm_link;
115 141
116 142 /* do the dirty work using rr module */
117 143 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
@@ -126,15 +152,25 @@ ngx_http_upstream_init_fair(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
126 152 peers->rrp = us->peer.data;
127 153 n = peers->rrp->number;
128 154
129   - shm.size = n * sizeof(ngx_http_upstream_fair_shared_t);
130   - shm.log = cf->log;
  155 + /* a plain malloc, not nginx's pool allocator functions */
  156 + shm_link = malloc(sizeof *shm_link);
  157 + if (!shm_link) {
  158 + return NGX_ERROR;
  159 + }
  160 +
  161 + shm_link->shm.size = n * sizeof(ngx_http_upstream_fair_shared_t);
  162 + shm_link->shm.log = cf->log;
131 163
132   - /* TODO: find a good place to free it (will probably leak on reload) */
133   - if (ngx_shm_alloc(&shm) != NGX_OK) {
  164 + if (ngx_shm_alloc(&shm_link->shm) != NGX_OK) {
  165 + free(shm_link);
134 166 return NGX_ERROR;
135 167 }
136   - peers->shared = (ngx_http_upstream_fair_shared_t *)shm.addr;
137   - ngx_memset(peers->shared, 0, shm.size);
  168 +
  169 + shm_link->next = shm_list;
  170 + shm_list = shm_link;
  171 +
  172 + peers->shared = (ngx_http_upstream_fair_shared_t *)shm_link->shm.addr;
  173 + ngx_memset(peers->shared, 0, shm_link->shm.size);
138 174
139 175 us->peer.init = ngx_http_upstream_init_fair_peer;
140 176

0 comments on commit e20d707

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