From 808e87fe54e6b6c91fd0def5c11ae478e4356abb Mon Sep 17 00:00:00 2001 From: git-hulk Date: Thu, 12 Jan 2017 22:24:09 +0800 Subject: [PATCH 1/2] MOD: limit max connections --- README.md | 3 ++- src/fc.c | 20 ++++++++++++++++---- src/fc_core.h | 1 + src/fc_server.c | 6 ++++++ src/fc_stats.c | 1 + 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 603eafa..cec01af 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ To build fatcache from source with _debug logs enabled_ and _assertions disabled ## Help Usage: fatcache [-?hVdS] [-o output file] [-v verbosity level] - [-p port] [-a addr] [-e hash power] + [-p port] [-a addr] [-e hash power] [-c max connections] [-f factor] [-n min item chunk size] [-I slab size] [-i max index memory[ [-m max slab memory] [-z slab profile] [-D ssd device] [-s server id] @@ -105,6 +105,7 @@ To build fatcache from source with _debug logs enabled_ and _assertions disabled -I, --slab-size=N : set slab size in bytes (default: 1048576 bytes) -i, --max-index-memory=N : set the maximum memory to use for item indexes in MB (default: 64 MB) -m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: 64 MB) + -c, --max-connections=N : set max simultaneous connections (default: 10240) -z, --slab-profile=S : set the profile of slab item chunk sizes (default: n/a) -D, --ssd-device=S : set the path to the ssd device file (default: n/a) -s, --server-id=I/N : set fatcache instance to be I out of total N instances (default: 0/1) diff --git a/src/fc.c b/src/fc.c index 584144f..4cf6278 100644 --- a/src/fc.c +++ b/src/fc.c @@ -50,6 +50,7 @@ #define FC_INDEX_MEMORY (64 * MB) #define FC_SLAB_MEMORY (64 * MB) +#define FC_MAX_CONNS (10240) #define FC_SERVER_ID 0 #define FC_SERVER_N 1 @@ -97,6 +98,7 @@ static char short_options[] = "I:" /* slab size in MB */ "i:" /* max memory for item index in MB */ "m:" /* max memory for slab in MB */ + "c:" /* max connections */ "z:" /* profile of slab item sizes */ "D:" /* path to ssd device file */ "s:" /* server instance id */ @@ -107,7 +109,7 @@ fc_show_usage(void) { log_stderr( "Usage: fatcache [-?hVdS] [-o output file] [-v verbosity level]" CRLF - " [-p port] [-a addr] [-e hash power]" CRLF + " [-p port] [-a addr] [-e hash power] [-c max connections]" CRLF " [-f factor] [-n min item chunk size] [-I slab size]" CRLF " [-i max index memory[ [-m max slab memory]" CRLF " [-z slab profile] [-D ssd device] [-s server id]" CRLF @@ -138,13 +140,15 @@ fc_show_usage(void) " -n, --min-item-chunk-size=N : set the minimum item chunk size in bytes (default: %d bytes)" CRLF " -I, --slab-size=N : set slab size in bytes (default: %d bytes)" CRLF " -i, --max-index-memory=N : set the maximum memory to use for item indexes in MB (default: %d MB)" CRLF - " -m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: %d MB)" + " -m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: %d MB)" CRLF + " -c, --max-connections=N : set max simultaneous connections (default: %d)" "", FC_FACTOR, FC_CHUNK_SIZE, SLAB_SIZE, FC_INDEX_MEMORY / MB, - FC_SLAB_MEMORY / MB); + FC_SLAB_MEMORY / MB, + FC_MAX_CONNS); log_stderr( " -z, --slab-profile=S : set the profile of slab item chunk sizes (default: n/a)" CRLF " -D, --ssd-device=S : set the path to the ssd device file (default: n/a)" CRLF @@ -270,6 +274,7 @@ fc_set_default_options(void) settings.max_slab_memory = FC_SLAB_MEMORY; settings.chunk_size = FC_CHUNK_SIZE; settings.slab_size = FC_SLAB_SIZE; + settings.max_conns = FC_MAX_CONNS; memset(settings.profile, 0, sizeof(settings.profile)); settings.profile_last_id = SLABCLASS_MAX_ID; @@ -429,7 +434,14 @@ fc_get_options(int argc, char **argv) settings.max_slab_memory = (size_t)value * MB; break; - + case 'c': + value = fc_atoi(optarg, strlen(optarg)); + if (value < 0) { + log_stderr("fatcache: option -c should no less than zero"); + return FC_ERROR; + } + settings.max_conns = value; + break; case 'z': parse_profile = 1; profile_optarg = (uint8_t *)optarg; diff --git a/src/fc_core.h b/src/fc_core.h index c689b73..6ccf954 100644 --- a/src/fc_core.h +++ b/src/fc_core.h @@ -143,6 +143,7 @@ struct settings { char *ssd_device; /* path to ssd device file */ + int max_conns; /* maximum connection num */ uint32_t server_id; /* server id */ uint32_t server_n; /* # server */ }; diff --git a/src/fc_server.c b/src/fc_server.c index 84009bb..157fd3a 100644 --- a/src/fc_server.c +++ b/src/fc_server.c @@ -54,6 +54,12 @@ server_accept(struct context *ctx, struct conn *s) break; } + if (settings.max_conns > 0 && conn_nused() >= settings.max_conns) { + close(sd); + log_warn("server reached max connections"); + return FC_OK; + } + c = conn_get(sd, true); if (c == NULL) { log_error("get conn for c %d from s %d failed: %s", sd, s->sd, diff --git a/src/fc_stats.c b/src/fc_stats.c index e27d9b5..8e4da29 100644 --- a/src/fc_stats.c +++ b/src/fc_stats.c @@ -282,6 +282,7 @@ stats_settings(void) APPEND_STAT(stats_buf, "chunk_size", "%u", settings.chunk_size); APPEND_STAT(stats_buf, "max_chunk_size", "%u", settings.max_chunk_size); APPEND_STAT(stats_buf, "slab_size", "%u", settings.slab_size); + APPEND_STAT(stats_buf, "max_conns", "%d", settings.max_conns); APPEND_STAT(stats_buf, "ssd_device", "%s", settings.ssd_device); APPEND_STAT(stats_buf, "server_id", "%u", settings.server_id); APPEND_STAT(stats_buf, "server_count", "%u", settings.server_n); From 57365b777836f87892d62f0d81c91c3ffb92da9a Mon Sep 17 00:00:00 2001 From: git-hulk Date: Thu, 12 Jan 2017 22:24:28 +0800 Subject: [PATCH 2/2] MOD: listen fd shouldn't close while accept error, or server would be core. --- src/fc_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fc_core.c b/src/fc_core.c index 3bd714e..1a96ce9 100644 --- a/src/fc_core.c +++ b/src/fc_core.c @@ -155,7 +155,7 @@ core_core(struct context *ctx, struct conn *conn, uint32_t events) /* read takes precedence over write */ if (events & (EPOLLIN | EPOLLHUP)) { status = core_recv(ctx, conn); - if (status != FC_OK || conn->done || conn->err) { + if (conn->client && (status != FC_OK || conn->done || conn->err)) { core_close(ctx, conn); return; }