merge in the 1.4 QF
authorAleš Křenek <ljocha@ics.muni.cz>
Wed, 30 Nov 2005 16:11:33 +0000 (16:11 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Wed, 30 Nov 2005 16:11:33 +0000 (16:11 +0000)
org.glite.lb.server-bones/examples/cnt_example.c
org.glite.lb.server-bones/src/srvbones.c

index 5f73a23..80b6af3 100644 (file)
@@ -21,6 +21,7 @@ static struct option opts[] = {
        { "debug",      no_argument,            NULL, 'd'},
        { "msg",        required_argument,      NULL, 'm'},
        { "port",       required_argument,      NULL, 'p'},
+       { "repeat",     required_argument,      NULL, 'r'},
 };
 
 int debug  = 0;
@@ -49,11 +50,11 @@ int main(int argc, char **argv)
        int                                     opt,
                                                sock,
                                                n;
-
+       int     repeat = 1;
 
        me = strrchr(argv[0], '/');
        if ( me ) me++; else me = argv[0];
-       while ( (opt = getopt_long(argc, argv,"p:m:hd", opts, NULL)) != EOF )
+       while ( (opt = getopt_long(argc, argv,"p:m:hdr:", opts, NULL)) != EOF )
        {
                switch ( opt )
                {
@@ -64,6 +65,7 @@ int main(int argc, char **argv)
                        port = atoi(optarg);
                        break;
                case 'd': debug = 1; break;
+               case 'r': repeat = atoi(optarg); break;
                case 'h': usage(me); return 0;
                case '?': usage(me); return 1;
                }
@@ -84,19 +86,21 @@ int main(int argc, char **argv)
                exit(1);
        }
        n = strlen(msg? msg: DEF_MSG);
-       if ( writen(sock, msg? msg: DEF_MSG, n) != n )
-       {
-               dprintf(("error writing message\n"));
-               exit(1);
-       }
-       printf("reply: "); fflush(stdout);
-       n = readln(sock, buff);
-       if ( n < 0 )
-       {
-               perror("read() reply error");
-               return 1;
+       for (;repeat; repeat--) {
+               if ( writen(sock, msg? msg: DEF_MSG, n) != n )
+               {
+                       dprintf(("error writing message\n"));
+                       exit(1);
+               }
+               printf("reply: "); fflush(stdout);
+               n = readln(sock, buff);
+               if ( n < 0 )
+               {
+                       perror("read() reply error");
+                       return 1;
+               }
+               writen(0, buff, n);
        }
-       writen(0, buff, n);
        close(sock);
 
        return 0;
index 12f748e..179cb4e 100644 (file)
 
 #define SLAVES_COUNT           5               /* default number of slaves */
 #define SLAVE_OVERLOAD         10              /* queue items per slave */
-#define SLAVE_CONNS_MAX                500             /* commit suicide after that many connections */
+#define SLAVE_REQS_MAX         500             /* commit suicide after that many connections */
 #define IDLE_TIMEOUT           30              /* keep idle connection that many seconds */
 #define CONNECT_TIMEOUT                5               /* timeout for establishing a connection */
 #define REQUEST_TIMEOUT                10              /* timeout for a single request */ 
+#define NEW_CLIENT_DURATION    10              /* how long a client is considered new, i.e. busy
+                                                  connection is not closed to serve other clients */
 
 #ifndef dprintf
 #define dprintf(x)                     { if (debug) printf x; }
@@ -44,7 +46,7 @@ static int                            services_ct;
 
 static int             set_slaves_ct = SLAVES_COUNT;
 static int             set_slave_overload = SLAVE_OVERLOAD;
-static int             set_slave_conns_max = SLAVE_CONNS_MAX;
+static int             set_slave_reqs_max = SLAVE_REQS_MAX;
 static struct timeval  set_idle_to = {IDLE_TIMEOUT, 0};
 static struct timeval  set_connect_to = {CONNECT_TIMEOUT, 0};
 static struct timeval  set_request_to = {REQUEST_TIMEOUT, 0};
@@ -276,12 +278,13 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
        sigset_t                sset;
        struct sigaction        sa;
        struct timeval          client_done,
-                               client_start;
+                               client_start,
+                               new_client_duration = { NEW_CLIENT_DURATION, 0 };
 
        void    *clnt_data = NULL;
        int     conn = -1,
                srv = -1,
-               conn_cnt = 0,
+               req_cnt = 0,
                sockflags,
                h_errno,
                pid, i,
@@ -322,19 +325,23 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                 */
                exit(1);
 
-       while ( !die && (conn_cnt < set_slave_conns_max || conn >= 0) )
+       while ( !die && req_cnt < set_slave_reqs_max)
        {
                fd_set                          fds;
                int                                     max = sock,
                                                        connflags,
                                                        newconn = -1,
-                                                       newsrv = -1,
-                                                       kick_client = 0;
+                                                       newsrv = -1;
+
+               enum { KICK_DONT = 0, KICK_IDLE, KICK_LOAD, KICK_HANDLER, KICK_COUNT }
+                       kick_client = KICK_DONT;
+
                static char * kicks[] = {
                        "don't kick",
                        "idle client",
                        "high load",
-                       "no request handler"
+                       "no request handler",
+                       "request count limit reached",
                };
                unsigned long           seq;
                struct timeval          now,to;
@@ -367,7 +374,7 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                sigprocmask(SIG_BLOCK, &sset, NULL);
 
                gettimeofday(&now,NULL);
-               if (conn >= 0 && check_timeout(set_idle_to, client_done, now)) kick_client = 1;
+               if (conn >= 0 && check_timeout(set_idle_to, client_done, now)) kick_client = KICK_IDLE;
 
                if ( conn >= 0 && !kick_client && FD_ISSET(conn, &fds) )
                {
@@ -377,11 +384,12 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                        int             rv;
 
                        dprintf(("[%d] incoming request\n", getpid()));
+
                        if ( !services[srv].on_request_hnd )
                        {
-                               kick_client = 3;
+                               kick_client = KICK_HANDLER;
                        } else {
-
+                               req_cnt++;
                                first_request = 0;
                                to = set_request_to;
                                if ((rv = services[srv].on_request_hnd(conn,to.tv_sec>=0 ? &to : NULL,clnt_data)) == ENOTCONN) {
@@ -417,12 +425,12 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                                        gettimeofday(&client_done, NULL);
                                }
 
-                               continue;
+                               if (!check_timeout(new_client_duration,client_start,now)) continue;
 
                        }
                }
 
-               if ( !first_request && FD_ISSET(sock, &fds) && conn_cnt < set_slave_conns_max )
+               if ( !first_request && FD_ISSET(sock, &fds) && req_cnt < set_slave_reqs_max )
                {
                        if ( conn >= 0 ) usleep(100000 + 1000 * (random() % 200));
                        if ( do_recvmsg(sock, &newconn, &seq, &newsrv) ) switch ( errno )
@@ -434,9 +442,11 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                                if (!debug) syslog(LOG_CRIT,"recvmsg(): %m\n");
                                exit(1);
                        }
-                       kick_client = 2;
+                       kick_client = KICK_LOAD;
                }
 
+               if (req_cnt >= set_slave_reqs_max) kick_client = KICK_COUNT;
+
                if ( kick_client && conn >= 0 )
                {
                        if ( services[srv].on_disconnect_hnd )
@@ -469,7 +479,7 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                                exit(1);
                        }
        
-                       conn_cnt++;
+                       req_cnt++;
                        dprintf(("[%d] serving %s connection %lu\n", getpid(),
                                        services[srv].id? services[srv].id: "", seq));
        
@@ -502,8 +512,12 @@ static int slave(slave_data_init_hnd data_init_hnd, int sock)
                dprintf(("[%d] Terminating on signal %d\n", getpid(), die));
                if ( !debug ) syslog(LOG_INFO, "Terminating on signal %d", die);
        }
-       dprintf(("[%d] Terminating after %d connections\n", getpid(), conn_cnt));
-       if ( !debug ) syslog(LOG_INFO, "Terminating after %d connections", conn_cnt);
+
+       if (conn >= 0  && services[srv].on_disconnect_hnd )
+               services[srv].on_disconnect_hnd(conn, NULL, clnt_data);
+
+       dprintf(("[%d] Terminating after %d requests\n", getpid(), req_cnt));
+       if ( !debug ) syslog(LOG_INFO, "Terminating after %d requests", req_cnt);
 
 
        exit(0);
@@ -618,7 +632,7 @@ static void glite_srvbones_set_slave_overload(int n)
 
 static void glite_srvbones_set_slave_conns_max(int n)
 {
-       set_slave_conns_max = (n == -1)? SLAVE_CONNS_MAX: n;
+       set_slave_reqs_max = (n == -1)? SLAVE_REQS_MAX: n;
 }
 
 static void set_timeout(struct timeval *to, struct timeval *val)