fix(lnm): seemingly fix performance regression
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/docker Pipeline was successful Details

new-lnm-integration
Jef Roosens 2023-12-08 22:03:47 +01:00
parent 58a8645c6c
commit a0954e8d07
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 13 additions and 12 deletions

View File

@ -6,7 +6,7 @@
#include "lnm/common.h" #include "lnm/common.h"
#define LNM_LOOP_BUF_SIZE 4096 #define LNM_LOOP_BUF_SIZE 2048
#define LNM_LOOP_INITIAL_CONNS 16 #define LNM_LOOP_INITIAL_CONNS 16
typedef enum { typedef enum {

View File

@ -341,11 +341,9 @@ void lnm_http_loop_process(lnm_http_conn *conn) {
http_loop_state = ctx->state; http_loop_state = ctx->state;
process_fns[http_loop_state](conn); process_fns[http_loop_state](conn);
} while ((conn->state == loop_state) && } while ((conn->state == state_map[ctx->state]) &&
(conn->state == state_map[http_loop_state]) &&
(http_loop_state != ctx->state)); (http_loop_state != ctx->state));
// Check required to prevent overwriting manually set event loop state // Check required to prevent overwriting manually set event loop state
conn->state = conn->state = conn->state == loop_state ? state_map[ctx->state] : conn->state;
conn->state == loop_state ? state_map[http_loop_state] : conn->state;
} }

View File

@ -76,7 +76,7 @@ lnm_err lnm_loop_accept(lnm_loop *l) {
} }
lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) { lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) {
int listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) { if (listen_fd < 0) {
return lnm_err_failed_network; return lnm_err_failed_network;
@ -105,6 +105,9 @@ lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) {
return lnm_err_failed_network; return lnm_err_failed_network;
} }
int flags = fcntl(listen_fd, F_GETFL);
fcntl(listen_fd, F_SETFL, flags | O_NONBLOCK);
l->listen_fd = listen_fd; l->listen_fd = listen_fd;
return lnm_err_ok; return lnm_err_ok;
@ -116,7 +119,7 @@ lnm_err lnm_loop_run(lnm_loop *l) {
} }
struct pollfd *poll_args = struct pollfd *poll_args =
malloc((LNM_LOOP_INITIAL_CONNS + 1) * sizeof(struct pollfd)); calloc(LNM_LOOP_INITIAL_CONNS + 1, sizeof(struct pollfd));
size_t poll_args_cap = LNM_LOOP_INITIAL_CONNS + 1; size_t poll_args_cap = LNM_LOOP_INITIAL_CONNS + 1;
if (poll_args == NULL) { if (poll_args == NULL) {
@ -128,7 +131,7 @@ lnm_err lnm_loop_run(lnm_loop *l) {
poll_args[0].events = POLLIN; poll_args[0].events = POLLIN;
while (1) { while (1) {
size_t poll_args_len = 1; nfds_t poll_args_len = 1;
// Add all open connections to the poll command // Add all open connections to the poll command
for (size_t i = 0; i < l->conns.len && poll_args_len < l->conns.open + 1; for (size_t i = 0; i < l->conns.len && poll_args_len < l->conns.open + 1;
@ -176,7 +179,7 @@ lnm_err lnm_loop_run(lnm_loop *l) {
} }
if (poll_args_cap < l->conns.open + 1) { if (poll_args_cap < l->conns.open + 1) {
struct pollfd *buf = malloc((l->conns.open + 1) * sizeof(struct pollfd)); struct pollfd *buf = calloc(l->conns.open + 1, sizeof(struct pollfd));
if (buf == NULL) { if (buf == NULL) {
return lnm_err_failed_alloc; return lnm_err_failed_alloc;

View File

@ -38,6 +38,8 @@ void lnm_loop_conn_io_req(lnm_loop *l, lnm_loop_conn *conn) {
void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) { void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
do { do {
l->data_write(conn);
ssize_t res; ssize_t res;
do { do {
@ -49,7 +51,7 @@ void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
return; return;
} }
if (res <= 0) { if (res < 0) {
conn->state = lnm_loop_state_end; conn->state = lnm_loop_state_end;
return; return;
@ -59,8 +61,6 @@ void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
// writer function more space to work with // writer function more space to work with
memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res); memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res);
conn->w.size -= res; conn->w.size -= res;
l->data_write(conn);
} while (conn->state == lnm_loop_state_res); } while (conn->state == lnm_loop_state_res);
} }