summaryrefslogtreecommitdiff
path: root/src/http/ngx_http_file_cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/ngx_http_file_cache.c')
-rw-r--r--src/http/ngx_http_file_cache.c176
1 files changed, 93 insertions, 83 deletions
diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
index f44fe1de1..b9ea606a2 100644
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -31,7 +31,7 @@ static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
ngx_queue_t *q, u_char *name);
static ngx_int_t
- ngx_http_file_cache_manager_sleep(ngx_http_file_cache_t *cache);
+ ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache);
static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
ngx_str_t *path);
static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
@@ -376,7 +376,7 @@ ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
if ((size_t) n < c->header_start) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
"cache file \"%s\" is too small", c->file.name.data);
- return NGX_ERROR;
+ return NGX_DECLINED;
}
h = (ngx_http_file_cache_header_t *) c->buf->pos;
@@ -530,26 +530,19 @@ ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
goto done;
}
- if (fcn->exists) {
+ if (fcn->exists || fcn->uses >= c->min_uses) {
c->exists = fcn->exists;
- c->body_start = fcn->body_start;
+ if (fcn->body_start) {
+ c->body_start = fcn->body_start;
+ }
rc = NGX_OK;
goto done;
}
- if (fcn->uses >= c->min_uses) {
-
- c->exists = fcn->exists;
- c->body_start = fcn->body_start;
-
- rc = NGX_OK;
-
- } else {
- rc = NGX_AGAIN;
- }
+ rc = NGX_AGAIN;
goto done;
}
@@ -858,7 +851,7 @@ ngx_http_cache_send(ngx_http_request_t *r)
c = r->cache;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
- "http file cache send: %s", c->file.name.data);
+ "http file cache send: %s", c->file.name.data);
/* we need to allocate all before the header would be sent */
@@ -1214,7 +1207,7 @@ ngx_http_file_cache_manager(void *data)
return wait;
}
- if (ngx_http_file_cache_manager_sleep(cache) != NGX_OK) {
+ if (ngx_quit || ngx_terminate) {
return next;
}
}
@@ -1268,32 +1261,39 @@ ngx_http_file_cache_loader(void *data)
static ngx_int_t
-ngx_http_file_cache_manager_sleep(ngx_http_file_cache_t *cache)
+ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
{
ngx_msec_t elapsed;
- if (cache->files++ > 100) {
+ if (++cache->files >= cache->loader_files) {
ngx_time_update();
elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
- "http file cache manager time: %M", elapsed);
+ "http file cache loader time elapsed: %M", elapsed);
- if (elapsed > 200) {
+ if (elapsed >= cache->loader_threshold) {
- /*
- * if processing 100 files takes more than 200ms,
- * it seems that many operations require disk i/o,
- * therefore sleep 200ms
- */
+ if (cache->loader_files > 1) {
+ cache->loader_files /= 2;
+ ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
+ "cache %V loader_files decreased to %ui",
+ &cache->path->name, cache->loader_files);
- ngx_msleep(200);
-
- ngx_time_update();
+ } else {
+ cache->loader_sleep *= 2;
+ ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
+ "cache %V loader_sleep increased to %Mms",
+ &cache->path->name, cache->loader_sleep);
+ }
}
+ ngx_msleep(cache->loader_sleep);
+
+ ngx_time_update();
+
cache->last = ngx_current_msec;
cache->files = 0;
}
@@ -1320,75 +1320,34 @@ ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
(void) ngx_http_file_cache_delete_file(ctx, path);
}
- return ngx_http_file_cache_manager_sleep(cache);
+ return ngx_http_file_cache_loader_sleep(cache);
}
static ngx_int_t
ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
{
- u_char *p;
- ngx_fd_t fd;
- ngx_int_t n;
- ngx_uint_t i;
- ngx_file_info_t fi;
- ngx_http_cache_t c;
- ngx_http_file_cache_t *cache;
- ngx_http_file_cache_header_t h;
+ u_char *p;
+ ngx_int_t n;
+ ngx_uint_t i;
+ ngx_http_cache_t c;
+ ngx_http_file_cache_t *cache;
if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
return NGX_ERROR;
}
- ngx_memzero(&c, sizeof(ngx_http_cache_t));
-
- fd = ngx_open_file(name->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
-
- if (fd == NGX_INVALID_FILE) {
- ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
- ngx_open_file_n " \"%s\" failed", name->data);
- return NGX_ERROR;
- }
-
- c.file.fd = fd;
- c.file.name = *name;
- c.file.log = ctx->log;
-
- n = ngx_read_file(&c.file, (u_char *) &h,
- sizeof(ngx_http_file_cache_header_t), 0);
- if (n == NGX_ERROR) {
- return NGX_ERROR;
- }
-
- if ((size_t) n < sizeof(ngx_http_file_cache_header_t)) {
+ if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
"cache file \"%s\" is too small", name->data);
return NGX_ERROR;
}
+ ngx_memzero(&c, sizeof(ngx_http_cache_t));
cache = ctx->data;
- if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
- ngx_fd_info_n " \"%s\" failed", name->data);
-
- } else {
- c.uniq = ngx_file_uniq(&fi);
- c.valid_sec = h.valid_sec;
- c.valid_msec = h.valid_msec;
- c.body_start = h.body_start;
- c.length = ngx_file_size(&fi);
- c.fs_size = (ngx_file_fs_size(&fi) + cache->bsize - 1) / cache->bsize;
- }
-
- if (ngx_close_file(fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
- ngx_close_file_n " \"%s\" failed", name->data);
- }
-
- if (c.body_start == 0) {
- return NGX_ERROR;
- }
+ c.length = ctx->size;
+ c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
@@ -1435,14 +1394,14 @@ ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
fcn->uses = 1;
fcn->count = 0;
- fcn->valid_msec = c->valid_msec;
+ fcn->valid_msec = 0;
fcn->error = 0;
fcn->exists = 1;
fcn->updating = 0;
fcn->deleting = 0;
- fcn->uniq = c->uniq;
- fcn->valid_sec = c->valid_sec;
- fcn->body_start = c->body_start;
+ fcn->uniq = 0;
+ fcn->valid_sec = 0;
+ fcn->body_start = 0;
fcn->fs_size = c->fs_size;
cache->sh->size += c->fs_size;
@@ -1510,6 +1469,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
time_t inactive;
ssize_t size;
ngx_str_t s, name, *value;
+ ngx_int_t loader_files, loader_sleep, loader_threshold;
ngx_uint_t i, n;
ngx_http_file_cache_t *cache;
@@ -1524,6 +1484,9 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
inactive = 600;
+ loader_files = 100;
+ loader_sleep = 50;
+ loader_threshold = 200;
name.len = 0;
size = 0;
@@ -1637,6 +1600,48 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
continue;
}
+ if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {
+
+ loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
+ if (loader_files == NGX_ERROR) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid loader_files value \"%V\"", &value[i]);
+ return NGX_CONF_ERROR;
+ }
+
+ continue;
+ }
+
+ if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {
+
+ s.len = value[i].len - 13;
+ s.data = value[i].data + 13;
+
+ loader_sleep = ngx_parse_time(&s, 0);
+ if (loader_sleep < 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid loader_sleep value \"%V\"", &value[i]);
+ return NGX_CONF_ERROR;
+ }
+
+ continue;
+ }
+
+ if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {
+
+ s.len = value[i].len - 17;
+ s.data = value[i].data + 17;
+
+ loader_threshold = ngx_parse_time(&s, 0);
+ if (loader_threshold < 0) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid loader_threshold value \"%V\"", &value[i]);
+ return NGX_CONF_ERROR;
+ }
+
+ continue;
+ }
+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[i]);
return NGX_CONF_ERROR;
@@ -1652,6 +1657,11 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
cache->path->manager = ngx_http_file_cache_manager;
cache->path->loader = ngx_http_file_cache_loader;
cache->path->data = cache;
+ cache->path->conf_file = cf->conf_file->file.name.data;
+ cache->path->line = cf->conf_file->line;
+ cache->loader_files = loader_files;
+ cache->loader_sleep = (ngx_msec_t) loader_sleep;
+ cache->loader_threshold = (ngx_msec_t) loader_threshold;
if (ngx_add_path(cf, &cache->path) != NGX_OK) {
return NGX_CONF_ERROR;