comparison src/http/ngx_http_file_cache.c @ 6795:1a917932db96

Cache: prefix-based temporary files. On Linux, the rename syscall can be slow due to a global file system lock, acquired for the entire rename operation, unless both old and new files are in the same directory. To address this temporary files are now created in the same directory as the expected resulting cache file when using the "use_temp_path=off" parameter. This change mostly reverts 99639bfdfa2a and 3281de8142f5, restoring the behaviour as of a9138c35120d (with minor changes).
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 03 Nov 2016 17:10:29 +0300
parents 3fbb3bdff824
children f18c285c2e59
comparison
equal deleted inserted replaced
6794:93b294c5d581 6795:1a917932db96
2110 2110
2111 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) { 2111 if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
2112 return NGX_ERROR; 2112 return NGX_ERROR;
2113 } 2113 }
2114 2114
2115 /*
2116 * Temporary files in cache have a suffix consisting of a dot
2117 * followed by 10 digits.
2118 */
2119
2120 if (name->len >= 2 * NGX_HTTP_CACHE_KEY_LEN + 1 + 10
2121 && name->data[name->len - 10 - 1] == '.')
2122 {
2123 return NGX_OK;
2124 }
2125
2115 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) { 2126 if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
2116 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0, 2127 ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
2117 "cache file \"%s\" is too small", name->data); 2128 "cache file \"%s\" is too small", name->data);
2118 return NGX_ERROR; 2129 return NGX_ERROR;
2119 } 2130 }
2254 char *confp = conf; 2265 char *confp = conf;
2255 2266
2256 off_t max_size; 2267 off_t max_size;
2257 u_char *last, *p; 2268 u_char *last, *p;
2258 time_t inactive; 2269 time_t inactive;
2259 size_t len;
2260 ssize_t size; 2270 ssize_t size;
2261 ngx_str_t s, name, *value; 2271 ngx_str_t s, name, *value;
2262 ngx_int_t loader_files, manager_files; 2272 ngx_int_t loader_files, manager_files;
2263 ngx_msec_t loader_sleep, manager_sleep, loader_threshold, 2273 ngx_msec_t loader_sleep, manager_sleep, loader_threshold,
2264 manager_threshold; 2274 manager_threshold;
2527 2537
2528 if (ngx_add_path(cf, &cache->path) != NGX_OK) { 2538 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
2529 return NGX_CONF_ERROR; 2539 return NGX_CONF_ERROR;
2530 } 2540 }
2531 2541
2532 if (!use_temp_path) {
2533 cache->temp_path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
2534 if (cache->temp_path == NULL) {
2535 return NGX_CONF_ERROR;
2536 }
2537
2538 len = cache->path->name.len + sizeof("/temp") - 1;
2539
2540 p = ngx_pnalloc(cf->pool, len + 1);
2541 if (p == NULL) {
2542 return NGX_CONF_ERROR;
2543 }
2544
2545 cache->temp_path->name.len = len;
2546 cache->temp_path->name.data = p;
2547
2548 p = ngx_cpymem(p, cache->path->name.data, cache->path->name.len);
2549 ngx_memcpy(p, "/temp", sizeof("/temp"));
2550
2551 ngx_memcpy(&cache->temp_path->level, &cache->path->level,
2552 NGX_MAX_PATH_LEVEL * sizeof(size_t));
2553
2554 cache->temp_path->len = cache->path->len;
2555 cache->temp_path->conf_file = cf->conf_file->file.name.data;
2556 cache->temp_path->line = cf->conf_file->line;
2557
2558 if (ngx_add_path(cf, &cache->temp_path) != NGX_OK) {
2559 return NGX_CONF_ERROR;
2560 }
2561 }
2562
2563 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post); 2542 cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
2564 if (cache->shm_zone == NULL) { 2543 if (cache->shm_zone == NULL) {
2565 return NGX_CONF_ERROR; 2544 return NGX_CONF_ERROR;
2566 } 2545 }
2567 2546
2572 } 2551 }
2573 2552
2574 2553
2575 cache->shm_zone->init = ngx_http_file_cache_init; 2554 cache->shm_zone->init = ngx_http_file_cache_init;
2576 cache->shm_zone->data = cache; 2555 cache->shm_zone->data = cache;
2556
2557 cache->use_temp_path = use_temp_path;
2577 2558
2578 cache->inactive = inactive; 2559 cache->inactive = inactive;
2579 cache->max_size = max_size; 2560 cache->max_size = max_size;
2580 2561
2581 caches = (ngx_array_t *) (confp + cmd->offset); 2562 caches = (ngx_array_t *) (confp + cmd->offset);