Klaus Demo nginx / b072a69
Cache: cache manager limits. The new parameters "manager_files", "manager_sleep" and "manager_threshold" were added to proxy_cache_path and friends. Note that ngx_path_manager_pt was changed to return ngx_msec_t instead of time_t (API change). Dmitry Volyntsev 5 years ago
5 changed file(s) with 105 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
4141 #define NGX_MAX_PATH_LEVEL 3
4242
4343
44 typedef time_t (*ngx_path_manager_pt) (void *data);
44 typedef ngx_msec_t (*ngx_path_manager_pt) (void *data);
4545 typedef ngx_msec_t (*ngx_path_purger_pt) (void *data);
4646 typedef void (*ngx_path_loader_pt) (void *data);
4747
165165 ngx_msec_t loader_sleep;
166166 ngx_msec_t loader_threshold;
167167
168 ngx_uint_t manager_files;
169 ngx_msec_t manager_sleep;
170 ngx_msec_t manager_threshold;
171
168172 ngx_shm_zone_t *shm_zone;
169173 };
170174
17581758 size_t len;
17591759 time_t now, wait;
17601760 ngx_path_t *path;
1761 ngx_msec_t elapsed;
17611762 ngx_queue_t *q;
17621763 ngx_http_file_cache_node_t *fcn;
17631764 u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];
18091810
18101811 if (fcn->count == 0) {
18111812 ngx_http_file_cache_delete(cache, q, name);
1812 continue;
1813 goto next;
18131814 }
18141815
18151816 if (fcn->deleting) {
18351836 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
18361837 "ignore long locked inactive cache entry %*s, count:%d",
18371838 (size_t) 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
1839
1840 next:
1841
1842 if (++cache->files >= cache->manager_files) {
1843 wait = 0;
1844 break;
1845 }
1846
1847 ngx_time_update();
1848
1849 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
1850
1851 if (elapsed >= cache->manager_threshold) {
1852 wait = 0;
1853 break;
1854 }
18381855 }
18391856
18401857 ngx_shmtx_unlock(&cache->shpool->mutex);
18961913 }
18971914
18981915
1899 static time_t
1916 static ngx_msec_t
19001917 ngx_http_file_cache_manager(void *data)
19011918 {
19021919 ngx_http_file_cache_t *cache = data;
19031920
19041921 off_t size;
19051922 time_t next, wait;
1923 ngx_msec_t elapsed;
19061924 ngx_uint_t count, watermark;
1907
1908 next = ngx_http_file_cache_expire(cache);
19091925
19101926 cache->last = ngx_current_msec;
19111927 cache->files = 0;
1928
1929 next = ngx_http_file_cache_expire(cache);
1930
1931 if (next == 0) {
1932 return cache->manager_sleep;
1933 }
19121934
19131935 for ( ;; ) {
19141936 ngx_shmtx_lock(&cache->shpool->mutex);
19241946 size, count, (ngx_int_t) watermark);
19251947
19261948 if (size < cache->max_size && count < watermark) {
1927 return next;
1949 return (ngx_msec_t) next * 1000;
19281950 }
19291951
19301952 wait = ngx_http_file_cache_forced_expire(cache);
19311953
19321954 if (wait > 0) {
1933 return wait;
1955 return (ngx_msec_t) wait * 1000;
19341956 }
19351957
19361958 if (ngx_quit || ngx_terminate) {
1937 return next;
1959 return (ngx_msec_t) next * 1000;
1960 }
1961
1962 if (++cache->files >= cache->manager_files) {
1963 return cache->manager_sleep;
1964 }
1965
1966 ngx_time_update();
1967
1968 elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
1969
1970 if (elapsed >= cache->manager_threshold) {
1971 return cache->manager_sleep;
19381972 }
19391973 }
19401974 }
22102244 size_t len;
22112245 ssize_t size;
22122246 ngx_str_t s, name, *value;
2213 ngx_int_t loader_files;
2214 ngx_msec_t loader_sleep, loader_threshold;
2247 ngx_int_t loader_files, manager_files;
2248 ngx_msec_t loader_sleep, manager_sleep, loader_threshold,
2249 manager_threshold;
22152250 ngx_uint_t i, n, use_temp_path;
22162251 ngx_array_t *caches;
22172252 ngx_http_file_cache_t *cache, **ce;
22292264 use_temp_path = 1;
22302265
22312266 inactive = 600;
2267
22322268 loader_files = 100;
22332269 loader_sleep = 50;
22342270 loader_threshold = 200;
2271
2272 manager_files = 100;
2273 manager_sleep = 50;
2274 manager_threshold = 200;
22352275
22362276 name.len = 0;
22372277 size = 0;
23982438 if (loader_threshold == (ngx_msec_t) NGX_ERROR) {
23992439 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
24002440 "invalid loader_threshold value \"%V\"", &value[i]);
2441 return NGX_CONF_ERROR;
2442 }
2443
2444 continue;
2445 }
2446
2447 if (ngx_strncmp(value[i].data, "manager_files=", 14) == 0) {
2448
2449 manager_files = ngx_atoi(value[i].data + 14, value[i].len - 14);
2450 if (manager_files == NGX_ERROR) {
2451 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2452 "invalid manager_files value \"%V\"", &value[i]);
2453 return NGX_CONF_ERROR;
2454 }
2455
2456 continue;
2457 }
2458
2459 if (ngx_strncmp(value[i].data, "manager_sleep=", 14) == 0) {
2460
2461 s.len = value[i].len - 14;
2462 s.data = value[i].data + 14;
2463
2464 manager_sleep = ngx_parse_time(&s, 0);
2465 if (manager_sleep == (ngx_msec_t) NGX_ERROR) {
2466 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2467 "invalid manager_sleep value \"%V\"", &value[i]);
2468 return NGX_CONF_ERROR;
2469 }
2470
2471 continue;
2472 }
2473
2474 if (ngx_strncmp(value[i].data, "manager_threshold=", 18) == 0) {
2475
2476 s.len = value[i].len - 18;
2477 s.data = value[i].data + 18;
2478
2479 manager_threshold = ngx_parse_time(&s, 0);
2480 if (manager_threshold == (ngx_msec_t) NGX_ERROR) {
2481 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2482 "invalid manager_threshold value \"%V\"", &value[i]);
24012483 return NGX_CONF_ERROR;
24022484 }
24032485
24242506 cache->loader_files = loader_files;
24252507 cache->loader_sleep = loader_sleep;
24262508 cache->loader_threshold = loader_threshold;
2509 cache->manager_files = manager_files;
2510 cache->manager_sleep = manager_sleep;
2511 cache->manager_threshold = manager_threshold;
24272512
24282513 if (ngx_add_path(cf, &cache->path) != NGX_OK) {
24292514 return NGX_CONF_ERROR;
11471147 static void
11481148 ngx_cache_manager_process_handler(ngx_event_t *ev)
11491149 {
1150 time_t next, n;
11511150 ngx_uint_t i;
1151 ngx_msec_t next, n;
11521152 ngx_path_t **path;
11531153
1154 next = 60 * 60;
1154 next = 60 * 60 * 1000;
11551155
11561156 path = ngx_cycle->paths.elts;
11571157 for (i = 0; i < ngx_cycle->paths.nelts; i++) {
11691169 next = 1;
11701170 }
11711171
1172 ngx_add_timer(ev, next * 1000);
1172 ngx_add_timer(ev, next);
11731173 }
11741174
11751175
920920 ngx_cache_manager_process_handler(void)
921921 {
922922 u_long ev;
923 time_t next, n;
924923 ngx_uint_t i;
924 ngx_msec_t next, n;
925925 ngx_path_t **path;
926926
927 next = 60 * 60;
927 next = 60 * 60 * 1000;
928928
929929 path = ngx_cycle->paths.elts;
930930 for (i = 0; i < ngx_cycle->paths.nelts; i++) {
942942 next = 1;
943943 }
944944
945 ev = WaitForSingleObject(ngx_cache_manager_event, (u_long) next * 1000);
945 ev = WaitForSingleObject(ngx_cache_manager_event, (u_long) next);
946946
947947 if (ev != WAIT_TIMEOUT) {
948948