The system used to maintain a pool of persistent connected database
authorMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 15 Mar 2007 20:46:28 +0000 (20:46 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 15 Mar 2007 20:46:28 +0000 (20:46 +0000)
sessions for performance.  However, the postgresql libraries seem to
have been compiled with thread-safety support disabled.  Despite only
one thread using a connection from the pool at a time, a thread being
assigned a connection previouly initiated by another thread (i.e. the
first client connection causes the first thread to spawn a "page" of
connections in the pool) would no longer be able to perform requests
on that inherited connection.

Explicitely openning/closing a db connection per session solves this
problem for now, although at a performance cost, of course.   I shall
resume testing with persistent connections on another box on which
I'll compile the postgresql client library with thread-safety support
eventually.

mmsoftware/mmmail/src/mmpop3d/mmpop3d.c
mmsoftware/mmmail/src/mmsmtpd/mmsmtpd.c

index 41fc2ad..ca7d2ba 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: mmpop3d.c,v 1.41.4.3 2007/03/15 17:52:34 mmondor Exp $ */
+/* $Id: mmpop3d.c,v 1.41.4.4 2007/03/15 20:46:26 mmondor Exp $ */
 
 /*
  * Copyright (C) 2001-2007, Matthew Mondor
@@ -83,7 +83,7 @@
 
 MMCOPYRIGHT("@(#) Copyright (c) 2001-2007\n\
 \tMatthew Mondor. All rights reserved.\n");
-MMRCSID("$Id: mmpop3d.c,v 1.41.4.3 2007/03/15 17:52:34 mmondor Exp $");
+MMRCSID("$Id: mmpop3d.c,v 1.41.4.4 2007/03/15 20:46:26 mmondor Exp $");
 
 
 
@@ -502,7 +502,7 @@ auth_pass(clientenv *clenv)
            params[1] = NULL;
            if ((pgres = PQexecParams(clenv->pgconn,
                    "SELECT \"user\",passwd FROM box LEFT JOIN \"user\" ON "
-                   "\"user\"=id WHERE address='$1' AND active='t'",
+                   "\"user\"=id WHERE address=$1 AND active='t'",
                    1, NULL, params, NULL, NULL, 0)) != NULL) {
                if (PQntuples(pgres) == 1) {
                    mm_strcpy(id, PQgetvalue(pgres, 0, 0));
@@ -850,8 +850,13 @@ clenv_constructor(pnode_t *pn)
 
        mmstat_init(&clenv->vstat, TRUE, TRUE);
        mmstat_init(&clenv->pstat, TRUE, FALSE);
-       if ((clenv->pgconn = PQconnectdb(CONF.DB_INFO)) == NULL)
-               return FALSE;
+
+       /* XXX Performance enhancement but requires thread safety
+       if ((clenv->pgconn = PQconnectdb(CONF.DB_INFO)) == NULL) {
+           mmsyslog(LOGLEVEL, 0, "PQconnectdb()");
+           return FALSE;
+       }
+       */
 
        return TRUE;
 }
@@ -859,12 +864,14 @@ clenv_constructor(pnode_t *pn)
 static void
 clenv_destructor(pnode_t *pn)
 {
+       /* XXX Performance enhancement but requires thread safety
        clientenv       *clenv = (clientenv *)pn;
 
        if (clenv->pgconn != NULL) {
-               PQfinish(clenv->pgconn);
-               clenv->pgconn = NULL;
+           PQfinish(clenv->pgconn);
+           clenv->pgconn = NULL;
        }
+       */
 }
 
 
@@ -878,6 +885,9 @@ alloc_clientenv(void)
     clenv = (clientenv *)pool_alloc(&clenv_pool, TRUE);
     pthread_mutex_unlock(&clenv_lock);
 
+    /* XXX for now */
+    clenv->pgconn = PQconnectdb(CONF.DB_INFO);
+
     return (clenv);
 }
 
