Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ulimit to Set Queue Size #53

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@

#include "main.h"

enum {
MAXCONN = 1000
};

enum Break {
BREAK,
NOBREAK,
Expand Down Expand Up @@ -63,7 +59,7 @@ struct Client_conn {
};

/* Globals */
static struct Client_conn client_cs[MAXCONN];
static struct Client_conn *client_cs;
static int nconnections;
static char *path;
static int max_descriptors;
Expand Down Expand Up @@ -131,12 +127,16 @@ static void install_sigterm_handler() {

static int get_max_descriptors() {
const int MARGIN = 5; /* stdin, stderr, listen socket, and whatever */
int max;
int max = 1000; /* initial value used only if getrlimit fails to return a value */
struct rlimit rlim;
int res;
const char *str;

max = MAXCONN;
res = getrlimit(RLIMIT_NOFILE, &rlim);
if (res != 0)
warning("getrlimit for open files");
else
max = rlim.rlim_cur - MARGIN;

str = getenv("TS_MAXCONN");
if (str != NULL) {
Expand All @@ -149,17 +149,6 @@ static int get_max_descriptors() {
if (max > FD_SETSIZE)
max = FD_SETSIZE - MARGIN;

/* I'd like to use OPEN_MAX or NR_OPEN, but I don't know if any
* of them is POSIX compliant */

res = getrlimit(RLIMIT_NOFILE, &rlim);
if (res != 0)
warning("getrlimit for open files");
else {
if (max > rlim.rlim_cur)
max = rlim.rlim_cur - MARGIN;
}

if (max < 1)
error("Too few opened descriptors available");

Expand All @@ -174,6 +163,9 @@ void server_main(int notify_fd, char *_path) {

process_type = SERVER;
max_descriptors = get_max_descriptors();

/* allocate dynamic memory for client_cs, thus removing any arbitrary limit */
client_cs = malloc(max_descriptors * sizeof(struct Client_conn));

/* Arbitrary limit, that will block the enqueuing, but should allow space
* for usual ts queries */
Expand Down Expand Up @@ -316,6 +308,7 @@ static void end_server(int ls) {
/* This comes from the parent, in the fork after server_main.
* This is the last use of path in this process.*/
free(path);
free(client_cs);
free(logdir);
#ifndef CPU
cleanupGpu();
Expand Down
Loading