Mercurial
comparison seobeo/os/s_linux_edge.c @ 33:c0f6c8c7829f
[Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Fri, 10 Oct 2025 06:59:32 -0700 |
| parents | 875bb6e10db7 |
| children | 84672efec192 |
comparison
equal
deleted
inserted
replaced
| 32:08a465eec50b | 33:c0f6c8c7829f |
|---|---|
| 3 | 3 |
| 4 | 4 |
| 5 void *Seobeo_Web_Edge_Worker(void *vargs) | 5 void *Seobeo_Web_Edge_Worker(void *vargs) |
| 6 { | 6 { |
| 7 WorkerArgs *args = vargs; | 7 WorkerArgs *args = vargs; |
| 8 struct epoll_event events[64]; | 8 uint32 max_events = 64; |
| 9 while (1) { | 9 struct epoll_event events[max_events]; |
| 10 int n = epoll_wait(args->evfd, events, 64, -1); | 10 while (1) |
| 11 { | |
| 12 int n = epoll_wait(args->evfd, events, max_events, -1); | |
| 11 if (n < 0) continue; | 13 if (n < 0) continue; |
| 12 for (int i = 0; i < n; i++) { | 14 |
| 13 Seobeo_PHandle h = events[i].data.ptr; | 15 for (int i = 0; i < n; i++) |
| 14 if (h == args->srv) { | 16 { |
| 15 // new connection | 17 Seobeo_PHandle socket_fd = events[i].data.ptr; |
| 18 // when server.... | |
| 19 if (socket_fd == args->srv) | |
| 20 { | |
| 16 Seobeo_PHandle cli = | 21 Seobeo_PHandle cli = |
| 17 Seobeo_Stream_Handle_Server_Accept(args->srv); | 22 Seobeo_Stream_Handle_Server_Accept(socket_fd); |
| 18 if (!cli) continue; | 23 if (!cli) continue; |
| 19 struct epoll_event ev = { | 24 |
| 20 .events = EPOLLIN, | 25 |
| 26 struct epoll_event client_ev = { | |
| 27 .events = EPOLLIN | EPOLLET, | |
| 21 .data.ptr = cli | 28 .data.ptr = cli |
| 22 }; | 29 }; |
| 23 epoll_ctl(args->evfd, EPOLL_CTL_ADD, | 30 epoll_ctl(args->evfd, EPOLL_CTL_ADD, |
| 24 cli->socket, &ev); | 31 cli->socket, &client_ev); |
| 25 } else { | 32 } |
| 26 Seobeo_Web_HandleClientRequest(h, args->cache); // This frees | 33 // when client.... |
| 27 epoll_ctl(args->evfd, EPOLL_CTL_DEL, | 34 else |
| 28 h->socket, NULL); | 35 { |
| 36 Seobeo_Web_HandleClientRequest(socket_fd, args->cache); // This frees | |
| 29 } | 37 } |
| 30 } | 38 } |
| 31 } | 39 } |
| 32 return NULL; | 40 return NULL; |
| 33 } | 41 } |
| 37 int thread_count, | 45 int thread_count, |
| 38 Dowa_PHashMap p_html_cache) | 46 Dowa_PHashMap p_html_cache) |
| 39 { | 47 { |
| 40 int epfd = epoll_create1(0); | 48 int epfd = epoll_create1(0); |
| 41 struct epoll_event ev = { | 49 struct epoll_event ev = { |
| 42 .events = EPOLLIN | EPOLLONESHOT, | 50 .events = EPOLLIN, |
| 43 .data.ptr = p_server_handle | 51 .data.ptr = p_server_handle |
| 44 }; | 52 }; |
| 45 epoll_ctl(epfd, EPOLL_CTL_ADD, | 53 epoll_ctl(epfd, EPOLL_CTL_ADD, |
| 46 p_server_handle->socket, &ev); | 54 p_server_handle->socket, &ev); |
| 47 | 55 |
| 48 pthread_attr_t attr; | 56 pthread_attr_t attr; |
| 49 pthread_attr_init(&attr); | 57 pthread_attr_init(&attr); |
| 50 pthread_attr_setstacksize(&attr, 100 * 1024 * 1024); // 100 MB | 58 pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB |
| 51 | 59 |
| 52 pthread_t threads[thread_count]; | 60 pthread_t threads[thread_count]; |
| 53 for (int i = 0; i < thread_count; i++) | 61 for (int i = 0; i < thread_count; i++) |
| 54 { | 62 { |
| 55 WorkerArgs *args = malloc(sizeof(WorkerArgs)); | 63 WorkerArgs *args = malloc(sizeof(WorkerArgs)); |