Don't accept connections if the connection queue is full.

This commit is contained in:
Jordan Bancino 2023-01-09 17:44:12 +00:00
parent 1d9ed5dcbf
commit 90a74c3b0a
2 changed files with 29 additions and 30 deletions

View file

@ -276,30 +276,6 @@ HttpSendHeaders(HttpServerContext * c)
fprintf(fp, "\n");
}
static int
QueueConnection(HttpServer * server, int fd)
{
FILE *fp;
int result;
if (!server)
{
return 0;
}
fp = fdopen(fd, "r+");
if (!fp)
{
return 0;
}
pthread_mutex_lock(&server->connQueueMutex);
result = QueuePush(server->connQueue, fp);
pthread_mutex_unlock(&server->connQueueMutex);
return result;
}
static FILE *
DequeueConnection(HttpServer * server)
{
@ -699,6 +675,7 @@ HttpServerEventThread(void *args)
int connFd;
int pollResult;
pollResult = poll(pollFds, 1, 500);
if (pollResult < 0)
@ -707,14 +684,32 @@ HttpServerEventThread(void *args)
continue;
}
pthread_mutex_lock(&server->connQueueMutex);
/* Don't even accept connections if the queue is full. */
if (!QueueFull(server->connQueue))
{
FILE *fp;
connFd = accept(server->sd, (struct sockaddr *) & addr, &addrLen);
if (connFd < 0)
{
pthread_mutex_unlock(&server->connQueueMutex);
continue;
}
QueueConnection(server, connFd);
fp = fdopen(connFd, "r+");
if (!fp)
{
pthread_mutex_unlock(&server->connQueueMutex);
close(connFd);
continue;
}
QueuePush(server->connQueue, fp);
}
pthread_mutex_unlock(&server->connQueueMutex);
}
for (i = 0; i < server->nThreads; i++)

View file

@ -108,7 +108,11 @@ recipe_build() {
done
if [ $do_rebuild -eq 1 ] || [ ! -f "../build/$PROG" ]; then
if [ -t 1 ]; then
printf "LD %s%*c\n" "$PROG" "16" " "
else
echo "LD $PROG"
fi
$CC $LDFLAGS -o "../build/$PROG" $objs
else
echo "Up to date."