changeset 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 08a465eec50b
children 6c322f9c2cb9
files README.md seobeo/os/s_linux_edge.c seobeo/os/s_macos_edge.c seobeo/s_linux_network.c seobeo/s_web.c
diffstat 5 files changed, 71 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Thu Oct 09 07:03:36 2025 -0700
+++ b/README.md	Fri Oct 10 06:59:32 2025 -0700
@@ -1,11 +1,29 @@
 # Zenbu
 
-This is a mono repo where I will share all my codes and utilize them using bazel. 
-I decied to do this since I re-use codes often and I don't want to deal with making make
-and cmake every time and this eliminates the problem that exists within C or C++ where using
-library is harder as we need to add gzillian stuff into it lmao.
+This is a mono repo where I will share all my codes and utilize them using bazel. I decied to do this since I re-use codes often and I don't want to deal with making make and cmake every time and this eliminates the problem that exists within C or C++ where using library is harder as we need to add gzillian stuff into it lmao.
+
+
+## Install
+
+I decide to use mercurial because I got used to this over git and I frankly don't need different branches for each feature to be merged in.
+I also decied to use the bazel for code sharing as I mentioned above.
 
-# Debugging Command
+```
+# linux 
+wget https://github.com/bazelbuild/bazelisk/releases/download/v1.18.0/bazelisk-linux-amd64 # Hope it still works lmao
+chmod +x bazelisk-linux-amd64
+sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel
+bazel version
+
+# mac
+brew install bazel
+```
+
+I might move these binary into the repo so that it has full history of it
+
+I assume mercurial is installed as well.
+
+## Debugging Command
 
 ```bash
 bazel build target -c dbg
@@ -19,6 +37,8 @@
 ```
 
 
+## This is for memory leak cheak in MacOS
+
 brew install valgrind
 # or use the unofficial ARM build:
 arch -x86_64 brew install valgrind
