refactor(config): add workers number to config
This commit is contained in:
@@ -4,6 +4,7 @@ host = "localhost"
|
||||
port = "3030"
|
||||
# Root folder in case there are no virtual hosts
|
||||
root = "www"
|
||||
workers = 6
|
||||
|
||||
[[virtual_hosts]]
|
||||
# "default" domain is required
|
||||
|
||||
@@ -19,6 +19,7 @@ typedef struct cws_config {
|
||||
char *host;
|
||||
char *port;
|
||||
char *root;
|
||||
int workers;
|
||||
cws_vhost_s *virtual_hosts;
|
||||
unsigned virtual_hosts_count;
|
||||
} cws_config_s;
|
||||
|
||||
@@ -13,17 +13,11 @@
|
||||
#define CWS_SERVER_BACKLOG 128
|
||||
|
||||
/* 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 */
|
||||
#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 */
|
||||
extern volatile sig_atomic_t cws_server_run;
|
||||
|
||||
|
||||
+4
-4
@@ -89,7 +89,7 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
server->workers = cws_worker_new(CWS_WORKERS_NUM, config);
|
||||
server->workers = cws_worker_new(config->workers, config);
|
||||
if (server->workers == NULL) {
|
||||
return CWS_WORKER_ERROR;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
size_t workers_index = 0;
|
||||
|
||||
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) {
|
||||
continue;
|
||||
@@ -122,7 +122,7 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
|
||||
cws_fd_set_nonblocking(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) {
|
||||
cws_worker_free(server->workers, CWS_WORKERS_NUM);
|
||||
cws_worker_free(server->workers, server->config->workers);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user