Mercurial > hg > nginx
comparison src/http/ngx_http_file_cache.c @ 3052:6060225e9261
FreeBSD and Linux AIO support
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 28 Aug 2009 08:12:35 +0000 |
parents | 8101d9101ed8 |
children | 47b7b6ac433e |
comparison
equal
deleted
inserted
replaced
3051:26dfc0fa22c8 | 3052:6060225e9261 |
---|---|
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 #include <ngx_md5.h> | 10 #include <ngx_md5.h> |
11 | 11 |
12 | 12 |
13 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r, | |
14 ngx_http_cache_t *c); | |
15 #if (NGX_HAVE_FILE_AIO) | |
16 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev); | |
17 #endif | |
13 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, | 18 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, |
14 ngx_http_cache_t *c); | 19 ngx_http_cache_t *c); |
15 static ngx_http_file_cache_node_t * | 20 static ngx_http_file_cache_node_t * |
16 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key); | 21 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key); |
17 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | 22 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, |
171 | 176 |
172 | 177 |
173 ngx_int_t | 178 ngx_int_t |
174 ngx_http_file_cache_open(ngx_http_request_t *r) | 179 ngx_http_file_cache_open(ngx_http_request_t *r) |
175 { | 180 { |
176 u_char *p; | 181 u_char *p; |
177 time_t now; | 182 ngx_int_t rc, rv; |
178 ssize_t n; | 183 ngx_uint_t cold, test; |
179 ngx_int_t rc, rv; | 184 ngx_path_t *path; |
180 ngx_uint_t cold, test; | 185 ngx_http_cache_t *c; |
181 ngx_path_t *path; | 186 ngx_pool_cleanup_t *cln; |
182 ngx_http_cache_t *c; | 187 ngx_open_file_info_t of; |
183 ngx_pool_cleanup_t *cln; | 188 ngx_http_file_cache_t *cache; |
184 ngx_open_file_info_t of; | 189 ngx_http_core_loc_conf_t *clcf; |
185 ngx_http_file_cache_t *cache; | |
186 ngx_http_core_loc_conf_t *clcf; | |
187 ngx_http_file_cache_header_t *h; | |
188 | 190 |
189 c = r->cache; | 191 c = r->cache; |
192 | |
193 if (c->buf) { | |
194 return ngx_http_file_cache_read(r, c); | |
195 } | |
196 | |
190 cache = c->file_cache; | 197 cache = c->file_cache; |
191 | 198 |
192 cln = ngx_pool_cleanup_add(r->pool, 0); | 199 cln = ngx_pool_cleanup_add(r->pool, 0); |
193 if (cln == NULL) { | 200 if (cln == NULL) { |
194 return NGX_ERROR; | 201 return NGX_ERROR; |
205 | 212 |
206 cln->handler = ngx_http_file_cache_cleanup; | 213 cln->handler = ngx_http_file_cache_cleanup; |
207 cln->data = c; | 214 cln->data = c; |
208 | 215 |
209 if (rc == NGX_AGAIN) { | 216 if (rc == NGX_AGAIN) { |
210 return rc; | 217 return NGX_HTTP_CACHE_SCARCE; |
211 } | 218 } |
212 | 219 |
213 cold = cache->sh->cold; | 220 cold = cache->sh->cold; |
214 | 221 |
215 if (rc == NGX_OK) { | 222 if (rc == NGX_OK) { |
225 } else { /* rc == NGX_DECLINED */ | 232 } else { /* rc == NGX_DECLINED */ |
226 | 233 |
227 if (c->min_uses > 1) { | 234 if (c->min_uses > 1) { |
228 | 235 |
229 if (!cold) { | 236 if (!cold) { |
230 return NGX_AGAIN; | 237 return NGX_HTTP_CACHE_SCARCE; |
231 } | 238 } |
232 | 239 |
233 test = 1; | 240 test = 1; |
234 rv = NGX_AGAIN; | 241 rv = NGX_HTTP_CACHE_SCARCE; |
235 | 242 |
236 } else { | 243 } else { |
237 c->temp_file = 1; | 244 c->temp_file = 1; |
238 test = cold ? 1 : 0; | 245 test = cold ? 1 : 0; |
239 rv = NGX_DECLINED; | 246 rv = NGX_DECLINED; |
297 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | 304 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
298 "http file cache fd: %d", of.fd); | 305 "http file cache fd: %d", of.fd); |
299 | 306 |
300 c->file.fd = of.fd; | 307 c->file.fd = of.fd; |
301 c->file.log = r->connection->log; | 308 c->file.log = r->connection->log; |
309 c->uniq = of.uniq; | |
310 c->length = of.size; | |
302 | 311 |
303 c->buf = ngx_create_temp_buf(r->pool, c->body_start); | 312 c->buf = ngx_create_temp_buf(r->pool, c->body_start); |
304 if (c->buf == NULL) { | 313 if (c->buf == NULL) { |
305 return NGX_ERROR; | 314 return NGX_ERROR; |
306 } | 315 } |
307 | 316 |
317 return ngx_http_file_cache_read(r, c); | |
318 } | |
319 | |
320 | |
321 static ngx_int_t | |
322 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c) | |
323 { | |
324 time_t now; | |
325 ssize_t n; | |
326 ngx_int_t rc; | |
327 ngx_http_file_cache_t *cache; | |
328 ngx_http_file_cache_header_t *h; | |
329 | |
330 c = r->cache; | |
331 | |
332 #if (NGX_HAVE_FILE_AIO) | |
333 { | |
334 ngx_http_core_loc_conf_t *clcf; | |
335 | |
336 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
337 | |
338 if (clcf->aio) { | |
339 n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool); | |
340 | |
341 if (n == NGX_AGAIN) { | |
342 c->file.aio->data = r; | |
343 c->file.aio->handler = ngx_http_cache_aio_event_handler; | |
344 | |
345 r->main->blocked++; | |
346 r->aio = 1; | |
347 | |
348 return NGX_AGAIN; | |
349 } | |
350 | |
351 } else { | |
352 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0); | |
353 } | |
354 } | |
355 #else | |
356 | |
308 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0); | 357 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0); |
358 | |
359 #endif | |
309 | 360 |
310 if (n == NGX_ERROR) { | 361 if (n == NGX_ERROR) { |
311 return n; | 362 return n; |
312 } | 363 } |
313 | 364 |
329 | 380 |
330 c->valid_sec = h->valid_sec; | 381 c->valid_sec = h->valid_sec; |
331 c->last_modified = h->last_modified; | 382 c->last_modified = h->last_modified; |
332 c->date = h->date; | 383 c->date = h->date; |
333 c->valid_msec = h->valid_msec; | 384 c->valid_msec = h->valid_msec; |
334 c->length = of.size; | |
335 c->body_start = h->body_start; | 385 c->body_start = h->body_start; |
336 | 386 |
337 r->cached = 1; | 387 r->cached = 1; |
338 | 388 |
339 if (cold) { | 389 cache = c->file_cache; |
390 | |
391 if (cache->sh->cold) { | |
340 | 392 |
341 ngx_shmtx_lock(&cache->shpool->mutex); | 393 ngx_shmtx_lock(&cache->shpool->mutex); |
342 | 394 |
343 if (!c->node->exists) { | 395 if (!c->node->exists) { |
344 c->node->uses = 1; | 396 c->node->uses = 1; |
345 c->node->body_start = c->body_start; | 397 c->node->body_start = c->body_start; |
346 c->node->exists = 1; | 398 c->node->exists = 1; |
347 c->node->uniq = of.uniq; | 399 c->node->uniq = c->uniq; |
348 | 400 |
349 cache->sh->size += (c->length + cache->bsize - 1) / cache->bsize; | 401 cache->sh->size += (c->length + cache->bsize - 1) / cache->bsize; |
350 } | 402 } |
351 | 403 |
352 ngx_shmtx_unlock(&cache->shpool->mutex); | 404 ngx_shmtx_unlock(&cache->shpool->mutex); |
375 return rc; | 427 return rc; |
376 } | 428 } |
377 | 429 |
378 return NGX_OK; | 430 return NGX_OK; |
379 } | 431 } |
432 | |
433 | |
434 #if (NGX_HAVE_FILE_AIO) | |
435 | |
436 | |
437 static void | |
438 ngx_http_cache_aio_event_handler(ngx_event_t *ev) | |
439 { | |
440 ngx_event_aio_t *aio; | |
441 ngx_http_request_t *r; | |
442 | |
443 aio = ev->data; | |
444 r = aio->data; | |
445 | |
446 r->main->blocked--; | |
447 r->aio = 0; | |
448 | |
449 r->connection->write->handler(r->connection->write); | |
450 } | |
451 | |
452 #endif | |
380 | 453 |
381 | 454 |
382 static ngx_int_t | 455 static ngx_int_t |
383 ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c) | 456 ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c) |
384 { | 457 { |