Mercurial > hg > nginx
comparison src/http/modules/ngx_http_upstream_zone_module.c @ 6103:79ddb0bdb273
Upstream: the "zone" directive.
Upstreams with the "zone" directive are kept in shared memory,
with a consistent view of all worker processes.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Tue, 14 Apr 2015 19:01:25 +0300 |
parents | |
children | caa103acf180 |
comparison
equal
deleted
inserted
replaced
6102:3264b7828f72 | 6103:79ddb0bdb273 |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Ruslan Ermilov | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 static char *ngx_http_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
14 void *conf); | |
15 static ngx_int_t ngx_http_upstream_init_zone(ngx_shm_zone_t *shm_zone, | |
16 void *data); | |
17 | |
18 | |
19 static ngx_command_t ngx_http_upstream_zone_commands[] = { | |
20 | |
21 { ngx_string("zone"), | |
22 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE2, | |
23 ngx_http_upstream_zone, | |
24 0, | |
25 0, | |
26 NULL }, | |
27 | |
28 ngx_null_command | |
29 }; | |
30 | |
31 | |
32 static ngx_http_module_t ngx_http_upstream_zone_module_ctx = { | |
33 NULL, /* preconfiguration */ | |
34 NULL, /* postconfiguration */ | |
35 | |
36 NULL, /* create main configuration */ | |
37 NULL, /* init main configuration */ | |
38 | |
39 NULL, /* create server configuration */ | |
40 NULL, /* merge server configuration */ | |
41 | |
42 NULL, /* create location configuration */ | |
43 NULL /* merge location configuration */ | |
44 }; | |
45 | |
46 | |
47 ngx_module_t ngx_http_upstream_zone_module = { | |
48 NGX_MODULE_V1, | |
49 &ngx_http_upstream_zone_module_ctx, /* module context */ | |
50 ngx_http_upstream_zone_commands, /* module directives */ | |
51 NGX_HTTP_MODULE, /* module type */ | |
52 NULL, /* init master */ | |
53 NULL, /* init module */ | |
54 NULL, /* init process */ | |
55 NULL, /* init thread */ | |
56 NULL, /* exit thread */ | |
57 NULL, /* exit process */ | |
58 NULL, /* exit master */ | |
59 NGX_MODULE_V1_PADDING | |
60 }; | |
61 | |
62 | |
63 static char * | |
64 ngx_http_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
65 { | |
66 ngx_http_upstream_srv_conf_t *uscf; | |
67 ssize_t size; | |
68 ngx_str_t *value; | |
69 | |
70 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); | |
71 | |
72 value = cf->args->elts; | |
73 | |
74 if (!value[1].len) { | |
75 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
76 "invalid zone name \"%V\"", &value[1]); | |
77 return NGX_CONF_ERROR; | |
78 } | |
79 | |
80 size = ngx_parse_size(&value[2]); | |
81 | |
82 if (size == NGX_ERROR) { | |
83 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
84 "invalid zone size \"%V\"", &value[2]); | |
85 return NGX_CONF_ERROR; | |
86 } | |
87 | |
88 if (size < (ssize_t) (8 * ngx_pagesize)) { | |
89 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
90 "zone \"%V\" is too small", &value[1]); | |
91 return NGX_CONF_ERROR; | |
92 } | |
93 | |
94 uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size, | |
95 &ngx_http_upstream_module); | |
96 if (uscf->shm_zone == NULL) { | |
97 return NGX_CONF_ERROR; | |
98 } | |
99 | |
100 if (uscf->shm_zone->data) { | |
101 uscf = uscf->shm_zone->data; | |
102 | |
103 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
104 "upstream \"%V\" in %s:%ui " | |
105 "is already bound to zone \"%V\"", | |
106 &uscf->host, uscf->file_name, uscf->line, | |
107 &value[1]); | |
108 return NGX_CONF_ERROR; | |
109 } | |
110 | |
111 uscf->shm_zone->init = ngx_http_upstream_init_zone; | |
112 uscf->shm_zone->data = uscf; | |
113 | |
114 uscf->shm_zone->noreuse = 1; | |
115 | |
116 return NGX_CONF_OK; | |
117 } | |
118 | |
119 | |
120 static ngx_int_t | |
121 ngx_http_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data) | |
122 { | |
123 ngx_http_upstream_srv_conf_t *ouscf = data; | |
124 | |
125 size_t len; | |
126 ngx_slab_pool_t *shpool; | |
127 ngx_http_upstream_rr_peer_t *peer, **peerp; | |
128 ngx_http_upstream_rr_peers_t *peers, *backup; | |
129 ngx_http_upstream_srv_conf_t *uscf; | |
130 | |
131 uscf = shm_zone->data; | |
132 | |
133 if (ouscf) { | |
134 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, | |
135 "zone \"%V\" cannot be reused", &shm_zone->shm.name); | |
136 return NGX_ERROR; | |
137 } | |
138 | |
139 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
140 | |
141 if (shm_zone->shm.exists) { | |
142 return NGX_ERROR; | |
143 } | |
144 | |
145 | |
146 /* copy peers to shared memory */ | |
147 | |
148 len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len; | |
149 | |
150 shpool->log_ctx = ngx_slab_alloc(shpool, len); | |
151 if (shpool->log_ctx == NULL) { | |
152 return NGX_ERROR; | |
153 } | |
154 | |
155 ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z", | |
156 &shm_zone->shm.name); | |
157 | |
158 peers = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t)); | |
159 if (peers == NULL) { | |
160 return NGX_ERROR; | |
161 } | |
162 | |
163 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_http_upstream_rr_peers_t)); | |
164 | |
165 peers->shpool = shpool; | |
166 | |
167 for (peerp = &peers->peer; *peerp; peerp = &peer->next) { | |
168 /* pool is unlocked */ | |
169 peer = ngx_slab_calloc_locked(shpool, | |
170 sizeof(ngx_http_upstream_rr_peer_t)); | |
171 if (peer == NULL) { | |
172 return NGX_ERROR; | |
173 } | |
174 | |
175 ngx_memcpy(peer, *peerp, sizeof(ngx_http_upstream_rr_peer_t)); | |
176 | |
177 *peerp = peer; | |
178 } | |
179 | |
180 if (peers->next == NULL) { | |
181 goto done; | |
182 } | |
183 | |
184 backup = ngx_slab_alloc(shpool, sizeof(ngx_http_upstream_rr_peers_t)); | |
185 if (backup == NULL) { | |
186 return NGX_ERROR; | |
187 } | |
188 | |
189 ngx_memcpy(backup, peers->next, sizeof(ngx_http_upstream_rr_peers_t)); | |
190 | |
191 backup->shpool = shpool; | |
192 | |
193 for (peerp = &backup->peer; *peerp; peerp = &peer->next) { | |
194 /* pool is unlocked */ | |
195 peer = ngx_slab_calloc_locked(shpool, | |
196 sizeof(ngx_http_upstream_rr_peer_t)); | |
197 if (peer == NULL) { | |
198 return NGX_ERROR; | |
199 } | |
200 | |
201 ngx_memcpy(peer, *peerp, sizeof(ngx_http_upstream_rr_peer_t)); | |
202 | |
203 *peerp = peer; | |
204 } | |
205 | |
206 peers->next = backup; | |
207 | |
208 done: | |
209 | |
210 uscf->peer.data = peers; | |
211 | |
212 return NGX_OK; | |
213 } |