Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / bluesky / store-simple.c
index a66053e..25b6883 100644 (file)
@@ -3,7 +3,29 @@
  * Copyright (C) 2009  The Regents of the University of California
  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
  *
- * TODO: Licensing
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 /* Interface to the simple BlueSky test storage server. */
 
 #include "bluesky-private.h"
 
+#define MAX_IDLE_CONNECTIONS 8
+
 typedef struct {
     GThreadPool *thread_pool;
     struct sockaddr_in server_addr;
+
+    /* A pool of open file connections to the server which are not currently in
+     * use. */
+    GQueue *fd_pool;
+    GMutex *fd_pool_lock;
 } SimpleStore;
 
+static int get_connection(SimpleStore *store)
+{
+    int fd = -1;
+
+    g_mutex_lock(store->fd_pool_lock);
+    if (!g_queue_is_empty(store->fd_pool)) {
+        fd = GPOINTER_TO_INT(g_queue_pop_head(store->fd_pool));
+    }
+    g_mutex_unlock(store->fd_pool_lock);
+    if (fd != -1)
+        return fd;
+
+    fd = socket(PF_INET, SOCK_STREAM, 0);
+    if (fd < 0) {
+        g_warning("Error creating simplestore socket: %m");
+        return -1;
+    }
+
+    if (connect(fd, (struct sockaddr *)&store->server_addr,
+                sizeof(store->server_addr)) < 0) {
+        g_warning("Error connecting to simplestore server: %m");
+        return -1;
+    }
+
+    return fd;
+}
+
+static void put_connection(SimpleStore *store, int fd)
+{
+    g_mutex_lock(store->fd_pool_lock);
+    g_queue_push_head(store->fd_pool, GINT_TO_POINTER(fd));
+    while (g_queue_get_length(store->fd_pool) > MAX_IDLE_CONNECTIONS) {
+        fd = GPOINTER_TO_INT(g_queue_pop_tail(store->fd_pool));
+        close(fd);
+    }
+    g_mutex_unlock(store->fd_pool_lock);
+}
+
 static gboolean write_data(int fd, const char *buf, size_t len)
 {
     while (len > 0) {
@@ -103,17 +170,8 @@ static void simplestore_task(gpointer a, gpointer b)
 
     async->status = ASYNC_RUNNING;
 
-    int fd = socket(PF_INET, SOCK_STREAM, 0);
+    int fd = get_connection(server);
     if (fd < 0) {
-        g_warning("Error creating simplestore socket: %m");
-        bluesky_store_async_mark_complete(async);
-        bluesky_store_async_unref(async);
-        return;
-    }
-
-    if (connect(fd, (struct sockaddr *)&server->server_addr,
-                sizeof(server->server_addr)) < 0) {
-        g_warning("Error connecting to simplestore server: %m");
         bluesky_store_async_mark_complete(async);
         bluesky_store_async_unref(async);
         return;
@@ -191,10 +249,15 @@ static void simplestore_task(gpointer a, gpointer b)
         break;
     }
 
+    int success = (async->result == 0);
     bluesky_store_async_mark_complete(async);
     bluesky_store_async_unref(async);
 
-    close(fd);
+    if (success) {
+        put_connection(server, fd);
+    } else {
+        close(fd);
+    }
 }
 
 static char *simplestore_lookup_last(gpointer s, const char *prefix)
@@ -208,7 +271,7 @@ static gpointer simplestore_new(const gchar *path)
 
     /* TODO: Right now we leak this memory.  We should probably clean up in
      * simplestore_destroy, but it's not a big deal. */
-    const gchar *host = "127.0.0.1", *port = "8257";
+    const gchar *host = "127.0.0.1", *port = "9541";
     if (path != NULL) {
         gchar **target = g_strsplit(path, ":", 0);
         if (target[0] != NULL) {
@@ -232,7 +295,6 @@ static gpointer simplestore_new(const gchar *path)
                 gai_strerror(res));
         return NULL;
     }
-    freeaddrinfo(lookup_result);
     for (struct addrinfo *ai = lookup_result; ai != NULL; ai = ai->ai_next) {
         printf("flags=%d family=%d socktype=%d proto=%d\n",
                ai->ai_flags,
@@ -246,6 +308,10 @@ static gpointer simplestore_new(const gchar *path)
             fprintf(stderr, "Warning: Bad address record size!\n");
         }
     }
+    freeaddrinfo(lookup_result);
+
+    store->fd_pool = g_queue_new();
+    store->fd_pool_lock = g_mutex_new();
 
     store->thread_pool = g_thread_pool_new(simplestore_task, NULL,
                                            bluesky_max_threads, FALSE, NULL);