Skip to content

Commit

Permalink
Allow setting up maximum number of file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Jun 1, 2024
1 parent 72430e5 commit 419c078
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ can be decided automatically, so some configuration options are provided.
| `proxy_protocol` | `bool` | `false` | Enables the [PROXY protocol](https://www.haproxy.com/blog/haproxy/proxy-protocol/). Versions 1 and 2 are supported. Only enable this setting if using Lwan behind a proxy, and the proxy supports this protocol; otherwise, this allows anybody to spoof origin IP addresses |
| `max_post_data_size` | `int` | `40960` | Sets the maximum number of data size for POST requests, in bytes |
| `max_put_data_size` | `int` | `40960` | Sets the maximum number of data size for PUT requests, in bytes |
| `max_file_descriptors` | `int` | `524288` | Maximum number of file descriptors. Needs to be at least 10x `threads` |
| `request_buffer_size` | `int` | `4096` | Request buffer size length. If larger than the default of `4096`, it'll be dynamically allocated. |
| `allow_temp_files` | `str` | `""` | Use temporary files; set to `post` for POST requests, `put` for PUT requests, or `all` (equivalent to setting to `post put`) for both.|
| `error_template` | `str` | Default error template | Template for error codes. See variables below. |
Expand Down
30 changes: 27 additions & 3 deletions src/lib/lwan.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static const struct lwan_config default_config = {
.allow_post_temp_file = false,
.max_put_data_size = 10 * DEFAULT_BUFFER_SIZE,
.allow_put_temp_file = false,
.max_file_descriptors = 524288,
};

LWAN_HANDLER_ROUTE(brew_coffee, NULL /* do not autodetect this route */)
Expand Down Expand Up @@ -663,6 +664,19 @@ static bool setup_from_config(struct lwan *lwan, const char *path)
}

lwan->config.request_buffer_size = (size_t)request_buffer_size;
} else if (streq(line->key, "max_file_descriptors")) {
long max_file_descriptors = parse_long(
line->value, (long)default_config.max_file_descriptors);

if (max_file_descriptors < 0) {
config_error(conf, "Maximum number of file descriptors can't be negative");
} else if (max_file_descriptors > 2000000l) {
config_error(conf, "2M file descriptors should be sufficient!");
} else if (max_file_descriptors == 0) {
max_file_descriptors = default_config.max_file_descriptors;
}

lwan->config.max_file_descriptors = (unsigned int)max_file_descriptors;
} else if (streq(line->key, "max_post_data_size")) {
long max_post_data_size = parse_long(
line->value, (long)default_config.max_post_data_size);
Expand Down Expand Up @@ -766,7 +780,7 @@ static void try_setup_from_config(struct lwan *l,
(l->config.ssl.send_hsts_header ? REQUEST_WANTS_HSTS_HEADER : 0);
}

static rlim_t setup_open_file_count_limits(void)
static rlim_t setup_open_file_count_limits(struct lwan *l)
{
struct rlimit r;

Expand All @@ -789,6 +803,9 @@ static rlim_t setup_open_file_count_limits(void)
goto out;
}

r.rlim_cur = LWAN_MIN(l->config.max_file_descriptors,
r.rlim_cur);

if (setrlimit(RLIMIT_NOFILE, &r) < 0) {
lwan_status_perror("Could not raise maximum number of file "
"descriptors to %" PRIu64 ". Leaving at "
Expand All @@ -798,7 +815,14 @@ static rlim_t setup_open_file_count_limits(void)
}

out:
return LWAN_MIN(655360ull, r.rlim_cur);
if (r.rlim_cur < 10 * l->thread.count) {
lwan_status_critical("Number of file descriptors (%ld) is smaller than 10x "
"the number of threads (%d)\n",
r.rlim_cur,
10 * l->thread.count);
}

return r.rlim_cur;
}

static void allocate_connections(struct lwan *l, size_t max_open_files)
Expand Down Expand Up @@ -901,7 +925,7 @@ void lwan_init_with_config(struct lwan *l, const struct lwan_config *config)
l->thread.count = l->config.n_threads;
}

rlim_t max_open_files = setup_open_file_count_limits();
rlim_t max_open_files = setup_open_file_count_limits(l);
allocate_connections(l, (size_t)max_open_files);

l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count;
Expand Down
1 change: 1 addition & 0 deletions src/lib/lwan.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ struct lwan_config {
unsigned int keep_alive_timeout;
unsigned int expires;
unsigned int n_threads;
unsigned int max_file_descriptors;

unsigned int quiet : 1;
unsigned int proxy_protocol : 1;
Expand Down

0 comments on commit 419c078

Please sign in to comment.