diff options
author | dormando <dormando@rydia.net> | 2020-02-25 18:09:05 -0800 |
---|---|---|
committer | dormando <dormando@rydia.net> | 2020-02-26 00:16:50 -0800 |
commit | 05721e4b6c393f25830021bb13f6637e5747dfcc (patch) | |
tree | 533ac96420a55842798070ee225a4ca32d39febc /cache.c | |
parent | 379e3df46dd2f08fb12f577bba70a9d7422b6f7c (diff) | |
download | memcached-05721e4b6c393f25830021bb13f6637e5747dfcc.tar.gz |
add separate limits for connection buffers
allows specifying a megabyte limit for either response objects or read
buffers. this is split among all of the worker threads.
useful if connection limit is extremely high and you want to
aggressively close connections if something
happens and all connections become active at the same time.
missing runtime tuning.
Diffstat (limited to 'cache.c')
-rw-r--r-- | cache.c | 19 |
1 files changed, 17 insertions, 2 deletions
@@ -45,6 +45,12 @@ cache_t* cache_create(const char *name, size_t bufsize, size_t align, return ret; } +void cache_set_limit(cache_t *cache, int limit) { + pthread_mutex_lock(&cache->mutex); + cache->limit = limit; + pthread_mutex_unlock(&cache->mutex); +} + static inline void* get_object(void *ptr) { #ifndef NDEBUG uint64_t *pre = ptr; @@ -82,7 +88,7 @@ void* do_cache_alloc(cache_t *cache) { if (cache->freecurr > 0) { ret = cache->ptr[--cache->freecurr]; object = get_object(ret); - } else { + } else if (cache->limit == 0 || cache->total < cache->limit) { object = ret = malloc(cache->bufsize); if (ret != NULL) { object = get_object(ret); @@ -94,6 +100,8 @@ void* do_cache_alloc(cache_t *cache) { } cache->total++; } + } else { + object = NULL; } #ifndef NDEBUG @@ -134,7 +142,14 @@ void do_cache_free(cache_t *cache, void *ptr) { } ptr = pre; #endif - if (cache->freecurr < cache->freetotal) { + if (cache->limit > cache->total) { + /* Allow freeing in case the limit was revised downward */ + if (cache->destructor) { + cache->destructor(ptr, NULL); + } + free(ptr); + cache->total--; + } else if (cache->freecurr < cache->freetotal) { cache->ptr[cache->freecurr++] = ptr; } else { /* try to enlarge free connections array */ |