Commits

Sümer Cip committed c315e2c

TP+. connection structure squezeed.

  • Participants
  • Parent commits c86407d
  • Tags v0.1 Build 5

Comments (0)

Files changed (4)

 
 #include "sys/epoll.h"
+#include "util.h"
 
 /* globals */
 static int epollfd = 0;
 int event_init(void (*ev_handler)(conn *c, event ev))
 {
     epollfd = epoll_create(POLL_MAX_EVENTS);
-    if (epollfd == -1) {
+    if (epollfd == -1) {        
+        LC_DEBUG(("epoll_ctl_create error.[%s]\r\n", strerror(errno)));
         syslog(LOG_ERR, "%s (%s)", "epoll create error.", strerror(errno));
         return 0;
     }
     /* todo : Note, Kernel < 2.6.9 requires a non null event pointer even for
          * EPOLL_CTL_DEL. */
     if (epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, 0) == -1) {
-        syslog(LOG_ERR, "%s (%s)", "epoll_ctl disconnect conn.", strerror(errno));
+        LC_DEBUG(("epoll_ctl_del error.[%s]\r\n", strerror(errno)));
+        syslog(LOG_ERR, "%s (%s)", "epoll_ctl_del error.", strerror(errno));
         return 0;
     }
     return 1;
     if (flags & EVENT_WRITE) {
         ev.events |= EPOLLOUT;
     }
-
-    if (!c->active) {
-        op = EPOLL_CTL_ADD;
-        c->active = 1;
-    } else {
-        op = EPOLL_CTL_MOD;
-    }
-
+    
+    // default op is ADD because conns are usually short lived but many.
+    op = EPOLL_CTL_ADD; 
     ev.data.ptr = c;
     if (epoll_ctl(epollfd, op, c->fd, &ev) == -1) {
-        syslog(LOG_ERR, "%s (%s)", "epoll_ctl_mod connection error.", strerror(errno));
-        return 0;
+        if (errno == EEXIST) {
+            if (epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) {
+                LC_DEBUG(("epoll_ctl_mod connection error.[%s]\r\n", strerror(errno)));
+                syslog(LOG_ERR, "epoll_ctl_mod connection error.[%s]\r\n", strerror(errno));
+                return 0;
+            }
+        }
     }
     return 1;
 }
 
 
     nfds = epoll_wait(epollfd, events, POLL_MAX_EVENTS, POLL_TIMEOUT);
-    if (nfds == -1) {
+    if (nfds == -1) {        
+        LC_DEBUG(("epoll_wait error.[%s]\r\n", strerror(errno)));
         syslog(LOG_ERR, "%s (%s)", "epoll wait error.", strerror(errno));
         return;
     }
 
     conn->fd = fd;
     conn->last_heard = CURRENT_TIME;
-    conn->active = 0;
     conn->listening = 0;
     conn->free = 0;
     conn->in = NULL;
 
 /* 
    This function will be called when application memory usage reaches a certain
-   ratio of the total available mem. Here, we will shrink static resources to gain
+   threshold ratio of the total available mem. Here, we will shrink static resources to gain
    more memory for dynamic resources.
   */
 void collect_unused_memory(void)
 {
-    // todo:
+    // todo: timedout items and free conns can be collected here.
 }
 
 
     OUT_OF_MEMORY = 0x06,
 } code_t;
 
-typedef struct conn conn;
-struct conn {
-    int fd; 						/* socket fd */
-    int listening;					/* listening socket? */
-    int active;					    /* conn have active events on the I/O interface */
-    time_t last_heard; 				/* last time we heard from the client */
-    conn_states state; 				/* state of the connection READ_KEY, READ_HEADER.etc...*/
-    request *in;					/* request */
-    response out;					/* response */
-    int free; 						/* recycle connection structure */
-    //int disconnect_after_send;      /* set this flag to disconnects the socket after send is completed */
-    conn *next;						/* next connection in the linked-list of the connections */
-};
+typedef struct conn {
+    int fd;                         /* socket fd */
+    uint8_t listening;              /* listening socket? */
+    uint8_t free;                   /* recycle connection structure */
+    time_t last_heard;              /* last time we heard from the client */
+    conn_states state;              /* state of the connection READ_KEY, READ_HEADER.etc...*/
+    request *in;                    /* request */
+    response out;                   /* response */
+    struct conn *next;              /* next connection in the linked-list of the connections */
+} conn;
 
 #endif
 [+] any optimization window for malloc()'s?
 v0.1 Build 5
 ------------
-[-] squeeze connection structures.
+[+] squeeze connection structures.
 [-] DEFERRED:TEST/INSPECT: dynamic alloc. failures.(OUT_OF_MEMORY)
 [-] any way to exclude math.h that requires -lm dependency for the linker?
 [-] lightcache clients shall be moved to clients folder. Maybe later make another rep.