refactor(config): add workers number to config

This commit is contained in:
2026-03-11 16:33:15 +01:00
parent 15d6ae6829
commit 8f47a0b666
4 changed files with 7 additions and 11 deletions
+1
View File
@@ -4,6 +4,7 @@ host = "localhost"
port = "3030" port = "3030"
# Root folder in case there are no virtual hosts # Root folder in case there are no virtual hosts
root = "www" root = "www"
workers = 6
[[virtual_hosts]] [[virtual_hosts]]
# "default" domain is required # "default" domain is required
+1
View File
@@ -19,6 +19,7 @@ typedef struct cws_config {
char *host; char *host;
char *port; char *port;
char *root; char *root;
int workers;
cws_vhost_s *virtual_hosts; cws_vhost_s *virtual_hosts;
unsigned virtual_hosts_count; unsigned virtual_hosts_count;
} cws_config_s; } cws_config_s;
+1 -7
View File
@@ -13,17 +13,11 @@
#define CWS_SERVER_BACKLOG 128 #define CWS_SERVER_BACKLOG 128
/* Max number of epoll events processed per iteration */ /* Max number of epoll events processed per iteration */
#define CWS_SERVER_EPOLL_MAXEVENTS 64 #define CWS_SERVER_EPOLL_MAXEVENTS 128
/* Blocking timeout for epoll_wait in ms */ /* Blocking timeout for epoll_wait in ms */
#define CWS_SERVER_EPOLL_TIMEOUT 3000 #define CWS_SERVER_EPOLL_TIMEOUT 3000
/* Maximum allowed HTTP request size */
#define CWS_SERVER_MAX_REQUEST_SIZE (16 * 1024) /* 16KB */
/* Number of worker threads */
#define CWS_WORKERS_NUM 6
/* Global flag used to stop server */ /* Global flag used to stop server */
extern volatile sig_atomic_t cws_server_run; extern volatile sig_atomic_t cws_server_run;
+4 -4
View File
@@ -89,7 +89,7 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
return ret; return ret;
} }
server->workers = cws_worker_new(CWS_WORKERS_NUM, config); server->workers = cws_worker_new(config->workers, config);
if (server->workers == NULL) { if (server->workers == NULL) {
return CWS_WORKER_ERROR; return CWS_WORKER_ERROR;
} }
@@ -104,7 +104,7 @@ cws_return cws_server_start(cws_server_s *server) {
size_t workers_index = 0; size_t workers_index = 0;
while (cws_server_run) { while (cws_server_run) {
int nfds = epoll_wait(server->epfd, events, 128, -1); int nfds = epoll_wait(server->epfd, events, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
if (nfds < 0) { if (nfds < 0) {
continue; continue;
@@ -122,7 +122,7 @@ cws_return cws_server_start(cws_server_s *server) {
cws_fd_set_nonblocking(client_fd); cws_fd_set_nonblocking(client_fd);
cws_epoll_add(server->workers[workers_index]->epfd, client_fd); cws_epoll_add(server->workers[workers_index]->epfd, client_fd);
workers_index = (workers_index + 1) % CWS_WORKERS_NUM; workers_index = (workers_index + 1) % server->config->workers;
} }
} }
@@ -172,6 +172,6 @@ void cws_server_shutdown(cws_server_s *server) {
} }
if (server->workers) { if (server->workers) {
cws_worker_free(server->workers, CWS_WORKERS_NUM); cws_worker_free(server->workers, server->config->workers);
} }
} }