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"); 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 * static FILE *
DequeueConnection(HttpServer * server) DequeueConnection(HttpServer * server)
{ {
@ -699,6 +675,7 @@ HttpServerEventThread(void *args)
int connFd; int connFd;
int pollResult; int pollResult;
pollResult = poll(pollFds, 1, 500); pollResult = poll(pollFds, 1, 500);
if (pollResult < 0) if (pollResult < 0)
@ -707,14 +684,32 @@ HttpServerEventThread(void *args)
continue; continue;
} }
connFd = accept(server->sd, (struct sockaddr *) & addr, &addrLen); pthread_mutex_lock(&server->connQueueMutex);
if (connFd < 0) /* Don't even accept connections if the queue is full. */
if (!QueueFull(server->connQueue))
{ {
continue; FILE *fp;
}
QueueConnection(server, connFd); connFd = accept(server->sd, (struct sockaddr *) & addr, &addrLen);
if (connFd < 0)
{
pthread_mutex_unlock(&server->connQueueMutex);
continue;
}
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++) for (i = 0; i < server->nThreads; i++)

View file

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