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

Make maximum number of threads used by web server configurable #2147

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/api/docs/content/specs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ components:
type: string
port:
type: string
threads:
type: integer
session:
type: object
properties:
Expand Down Expand Up @@ -739,6 +741,7 @@ components:
domain: pi.hole
acl: "+0.0.0.0/0,::/0"
port: 80,[::]:80
threads: 0
session:
timeout: 300
restore: true
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,12 @@ static void initConfig(struct config *conf)
conf->webserver.port.d.s = (char*)"80,[::]:80,443s,[::]:443s";
conf->webserver.port.c = validate_stub; // Type-based checking + civetweb syntax checking

conf->webserver.threads.k = "webserver.threads";
conf->webserver.threads.h = "Maximum number of worker threads allowed.\n The Pi-hole web server handles each incoming connection in a separate thread. Therefore, the value of this option is effectively the number of concurrent HTTP connections that can be handled. Any other connections are queued until they can be processed by a unoccupied thread.\n The default value of 0 means that the number of threads is automatically determined by the number of online CPU cores minus 1 (e.g., launching up to 8-1 = 7 threads on 8 cores). Any other value specifies the number of threads explicitly. A hard-coded maximum of 64 threads is enforced for this option.\n The total number of threads you see may be lower than the configured value as threads are only created when needed due to incoming connections.";
conf->webserver.threads.t = CONF_UINT;
conf->webserver.threads.d.ui = 0;
conf->webserver.threads.c = validate_stub; // Only type-based checking

conf->webserver.tls.cert.k = "webserver.tls.cert";
conf->webserver.tls.cert.h = "Path to the TLS (SSL) certificate file. All directories along the path must be readable and accessible by the user running FTL (typically 'pihole'). This option is only required when at least one of webserver.port is TLS. The file must be in PEM format, and it must have both, private key and certificate (the *.pem file created must contain a 'CERTIFICATE' section as well as a 'RSA PRIVATE KEY' section).\n The *.pem file can be created using\n cp server.crt server.pem\n cat server.key >> server.pem\n if you have these files instead";
conf->webserver.tls.cert.a = cJSON_CreateStringReference("<valid TLS certificate file (*.pem)>");
Expand Down
1 change: 1 addition & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ struct config {
struct conf_item domain;
struct conf_item acl;
struct conf_item port;
struct conf_item threads;
struct {
struct conf_item timeout;
struct conf_item restore;
Expand Down
31 changes: 22 additions & 9 deletions src/webserver/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,28 @@ void http_init(void)
return;
}

char num_threads[3] = { 0 };
// Calculate number of threads for the web server
// any positive number = number of threads (limited to at most MAX_WEBTHREADS)
// 0 = the number of online processors (at least 1, no more than 16)
// For the automatic option, we use the number of available (= online)
// cores which may be less than the total number of cores in the system,
// e.g., if a virtualization environment is used and fewer cores are
// assigned to the VM than are available on the host.
sprintf(num_threads, "%d", get_nprocs() > 8 ? 16 : 2*get_nprocs());

if(config.webserver.threads.v.ui > 0)
{
const unsigned int threads = LIMIT_MIN_MAX(config.webserver.threads.v.ui, 1, MAX_WEBTHREADS);
snprintf(num_threads, sizeof(num_threads), "%u", threads);
}
else // Automatic thread calculation
{
const int nprocs = get_nprocs();
const unsigned int threads = LIMIT_MIN_MAX(nprocs - 1, 1, 16);
snprintf(num_threads, sizeof(num_threads), "%u", threads);
}

/* Initialize the library */
log_web("Initializing HTTP server on ports \"%s\"", config.webserver.port.v.s);
unsigned int features = MG_FEATURES_FILES |
Expand Down Expand Up @@ -397,15 +419,6 @@ void http_init(void)
// A referrer will be sent for same-site origins, but cross-origin requests will
// send no referrer information.
// The latter four headers are set as expected by https://securityheaders.io
char num_threads[3] = { 0 };
// Use 16 threads if more than 8 cores are available, otherwise use
// 2*cores. This is to prevent overloading the system with too many
// threads.
// We use the number of available (= online) cores which may be less
// than the total number of cores in the system, e.g., if a
// virtualization environment is used and fewer cores are assigned to
// the VM than are available on the host.
sprintf(num_threads, "%d", get_nprocs() > 8 ? 16 : 2*get_nprocs());
const char *options[] = {
"document_root", config.webserver.paths.webroot.v.s,
"error_pages", error_pages,
Expand Down
5 changes: 5 additions & 0 deletions src/webserver/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <stdbool.h>

// Hard-coded maximum number of allowed web server threads
#define MAX_WEBTHREADS 64
// Macro to limiting a numeric value to a certain minimum and maximum
#define LIMIT_MIN_MAX(a, b, c) ((a) < (b) ? (b) : (a) > (c) ? (c) : (a))

void http_init(void);
void http_terminate(void);

Expand Down
19 changes: 16 additions & 3 deletions test/pihole.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Pi-hole configuration file (v5.25.2-1921-gd3948088-dirty)
# Pi-hole configuration file (v5.25.2-2439-g93521615-dirty)
# Encoding: UTF-8
# This file is managed by pihole-FTL
# Last updated on 2024-06-15 09:10:13 UTC
# Last updated on 2025-01-01 12:33:13 UTC

[dns]
# Array of upstream DNS servers used by Pi-hole
Expand Down Expand Up @@ -659,6 +659,19 @@
# comma-separated list of <[ip_address:]port>
port = "80,[::]:80,443s,[::]:443s"

# Maximum number of worker threads allowed.
# The Pi-hole web server handles each incoming connection in a separate thread.
# Therefore, the value of this option is effectively the number of concurrent HTTP
# connections that can be handled. Any other connections are queued until they can be
# processed by a unoccupied thread.
# The default value of 0 means that the number of threads is automatically determined
# by the number of online CPU cores minus 1 (e.g., launching up to 8-1 = 7 threads on
# 8 cores). Any other value specifies the number of threads explicitly. A hard-coded
# maximum of 64 threads is enforced for this option.
# The total number of threads you see may be lower than the configured value as
# threads are only created when needed due to incoming connections.
threads = 0

[webserver.session]
# Session timeout in seconds. If a session is inactive for more than this time, it will
# be terminated. Sessions are continuously refreshed by the web interface, preventing
Expand Down Expand Up @@ -1124,7 +1137,7 @@
all = true ### CHANGED, default = false

# Configuration statistics:
# 150 total entries out of which 94 entries are default
# 151 total entries out of which 95 entries are default
# --> 56 entries are modified
# 2 entries are forced through environment:
# - misc.nice
Expand Down
Loading