--- a/seobeo/os/s_linux_edge.c	Thu Oct 09 07:03:36 2025 -0700
+++ b/seobeo/os/s_linux_edge.c	Fri Oct 10 06:59:32 2025 -0700
@@ -5,27 +5,35 @@
 void  *Seobeo_Web_Edge_Worker(void *vargs)
 {
   WorkerArgs *args = vargs;
-  struct epoll_event events[64];
-  while (1) {
-    int n = epoll_wait(args->evfd, events, 64, -1);
+  uint32 max_events = 64;
+  struct epoll_event events[max_events];
+  while (1)
+  {
+    int n = epoll_wait(args->evfd, events, max_events, -1);
     if (n < 0) continue;
-    for (int i = 0; i < n; i++) {
-      Seobeo_PHandle h = events[i].data.ptr;
-      if (h == args->srv) {
-        // new connection
+
+    for (int i = 0; i < n; i++)
+    {
+      Seobeo_PHandle socket_fd = events[i].data.ptr;
+      // when server....
+      if (socket_fd == args->srv)
+      {
         Seobeo_PHandle cli =
-          Seobeo_Stream_Handle_Server_Accept(args->srv);
+          Seobeo_Stream_Handle_Server_Accept(socket_fd);
         if (!cli) continue;
-        struct epoll_event ev = {
-          .events   = EPOLLIN,
+
+
+        struct epoll_event client_ev = {
+          .events = EPOLLIN | EPOLLET,  
           .data.ptr = cli
         };
         epoll_ctl(args->evfd, EPOLL_CTL_ADD,
-                  cli->socket, &ev);
-      } else {
-        Seobeo_Web_HandleClientRequest(h, args->cache); // This frees
-        epoll_ctl(args->evfd, EPOLL_CTL_DEL,
-                  h->socket, NULL);
+                  cli->socket, &client_ev);
+      }
+      // when client....
+      else
+      {
+        Seobeo_Web_HandleClientRequest(socket_fd, args->cache); // This frees
       }
     }
   }
@@ -39,7 +47,7 @@
 {
   int epfd = epoll_create1(0);
   struct epoll_event ev = {
-    .events   = EPOLLIN | EPOLLONESHOT,
+    .events   = EPOLLIN,
     .data.ptr = p_server_handle
   };
   epoll_ctl(epfd, EPOLL_CTL_ADD,
@@ -47,7 +55,7 @@
 
   pthread_attr_t attr;
   pthread_attr_init(&attr);
-  pthread_attr_setstacksize(&attr, 100 * 1024 * 1024); // 100 MB
+  pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB
 
   pthread_t  threads[thread_count];
   for (int i = 0; i < thread_count; i++)
--- a/seobeo/os/s_macos_edge.c	Thu Oct 09 07:03:36 2025 -0700
+++ b/seobeo/os/s_macos_edge.c	Fri Oct 10 06:59:32 2025 -0700
@@ -6,14 +6,19 @@
 {
   WorkerArgs *args = vargs;
   struct kevent evlist[64];
-  while (1) {
+  while (1)
+  {
     int ne = kevent(args->evfd, NULL, 0, evlist, 64, NULL);
     if (ne < 0) continue;
-    for (int i = 0; i < ne; i++) {
+
+    for (int i = 0; i < ne; i++)
+    {
       Seobeo_PHandle h = evlist[i].udata;
-      if (h == args->srv) {
+      if (h == args->srv)
+      {
         Seobeo_PHandle cli =
           Seobeo_Stream_Handle_Server_Accept(args->srv);
+
         if (!cli) continue;
         struct kevent kev = {
           .ident  = cli->socket,
@@ -55,7 +60,7 @@
 
   pthread_attr_t attr;
   pthread_attr_init(&attr);
-  pthread_attr_setstacksize(&attr, 100 * 1024 * 1024); // 100 MB
+  pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB
 
   pthread_t  threads[thread_count];
   for (int i = 0; i < thread_count; i++)
--- a/seobeo/s_linux_network.c	Thu Oct 09 07:03:36 2025 -0700
+++ b/seobeo/s_linux_network.c	Fri Oct 10 06:59:32 2025 -0700
@@ -148,6 +148,7 @@
   struct sockaddr_storage addr;
   socklen_t addrlen = sizeof addr;
   char client_inet_addr[INET6_ADDRSTRLEN];
+
   int client_fd = accept(p_server_handle->socket,
                          (struct sockaddr*)&addr,
                          &addrlen);
@@ -155,11 +156,12 @@
       addr.ss_family,
       Seobeo_Get_IP4_Or_IP6((struct sockaddr *)&addr),
       client_inet_addr, sizeof client_inet_addr);
+  if (client_fd == -1) return NULL;
 
-  if (client_fd == -1)
-  {
-    return NULL;
-  }
+  // Set non blocking...
+  int flags = fcntl(client_fd, F_GETFL, 0);
+  if (flags == -1) return NULL;
+  fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
 
   Seobeo_PHandle p_client_handle        = malloc(sizeof *p_client_handle);
 
--- a/seobeo/s_web.c	Thu Oct 09 07:03:36 2025 -0700
+++ b/seobeo/s_web.c	Fri Oct 10 06:59:32 2025 -0700
@@ -202,6 +202,7 @@
                         (const uint8*)entry->buffer,
                         (uint32)body_size);
     Seobeo_Handle_Flush(p_cli_handle);
+    printf("DONE\n\n\n");
   }
   else if (strcmp(method, "POST") == 0)
   {
@@ -247,10 +248,15 @@
                         (uint32)strlen(p_response_header));
     Seobeo_Handle_Flush(p_cli_handle);
   }
+  goto clean_up;
 
 clean_up:
+  printf("CLEAN UP\n\n\n");
   if (p_cli_handle)
+  {
+    printf("CLEAN UP called on client handle\n\n\n");
     Seobeo_Handle_Destroy(p_cli_handle);
+  }
   if (p_response_arena)
     Dowa_Arena_Destroy(p_response_arena);
 }