diff src/c/urweb.c @ 673:a8effb6159c2

Variable timeouts and client keep-alive
author Adam Chlipala <adamc@hcoop.net>
date Tue, 24 Mar 2009 15:35:46 -0400
parents df6eb58de040
children 5ff1ff38e2db
line wrap: on
line diff
--- a/src/c/urweb.c	Tue Mar 24 15:05:28 2009 -0400
+++ b/src/c/urweb.c	Tue Mar 24 15:35:46 2009 -0400
@@ -220,10 +220,8 @@
   }
 
   if (c->data.used.sock != -1) {
-    pthread_mutex_unlock(&c->data.used.lock);
-    close(sock);
-    fprintf(stderr, "Duplicate client connection (%d)\n", (int)id);
-    return;
+    close(c->data.used.sock);
+    c->data.used.sock = -1;
   }
 
   c->data.used.last_contact = time(NULL);
@@ -288,11 +286,13 @@
   }
 }
 
-void uw_prune_clients(time_t timeout) {
+extern int uw_timeout;
+
+void uw_prune_clients() {
   size_t i;
   time_t cutoff;
 
-  cutoff = time(NULL) - timeout;
+  cutoff = time(NULL) - uw_timeout;
 
   pthread_mutex_lock(&clients_mutex);
 
@@ -507,6 +507,8 @@
   size_t n_deltas;
   channel_delta *deltas;
 
+  int timeout;
+
   char error_message[ERROR_BUF_LEN];
 };
 
@@ -541,6 +543,8 @@
   ctx->n_deltas = 0;
   ctx->deltas = malloc(0);
 
+  ctx->timeout = uw_timeout;
+
   return ctx;
 }
 
@@ -834,10 +838,15 @@
     int pass;
     client *c = uw_new_client(&pass);
 
-    char *r = uw_malloc(ctx, strlen(ctx->script_header) + 56 + 2 * INTS_MAX + buf_used(&ctx->script)
+    char *r = uw_malloc(ctx, strlen(ctx->script_header) + 65 + 3 * INTS_MAX + buf_used(&ctx->script)
                         + strlen(ctx->url_prefix));
-    sprintf(r, "%s<script>client_id=%d;client_pass=%d;url_prefix=\"%s\";%s</script>",
-            ctx->script_header, (int)c->id, c->data.used.pass, ctx->url_prefix, ctx->script.start);
+    sprintf(r, "%s<script>client_id=%d;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s</script>",
+            ctx->script_header,
+            (int)c->id,
+            c->data.used.pass,
+            ctx->url_prefix,
+            ctx->timeout,
+            ctx->script.start);
     return r;
   }
 }