Happy new year
[pazpar2-moved-to-github.git] / src / connection.c
index 2c7c0c2..80d6ec0 100644 (file)
@@ -144,6 +144,7 @@ static struct connection *connection_create(struct client *cl,
                                             iochan_man_t iochan_man)
 {
     struct connection *co;
+    int ret;
 
     co = xmalloc(sizeof(*co));
 
@@ -159,9 +160,13 @@ static struct connection *connection_create(struct client *cl,
     co->operation_timeout = operation_timeout;
     co->session_timeout = session_timeout;
 
-    connection_connect(co, iochan_man);
-
+    ret = connection_connect(co, iochan_man);
     connection_use(1);
+    if (ret)
+    {   /* error */
+        connection_destroy(co);
+        co = 0;
+    }
     return co;
 }
 
@@ -241,10 +246,7 @@ static void non_block_events(struct connection *co)
     {
         struct client *cl = co->client;
         if (cl)
-        {
-            client_check_preferred_watch(cl);
             client_got_records(cl);
-        }
     }
 }
 
@@ -318,6 +320,11 @@ static void connection_handler(IOCHAN iochan, int event)
     }
 }
 
+void connection_release2(struct connection *co)
+{
+    co->client = 0;
+}
+
 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
 {
     ZOOM_options zoptions = ZOOM_options_create();
@@ -326,10 +333,13 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     const char *sru;
     const char *sru_version = 0;
     const char *value;
+    int r = 0;
     WRBUF w;
 
-    struct session_database *sdb = client_get_database(con->client);
+    struct client *cl = con->client;
+    struct session_database *sdb = client_get_database(cl);
     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
+    struct session *se = client_get_session(cl);
     const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
     const char *redis = session_setting_oneval(sdb, PZ_REDIS);
 
@@ -394,7 +404,8 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
 
     if (!(con->link = ZOOM_connection_create(zoptions)))
     {
-        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
+        session_log(se, YLOG_WARN, "%s: ZOOM_connection_create failed",
+                    client_get_id(cl));
         ZOOM_options_destroy(zoptions);
         return -1;
     }
@@ -416,12 +427,22 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     con->state = Conn_Connecting;
     iochan_settimeout(con->iochan, con->operation_timeout);
     iochan_setdata(con->iochan, con);
-    iochan_add(iochan_man, con->iochan);
-
-    client_set_state(con->client, Client_Connecting);
+    if (iochan_add(iochan_man, con->iochan, 20))
+    {
+        session_log(se, YLOG_WARN, "%s: out of connections", client_get_id(cl));
+        iochan_destroy(con->iochan);
+        con->iochan = 0;
+        ZOOM_connection_destroy(con->link);
+        con->link = 0;
+        r = -1;
+    }
+    else
+    {
+        client_set_state(cl, Client_Connecting);
+    }
     ZOOM_options_destroy(zoptions);
     wrbuf_destroy(w);
-    return 0;
+    return r;
 }
 
 // Ensure that client has a connection associated
@@ -445,6 +466,8 @@ int client_prep_connection(struct client *cl,
             client_get_id(cl), url);
 
     co = client_get_connection(cl);
+    if (co)
+        return 2;
     if (!co)
     {
         co = connection_create(cl, url, zproxy,