From 620d613d1a98f7017495802b9b9d62b1b76c4fa9 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Sun, 26 Oct 2014 16:12:31 +1100 Subject: [PATCH] Add a maxclients directive that stops accepting clients when there are too many --- src/ckpool.c | 1 + src/ckpool.h | 2 ++ src/connector.c | 12 +++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 2cade1eb..688d9731 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -997,6 +997,7 @@ static void parse_config(ckpool_t *ckp) json_get_int64(&ckp->startdiff, json_conf, "startdiff"); json_get_int64(&ckp->maxdiff, json_conf, "maxdiff"); json_get_string(&ckp->logdir, json_conf, "logdir"); + json_get_int(&ckp->maxclients, json_conf, "maxclients"); arr_val = json_object_get(json_conf, "proxy"); if (arr_val && json_is_array(arr_val)) { int arr_size = json_array_size(arr_val); diff --git a/src/ckpool.h b/src/ckpool.h index b1ce196f..9284165d 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -113,6 +113,8 @@ struct ckpool_instance { int oldconnfd; /* Should we inherit a running instance's socket and shut it down */ bool handover; + /* How many clients maximum to accept before rejecting further */ + int maxclients; /* Logger message queue NOTE: Unique per process */ ckmsgq_t *logger; diff --git a/src/connector.c b/src/connector.c index 5be43465..6038506f 100644 --- a/src/connector.c +++ b/src/connector.c @@ -90,8 +90,18 @@ static pthread_cond_t sender_cond; static int accept_client(conn_instance_t *ci) { client_instance_t *client, *old_client; + ckpool_t *ckp = ci->pi->ckp; + int fd, port, no_clients; socklen_t address_len; - int fd, port; + + ck_rlock(&ci->lock); + no_clients = HASH_COUNT(clients); + ck_runlock(&ci->lock); + + if (ckp->maxclients && no_clients >= ckp->maxclients) { + LOGWARNING("Server full with %d clients", no_clients); + return 0; + } client = ckzalloc(sizeof(client_instance_t)); address_len = sizeof(client->address);