Add Timeout for processing connections
authorEduardo Silva <edsiper@gmail.com>
Fri, 6 Nov 2009 22:17:15 +0000 (19:17 -0300)
committerEduardo Silva <edsiper@gmail.com>
Fri, 6 Nov 2009 22:17:15 +0000 (19:17 -0300)
configure
src/epoll.c
src/http.c
src/include/epoll.h
src/include/request.h
src/monkey.c
src/request.c

index 4b0dd7f..311e39c 100755 (executable)
--- a/configure
+++ b/configure
@@ -119,14 +119,14 @@ main()
 
         echo 
        echo "Checking dependencies"
-        @echo "====================="
+        echo "====================="
         echo 
        messages_$lang "7"
        check_lib lang
 
         echo 
-        @echo "Creating Makefiles and scripts"
-        @echo "=============================="
+        echo "Creating Makefiles and scripts"
+        echo "=============================="
         echo 
 
        messages_$lang "1"
@@ -295,13 +295,13 @@ OBJ       = monkey.o method.o mimetype.o request.o \\
 all: ../bin/monkey
 
 ../bin/monkey: \$(OBJ)
-       echo "Compiling Monkey"
-       echo "================"
+       @echo "Compiling Monkey"
+       @echo "================"
        \$(CC) \$(CFLAGS) \$(LIBS) \$(LDFLAGS) -o \$@ \$(OBJ) $mod_obj
        $STRIP ../bin/monkey
-       echo
-       printf "Compiling Plugins"
-       echo "================="
+       @echo
+       @echo "Compiling Plugins"
+       @echo "================="
        @(cd plugins && make all && cd ..)
        
 clean:
index 1838ad8..27d025e 100644 (file)
 
 #include "monkey.h"
 #include "socket.h"
+#include "clock.h"
+#include "request.h"
+#include "config.h"
+#include "scheduler.h"
 #include "epoll.h"
 
 #define MAX_EVENTS 5000
@@ -63,15 +67,22 @@ int mk_epoll_create(int max_events)
 void *mk_epoll_init(int efd, mk_epoll_calls *calls, int max_events)
 {
        int i, ret=-1;
+        int num_fds;
+        int fds_timeout;
+        struct epoll_event events[max_events];
+        struct request_idx  *req_idx;
+        struct client_request *req_cl;
 
         pthread_mutex_lock(&mutex_wait_register);
         pthread_mutex_unlock(&mutex_wait_register);
         
+        fds_timeout = log_current_utime + config->timeout;
+
         while(1){
-                struct epoll_event events[max_events];
-                int num_fds = epoll_wait(efd, events, max_events, -1);
+                num_fds = epoll_wait(efd, events, 
+                                     max_events, MK_EPOLL_WAIT_TIMEOUT);
 
-                for(i=0; i< num_fds; i++) {
+                for(i=0; i<num_fds; i++) {
                         // Case 1: Error condition
                         if (events[i].events & (EPOLLHUP | EPOLLERR)) {
                                 close(events[i].data.fd);
@@ -96,6 +107,26 @@ void *mk_epoll_init(int efd, mk_epoll_calls *calls, int max_events)
                                 close(events[i].data.fd);
                         }
                 }
+
+                /* Check timeouts */
+                if(log_current_utime >= fds_timeout){
+                        /* Update next timeout */
+                        fds_timeout = log_current_utime + config->timeout;
+
+                        req_idx = mk_sched_get_request_index();
+                        req_cl = req_idx->first;
+
+                        while(req_cl){
+                                if(req_cl->status == MK_REQUEST_STATUS_INCOMPLETE){
+                                        if(req_cl->init_time + 
+                                           config->timeout >= log_current_utime){
+                                                close(req_cl->socket);
+                                        }
+                                }
+                                req_cl = req_cl->next;
+                        }
+                }
+
         }
 }
 
index 6636485..261fa44 100644 (file)
@@ -564,10 +564,12 @@ int mk_http_pending_request(struct client_request *cr)
                                  * later
                                  */
                                 if(mk_method_post_content_length(cr->body) < 0){
+                                        cr->status = MK_REQUEST_STATUS_COMPLETED;
                                         return 0;
                                 }
                         }
                         else{
+                                cr->status = MK_REQUEST_STATUS_COMPLETED;
                                 return 0;
                         }
                 }
@@ -576,6 +578,7 @@ int mk_http_pending_request(struct client_request *cr)
                 }
         }
 
+        cr->status = MK_REQUEST_STATUS_COMPLETED;
         return 0;
 }
 
index 08e48ab..19c484d 100644 (file)
@@ -23,6 +23,8 @@
 #define MK_EPOLL_WRITE 1
 #define MK_EPOLL_RW 2
 
+#define MK_EPOLL_WAIT_TIMEOUT 3000
+
 #define MK_EPOLL_BEHAVIOR_DEFAULT 2 
 #define MK_EPOLL_BEHAVIOR_TRIGGERED 3
 
index 4593a62..963b395 100644 (file)
@@ -83,6 +83,9 @@ parametros de una peticion */
 #define MAX_REQUEST_PROTOCOL 10
 #define MAX_SCRIPTALIAS 3
 
+#define MK_REQUEST_STATUS_INCOMPLETE -1
+#define MK_REQUEST_STATUS_COMPLETED 0
+
 #define EXIT_NORMAL -1
 #define EXIT_PCONNECTION 24
 
index 2665f60..92b4a6d 100644 (file)
@@ -102,7 +102,7 @@ int main(int argc, char **argv)
        config->file_config=0;
                        
        opterr = 0;
-       while ((opt = getopt(argc, argv, "bDSvhc:")) != -1)
+       while ((opt = getopt(argc, argv, "DSvhc:")) != -1)
        {
                switch (opt) {
                        case 'v': 
index 4a30e5e..6ae6c9f 100644 (file)
@@ -921,8 +921,9 @@ struct client_request *mk_request_client_create(int socket)
        cr->pipelined = FALSE;
        cr->counter_connections = 0;
        cr->socket = socket;
-       cr->request = NULL;
-
+       cr->status = MK_REQUEST_STATUS_INCOMPLETE;
+        cr->request = NULL;
+        
         mk_pointer_set(&cr->ip, mk_socket_get_ip(socket));
 
         /* creation time in unix time */