diff --git a/src/HttpServer.c b/src/HttpServer.c index 8dd0554..2e0c83b 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -39,6 +39,7 @@ struct HttpServer { int sd; unsigned int nThreads; + unsigned int maxConnections; pthread_t socketThread; volatile unsigned int stop:1; @@ -49,7 +50,7 @@ struct HttpServer }; HttpServer * -HttpServerCreate(unsigned short port, unsigned int nThreads, +HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxConnections, HttpHandler * requestHandler, void *handlerArgs) { HttpServer *server; @@ -85,8 +86,7 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, return NULL; } - /* TODO: Make this a user-tunable parameter? */ - if (listen(server->sd, 32) < 0) + if (listen(server->sd, maxConnections) < 0) { close(server->sd); free(server); @@ -94,6 +94,7 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, } server->nThreads = nThreads; + server->maxConnections = maxConnections; server->requestHandler = requestHandler; server->handlerArgs = handlerArgs; server->stop = 0; diff --git a/src/Telodendria.c b/src/Telodendria.c index fc0ffd2..8fc8e6c 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -297,7 +297,7 @@ main(int argc, char **argv) } /* Bind the socket before possibly dropping permissions */ - httpServer = HttpServerCreate(tConfig->listenPort, tConfig->threads, + httpServer = HttpServerCreate(tConfig->listenPort, tConfig->threads, tConfig->maxConnections, TelodendriaHttpHandler, NULL); if (!httpServer) { diff --git a/src/TelodendriaConfig.c b/src/TelodendriaConfig.c index abe03b0..f16fdea 100644 --- a/src/TelodendriaConfig.c +++ b/src/TelodendriaConfig.c @@ -153,6 +153,11 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc) if (IsInteger(ArrayGet(value, 0))) { tConfig->threads = atoi(ArrayGet(value, 0)); + if (!tConfig->threads) + { + Log(lc, LOG_ERROR, "threads must be greater than zero"); + goto error; + } } else { @@ -162,6 +167,34 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc) goto error; } + directive = (ConfigDirective *) HashMapGet(config, "max-connections"); + if (!directive) + { + Log(lc, LOG_WARNING, "max-connections not specified; using defaults, which may change"); + tConfig->maxConnections = 32; + } + else + { + ASSERT_NO_CHILDREN("max-connections"); + ASSERT_VALUES("max-connections", 1); + if (IsInteger(ArrayGet(value, 0))) + { + tConfig->maxConnections = atoi(ArrayGet(value, 0)); + if (!tConfig->maxConnections) + { + Log(lc, LOG_ERROR, "max-connections must be greater than zero."); + goto error; + } + } + else + { + Log(lc, LOG_ERROR, "Expected integer for max-connections, got '%s'", ArrayGet(value, 0)); + goto error; + } + } + + + GET_DIRECTIVE("federation"); ASSERT_NO_CHILDREN("federation"); ASSERT_VALUES("federation", 1); diff --git a/src/include/HttpServer.h b/src/include/HttpServer.h index 0e2c3f3..4d65e5b 100644 --- a/src/include/HttpServer.h +++ b/src/include/HttpServer.h @@ -31,7 +31,7 @@ typedef struct HttpServer HttpServer; typedef void (HttpHandler) (HttpRequest *, HttpResponse *, void *); extern HttpServer * - HttpServerCreate(unsigned short, unsigned int, HttpHandler *, void *); + HttpServerCreate(unsigned short, unsigned int, unsigned int, HttpHandler *, void *); extern void HttpServerFree(HttpServer *); diff --git a/src/include/TelodendriaConfig.h b/src/include/TelodendriaConfig.h index fd88a9a..3bca82c 100644 --- a/src/include/TelodendriaConfig.h +++ b/src/include/TelodendriaConfig.h @@ -61,6 +61,7 @@ typedef struct TelodendriaConfig unsigned short listenPort; unsigned int flags; unsigned int threads; + unsigned int maxConnections; char *logOut; char *logTimestamp;