This commit is contained in:
Jordan Bancino 2022-08-21 12:35:16 -04:00
parent 30d055d570
commit 315589cd1b
5 changed files with 40 additions and 5 deletions

View file

@ -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;

View file

@ -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)
{

View file

@ -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);

View file

@ -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 *);

View file

@ -61,6 +61,7 @@ typedef struct TelodendriaConfig
unsigned short listenPort;
unsigned int flags;
unsigned int threads;
unsigned int maxConnections;
char *logOut;
char *logTimestamp;