2 Commits

3 changed files with 50 additions and 18 deletions
+17 -7
View File
@@ -21,7 +21,8 @@
#include <unistd.h> #include <unistd.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#define ASYNC_MAX_EVENTS 16 #define ASYNC_QUEUE_MAX_EVENTS 256
#define ASYNC_EPOLL_MAX_EVENTS 64
typedef struct { typedef struct {
int fd; int fd;
@@ -36,7 +37,7 @@ typedef struct {
typedef struct { typedef struct {
int n; int n;
evt_listen_t *q[ASYNC_MAX_EVENTS]; evt_listen_t *q[ASYNC_QUEUE_MAX_EVENTS];
} listen_queue_t; } listen_queue_t;
static volatile listen_queue_t listen1, listen2, *listen_q = &listen1; static volatile listen_queue_t listen1, listen2, *listen_q = &listen1;
@@ -123,7 +124,13 @@ static int async_add_to_queue(evt_listen_t *evt) {
} }
} }
evt_listen_t *ptr = malloc(sizeof(evt_listen_t)); if (listen_q->n >= ASYNC_QUEUE_MAX_EVENTS) {
sem_post(&lock);
errno = ENOBUFS;
return -1;
}
evt_listen_t *ptr = malloc(sizeof(*evt));
if (ptr == NULL) { if (ptr == NULL) {
sem_post(&lock); sem_post(&lock);
return -1; return -1;
@@ -198,9 +205,12 @@ static int async_add(evt_listen_t *evt) {
if (async_check(evt) == 1) if (async_check(evt) == 1)
return 0; return 0;
int ret = async_add_to_queue(evt); int ret;
if (ret == 0 && thread != -1) if ((ret = async_add_to_queue(evt)) != 0) {
alert("Unable to add event to async queue");
} else if (thread != -1) {
pthread_kill(thread, SIGUSR1); pthread_kill(thread, SIGUSR1);
}
return ret; return ret;
} }
@@ -257,7 +267,7 @@ void async_free(void) {
} }
void async_thread(void) { void async_thread(void) {
struct epoll_event ev, events[ASYNC_MAX_EVENTS]; struct epoll_event ev, events[ASYNC_EPOLL_MAX_EVENTS];
int num_fds, idx; int num_fds, idx;
long ts, min_ts, cur_ts; long ts, min_ts, cur_ts;
volatile listen_queue_t *l; volatile listen_queue_t *l;
@@ -343,7 +353,7 @@ void async_thread(void) {
} }
// epoll is used in level-triggered mode, so buffers are taken into account // epoll is used in level-triggered mode, so buffers are taken into account
if ((num_fds = epoll_wait(epoll_fd, events, ASYNC_MAX_EVENTS, (int) (min_ts / 1000))) == -1) { if ((num_fds = epoll_wait(epoll_fd, events, ASYNC_EPOLL_MAX_EVENTS, (int) (min_ts / 1000))) == -1) {
if (errno == EINTR) { if (errno == EINTR) {
// interrupt // interrupt
errno = 0; errno = 0;
+13 -1
View File
@@ -411,7 +411,19 @@ int main(int argc, char *const argv[]) {
logger_set_name("main"); logger_set_name("main");
workers_init(); if (workers_init() != 0) {
critical("Unable to initialize workers");
ssl_free();
list_free(clients);
sem_destroy(&sem_clients_lock);
geoip_free();
proxy_unload();
cache_join();
async_free();
logger_stop();
logger_join();
return 1;
}
for (int i = 0; i < NUM_SOCKETS; i++) { for (int i = 0; i < NUM_SOCKETS; i++) {
async_fd(sockets[i], ASYNC_WAIT_READ, ASYNC_KEEP, &sockets[i], accept_cb, accept_err_cb, accept_err_cb); async_fd(sockets[i], ASYNC_WAIT_READ, ASYNC_KEEP, &sockets[i], accept_cb, accept_err_cb, accept_err_cb);
+20 -10
View File
@@ -16,16 +16,26 @@ static mpmc_t tcp_acceptor_ctx, request_handler_ctx, local_handler_ctx, fastcgi_
proxy_peer_handler_ctx, ws_frame_handler_ctx, chunk_handler_ctx, fastcgi_frame_handler_ctx; proxy_peer_handler_ctx, ws_frame_handler_ctx, chunk_handler_ctx, fastcgi_frame_handler_ctx;
int workers_init(void) { int workers_init(void) {
mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp"); int ret;
mpmc_init(&request_handler_ctx, 8, 64, (void (*)(void *)) request_handler_func, "req"); if ((ret = mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp")) != 0)
mpmc_init(&local_handler_ctx, 8, 64, (void (*)(void *)) local_handler_func, "local"); return ret;
mpmc_init(&fastcgi_handler_ctx, 8, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi"); if ((ret = mpmc_init(&request_handler_ctx, 8, 64, (void (*)(void *)) request_handler_func, "req")) != 0)
mpmc_init(&proxy_handler_ctx, 8, 64, (void (*)(void *)) proxy_handler_func, "proxy"); return ret;
mpmc_init(&proxy_peer_handler_ctx, 1, 8, (void (*)(void *)) proxy_peer_handler_func, "prxy_p"); if ((ret = mpmc_init(&local_handler_ctx, 8, 64, (void (*)(void *)) local_handler_func, "local")) != 0)
mpmc_init(&ws_frame_handler_ctx, 8, 64, (void (*)(void *)) ws_frame_handler_func, "ws"); return ret;
mpmc_init(&chunk_handler_ctx, 8, 64, (void (*)(void *)) chunk_handler_func, "chunk"); if ((ret = mpmc_init(&fastcgi_handler_ctx, 8, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi")) != 0)
mpmc_init(&fastcgi_frame_handler_ctx, 8, 64, (void (*)(void *)) fastcgi_frame_handler_func, "fcgi_f"); return ret;
return -1; if ((ret = mpmc_init(&proxy_handler_ctx, 8, 64, (void (*)(void *)) proxy_handler_func, "proxy")) != 0)
return ret;
if ((ret = mpmc_init(&proxy_peer_handler_ctx, 1, 8, (void (*)(void *)) proxy_peer_handler_func, "prxy_p")) != 0)
return ret;
if ((ret = mpmc_init(&ws_frame_handler_ctx, 8, 64, (void (*)(void *)) ws_frame_handler_func, "ws")) != 0)
return ret;
if ((ret = mpmc_init(&chunk_handler_ctx, 8, 64, (void (*)(void *)) chunk_handler_func, "chunk")) != 0)
return ret;
if ((ret = mpmc_init(&fastcgi_frame_handler_ctx, 8, 64, (void (*)(void *)) fastcgi_frame_handler_func, "fcgi_f")) != 0)
return ret;
return 0;
} }
void workers_stop(void) { void workers_stop(void) {