Add workers
This commit is contained in:
5
Makefile
5
Makefile
@ -45,8 +45,9 @@ bin/lib/%.o: src/lib/%.c
|
||||
bin/worker/%.o: src/worker/%.c
|
||||
$(CC) -c -o $@ $(CFLAGS) $<
|
||||
|
||||
bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o \
|
||||
bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o bin/workers.o \
|
||||
bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o bin/worker/responder.o \
|
||||
bin/worker/fastcgi_handler.o \
|
||||
bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
||||
bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \
|
||||
bin/lib/utils.o bin/lib/websocket.o bin/lib/mpmc.o
|
||||
@ -62,6 +63,8 @@ bin/cache_handler.o: src/cache_handler.h src/lib/utils.h src/lib/uri.h src/lib/c
|
||||
|
||||
bin/async.o: src/async.h src/logger.h
|
||||
|
||||
bin/workers.o: src/workers.h src/lib/mpmc.h src/worker/*.h
|
||||
|
||||
bin/worker/request_handler.o: src/worker/request_handler.h
|
||||
|
||||
bin/worker/tcp_acceptor.o: src/worker/tcp_acceptor.h
|
||||
|
22
src/server.c
22
src/server.c
@ -10,15 +10,12 @@
|
||||
#include "server.h"
|
||||
#include "logger.h"
|
||||
#include "async.h"
|
||||
#include "worker/tcp_acceptor.h"
|
||||
|
||||
#include "cache_handler.h"
|
||||
#include "lib/config.h"
|
||||
#include "lib/proxy.h"
|
||||
#include "lib/geoip.h"
|
||||
#include "worker/tcp_closer.h"
|
||||
#include "worker/request_handler.h"
|
||||
#include "worker/responder.h"
|
||||
#include "workers.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
@ -27,7 +24,6 @@
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <openssl/err.h>
|
||||
@ -111,15 +107,8 @@ static void terminate_gracefully(int sig) {
|
||||
signal(SIGINT, terminate_forcefully);
|
||||
signal(SIGTERM, terminate_forcefully);
|
||||
|
||||
tcp_acceptor_stop();
|
||||
request_handler_stop();
|
||||
tcp_closer_stop();
|
||||
responder_stop();
|
||||
|
||||
tcp_acceptor_destroy();
|
||||
request_handler_destroy();
|
||||
tcp_closer_destroy();
|
||||
responder_destroy();
|
||||
workers_stop();
|
||||
workers_destroy();
|
||||
|
||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||
close(sockets[i]);
|
||||
@ -264,10 +253,7 @@ int main(int argc, char *const argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
tcp_acceptor_init(CNX_HANDLER_WORKERS, 64);
|
||||
tcp_closer_init(CNX_HANDLER_WORKERS, 64);
|
||||
request_handler_init(REQ_HANDLER_WORKERS, 64);
|
||||
responder_init(REQ_HANDLER_WORKERS, 64);
|
||||
workers_init();
|
||||
|
||||
for (int i = 0; i < NUM_SOCKETS; i++) {
|
||||
async(sockets[i], POLLIN, ASYNC_KEEP, accept_cb, &sockets[i], accept_err_cb, &sockets[i]);
|
||||
|
@ -8,30 +8,9 @@
|
||||
|
||||
#include "fastcgi_handler.h"
|
||||
#include "../logger.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "../lib/utils.h"
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void fastcgi_handler_func(client_ctx_t *ctx);
|
||||
|
||||
int fastcgi_handler_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) fastcgi_handler_func, "fcgi");
|
||||
}
|
||||
|
||||
int fastcgi_handle(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void fastcgi_handler_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void fastcgi_handler_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void fastcgi_handler_func(client_ctx_t *ctx) {
|
||||
void fastcgi_handler_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
// TODO
|
||||
}
|
||||
|
@ -11,12 +11,6 @@
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int fastcgi_handler_init(int n_workers, int buf_size);
|
||||
|
||||
int fastcgi_handle(client_ctx_t *ctx);
|
||||
|
||||
void fastcgi_handler_stop(void);
|
||||
|
||||
void fastcgi_handler_destroy(void);
|
||||
void fastcgi_handler_func(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_FASTCGI_HANDLER_H
|
||||
|
@ -8,52 +8,23 @@
|
||||
|
||||
#include "../defs.h"
|
||||
#include "request_handler.h"
|
||||
#include "../workers.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "tcp_closer.h"
|
||||
#include "../async.h"
|
||||
|
||||
#include "../server.h"
|
||||
#include "../logger.h"
|
||||
|
||||
#include "../lib/utils.h"
|
||||
#include "../lib/config.h"
|
||||
#include "../lib/sock.h"
|
||||
#include "../lib/http.h"
|
||||
#include "../lib/proxy.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
#include "../cache_handler.h"
|
||||
#include "../lib/compress.h"
|
||||
#include "../lib/websocket.h"
|
||||
#include "responder.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/err.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void request_handler_func(client_ctx_t *ctx);
|
||||
static int request_handler(client_ctx_t *ctx);
|
||||
|
||||
int request_handler_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) request_handler_func, "req");
|
||||
}
|
||||
|
||||
int handle_request(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void request_handler_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void request_handler_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void request_handler_func(client_ctx_t *ctx) {
|
||||
void request_handler_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
|
||||
|
||||
if (request_handler(ctx) == 0) {
|
||||
|
@ -11,12 +11,6 @@
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int request_handler_init(int n_workers, int buf_size);
|
||||
|
||||
int handle_request(client_ctx_t *ctx);
|
||||
|
||||
void request_handler_stop(void);
|
||||
|
||||
void request_handler_destroy(void);
|
||||
void request_handler_func(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_REQUEST_HANDLER_H
|
||||
|
@ -8,48 +8,23 @@
|
||||
|
||||
#include "../defs.h"
|
||||
#include "responder.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "tcp_closer.h"
|
||||
#include "../async.h"
|
||||
#include "../logger.h"
|
||||
|
||||
#include "../lib/utils.h"
|
||||
#include "../lib/config.h"
|
||||
#include "../lib/sock.h"
|
||||
#include "../lib/http.h"
|
||||
#include "../lib/proxy.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
#include "../lib/compress.h"
|
||||
#include "../lib/websocket.h"
|
||||
#include "request_handler.h"
|
||||
#include "../workers.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/err.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void responder_func(client_ctx_t *ctx);
|
||||
static void responder(client_ctx_t *ctx);
|
||||
|
||||
int responder_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) responder_func, "res");
|
||||
}
|
||||
|
||||
int respond(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void responder_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void responder_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void responder_func(client_ctx_t *ctx) {
|
||||
void responder_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
responder(ctx);
|
||||
|
||||
|
@ -11,12 +11,6 @@
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int responder_init(int n_workers, int buf_size);
|
||||
|
||||
int respond(client_ctx_t *ctx);
|
||||
|
||||
void responder_stop(void);
|
||||
|
||||
void responder_destroy(void);
|
||||
void responder_func(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_RESPONDER_H
|
||||
|
@ -6,43 +6,20 @@
|
||||
* @date 2022-12-28
|
||||
*/
|
||||
|
||||
#include "../server.h"
|
||||
#include "tcp_acceptor.h"
|
||||
#include "../async.h"
|
||||
#include "../logger.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "../lib/utils.h"
|
||||
#include "../lib/geoip.h"
|
||||
#include "../lib/config.h"
|
||||
#include "tcp_closer.h"
|
||||
#include "request_handler.h"
|
||||
#include "tcp_acceptor.h"
|
||||
#include "../workers.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void tcp_acceptor_func(client_ctx_t *ctx);
|
||||
|
||||
int tcp_acceptor_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) tcp_acceptor_func, "tcp_a");
|
||||
}
|
||||
|
||||
int tcp_accept(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void tcp_acceptor_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void tcp_acceptor_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void tcp_acceptor_func(client_ctx_t *ctx) {
|
||||
void tcp_acceptor_func(client_ctx_t *ctx) {
|
||||
struct sockaddr_in6 server_addr;
|
||||
|
||||
inet_ntop(ctx->socket.addr.ipv6.sin6_family, &ctx->socket.addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
|
||||
|
@ -11,12 +11,6 @@
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int tcp_acceptor_init(int n_workers, int buf_size);
|
||||
|
||||
int tcp_accept(client_ctx_t *ctx);
|
||||
|
||||
void tcp_acceptor_stop(void);
|
||||
|
||||
void tcp_acceptor_destroy(void);
|
||||
void tcp_acceptor_func(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_TCP_ACCEPTOR_H
|
||||
|
@ -8,32 +8,11 @@
|
||||
|
||||
#include "tcp_closer.h"
|
||||
#include "../logger.h"
|
||||
#include "../lib/mpmc.h"
|
||||
#include "../lib/utils.h"
|
||||
|
||||
#include <memory.h>
|
||||
|
||||
static mpmc_t mpmc_ctx;
|
||||
|
||||
static void tcp_closer_func(client_ctx_t *ctx);
|
||||
|
||||
int tcp_closer_init(int n_workers, int buf_size) {
|
||||
return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) tcp_closer_func, "tcp_c");
|
||||
}
|
||||
|
||||
int tcp_close(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&mpmc_ctx, ctx);
|
||||
}
|
||||
|
||||
void tcp_closer_stop(void) {
|
||||
mpmc_stop(&mpmc_ctx);
|
||||
}
|
||||
|
||||
void tcp_closer_destroy(void) {
|
||||
mpmc_destroy(&mpmc_ctx);
|
||||
}
|
||||
|
||||
static void tcp_closer_func(client_ctx_t *ctx) {
|
||||
void tcp_closer_func(client_ctx_t *ctx) {
|
||||
logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix);
|
||||
|
||||
sock_close(&ctx->socket);
|
||||
|
@ -11,12 +11,6 @@
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
int tcp_closer_init(int n_workers, int buf_size);
|
||||
|
||||
int tcp_close(client_ctx_t *ctx);
|
||||
|
||||
void tcp_closer_stop(void);
|
||||
|
||||
void tcp_closer_destroy(void);
|
||||
void tcp_closer_func(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_TCP_CLOSER_H
|
||||
|
62
src/workers.c
Normal file
62
src/workers.c
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* sesimos - secure, simple, modern web server
|
||||
* @brief Worker interface
|
||||
* @file src/workers.c
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2022-12-29
|
||||
*/
|
||||
|
||||
#include "workers.h"
|
||||
#include "lib/mpmc.h"
|
||||
|
||||
#include "worker/tcp_acceptor.h"
|
||||
#include "worker/tcp_closer.h"
|
||||
#include "worker/responder.h"
|
||||
#include "worker/request_handler.h"
|
||||
#include "worker/fastcgi_handler.h"
|
||||
|
||||
static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx, responder_ctx, fastcgi_handler_cxt;
|
||||
|
||||
int workers_init(void) {
|
||||
mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp_a");
|
||||
mpmc_init(&tcp_closer_ctx, 8, 64, (void (*)(void *)) tcp_closer_func, "tcp_c");
|
||||
mpmc_init(&request_handler_ctx, 16, 64, (void (*)(void *)) request_handler_func, "req");
|
||||
mpmc_init(&responder_ctx, 16, 64, (void (*)(void *)) responder_func, "res");
|
||||
mpmc_init(&fastcgi_handler_cxt, 16, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi");
|
||||
return -1;
|
||||
}
|
||||
|
||||
void workers_stop(void) {
|
||||
mpmc_stop(&tcp_acceptor_ctx);
|
||||
mpmc_stop(&request_handler_ctx);
|
||||
mpmc_stop(&responder_ctx);
|
||||
mpmc_stop(&tcp_closer_ctx);
|
||||
}
|
||||
|
||||
void workers_destroy(void) {
|
||||
mpmc_destroy(&tcp_acceptor_ctx);
|
||||
mpmc_destroy(&request_handler_ctx);
|
||||
mpmc_destroy(&responder_ctx);
|
||||
mpmc_destroy(&tcp_closer_ctx);
|
||||
}
|
||||
|
||||
int tcp_accept(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&tcp_acceptor_ctx, ctx);
|
||||
}
|
||||
|
||||
int tcp_close(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&tcp_closer_ctx, ctx);
|
||||
}
|
||||
|
||||
int handle_request(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&request_handler_ctx, ctx);
|
||||
}
|
||||
|
||||
int respond(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&responder_ctx, ctx);
|
||||
}
|
||||
|
||||
int fastcgi_handle(client_ctx_t *ctx) {
|
||||
return mpmc_queue(&fastcgi_handler_cxt, ctx);
|
||||
}
|
||||
|
30
src/workers.h
Normal file
30
src/workers.h
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* sesimos - secure, simple, modern web server
|
||||
* @brief Worker interface (header file)
|
||||
* @file src/workers.h
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2022-12-29
|
||||
*/
|
||||
|
||||
#ifndef SESIMOS_WORKERS_H
|
||||
#define SESIMOS_WORKERS_H
|
||||
|
||||
#include "server.h"
|
||||
|
||||
int workers_init(void);
|
||||
|
||||
void workers_stop(void);
|
||||
|
||||
void workers_destroy(void);
|
||||
|
||||
int tcp_accept(client_ctx_t *ctx);
|
||||
|
||||
int tcp_close(client_ctx_t *ctx);
|
||||
|
||||
int handle_request(client_ctx_t *ctx);
|
||||
|
||||
int respond(client_ctx_t *ctx);
|
||||
|
||||
int fastcgi_handle(client_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_WORKERS_H
|
Reference in New Issue
Block a user