@@ -922,6 +932,10 @@ free_clientenv(clientenv *clenv)
     if (clenv->index != NULL)
        free(clenv->index);
 
+    /* XXX for now */
+    if (clenv->pgconn != NULL)
+               PQfinish(clenv->pgconn);
+
     pthread_mutex_lock(&clenv_lock);
     pool_free((pnode_t *)clenv);
     pthread_mutex_unlock(&clenv_lock);
@@ -1111,9 +1125,9 @@ do_buildindex(clientenv *clenv)
            clenv->size = 0;
            for (i = 0; i < rows; i++) {
                mm_strcpy(mnode[i].id, PQgetvalue(pgres, i, 0));
+               mnode[i].size = atol(PQgetvalue(pgres, i, 1));
                (void) snprintf(mnode[i].file, 255, "%s/%s", CONF.MAIL_DIR,
-                       PQgetvalue(pgres, i, 1));
-               mnode[i].size = atol(PQgetvalue(pgres, i, 2));
+                       PQgetvalue(pgres, i, 2));
                clenv->size += mnode[i].size;
            }
            clenv->index = mnode;
index 4705c35..9f82d0e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: mmsmtpd.c,v 1.75.4.2 2007/03/15 17:01:56 mmondor Exp $ */
+/* $Id: mmsmtpd.c,v 1.75.4.3 2007/03/15 20:46:28 mmondor Exp $ */
 
 /*
  * Copyright (C) 2001-2007, Matthew Mondor
@@ -82,7 +82,7 @@
 
 MMCOPYRIGHT("@(#) Copyright (c) 2001-2007\n\
 \tMatthew Mondor. All rights reserved.\n");
-MMRCSID("$Id: mmsmtpd.c,v 1.75.4.2 2007/03/15 17:01:56 mmondor Exp $");
+MMRCSID("$Id: mmsmtpd.c,v 1.75.4.3 2007/03/15 20:46:28 mmondor Exp $");
 
 
 
@@ -1124,8 +1124,13 @@ clientenv_constructor(pnode_t *pn)
 
     mmstat_init(&clenv->vstat, TRUE, TRUE);
     mmstat_init(&clenv->pstat, TRUE, FALSE);
-    if ((clenv->pgconn = PQconnectdb(CONF.DB_INFO)) == NULL)
+
+    /* XXX Performance enhancement but requires thread safety
+    if ((clenv->pgconn = PQconnectdb(CONF.DB_INFO)) == NULL) {
+       mmsyslog(LOGLEVEL, 0, "PQconnectdb()");
        return FALSE;
+    }
+    */
 
     return TRUE;
 }
@@ -1133,12 +1138,14 @@ clientenv_constructor(pnode_t *pn)
 static void
 clientenv_destructor(pnode_t *pn)
 {
+    /* XXX Performance enhancement but requires thread safety
     clientenv  *clenv = (clientenv *)pn;
 
     if (clenv->pgconn != NULL) {
        PQfinish(clenv->pgconn);
        clenv->pgconn = NULL;
     }
+    */
 }
 
 
@@ -1152,6 +1159,9 @@ alloc_clientenv(void)
     clenv = (clientenv *)pool_alloc(&clenv_pool, TRUE);
     pthread_mutex_unlock(&clenv_lock);
 
+    /* XXX for now */
+    clenv->pgconn = PQconnectdb(CONF.DB_INFO);
+
     return (clenv);
 }
 
@@ -1182,6 +1192,10 @@ free_clientenv(clientenv *clenv)
        mmstrfree(clenv->from);
     empty_rcpts(&clenv->rcpt);
 
+    /* XXX for now */
+    if (clenv->pgconn != NULL)
+       PQfinish(clenv->pgconn);
+
     pthread_mutex_lock(&clenv_lock);
     pool_free((pnode_t *)clenv);
     pthread_mutex_unlock(&clenv_lock);