resolver thread checks for dead session PAZ-879
[pazpar2-moved-to-github.git] / src / connection.c
index d3eaeef..43cdc14 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2012 Index Data
+   Copyright (C) 2006-2013 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -257,8 +257,7 @@ static void non_block_events(struct connection *co)
             client_search_response(cl);
             break;
         case ZOOM_EVENT_RECV_RECORD:
-            client_record_response(cl);
-            got_records = 1;
+            client_record_response(cl, &got_records);
             break;
         default:
             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
@@ -281,20 +280,7 @@ void connection_continue(struct connection *co)
 {
     int r = ZOOM_connection_exec_task(co->link);
     if (!r)
-    {
-        const char *error, *addinfo;
-        int err;
-        if ((err = ZOOM_connection_error(co->link, &error, &addinfo)))
-        {
-            if (co->client)
-            {
-                yaz_log(YLOG_LOG, "Error %s from %s",
-                        error, client_get_id(co->client));
-                client_set_diagnostic(co->client, err, error, addinfo);
-                client_set_state_nb(co->client, Client_Error);
-            }
-        }
-    }
+        non_block_events(co);
     else
     {
         iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
@@ -367,36 +353,37 @@ void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
 {
     struct connection *con;
 
-start:
     yaz_mutex_enter(host->mutex);
     con = host->connections;
     while (con)
     {
         if (con->state == Conn_Closed)
         {
-            if (!host->ipport) /* unresolved */
+            if (!host->ipport || !con->client) /* unresolved or no client */
             {
                 remove_connection_from_host(con);
                 yaz_mutex_leave(host->mutex);
                 connection_destroy(con);
-                goto start;
-                /* start all over .. at some point it will be NULL */
-            }
-            else if (!con->client)
-            {
-                remove_connection_from_host(con);
-                yaz_mutex_leave(host->mutex);
-                connection_destroy(con);
-                /* start all over .. at some point it will be NULL */
-                goto start;
             }
             else
             {
-                yaz_mutex_leave(host->mutex);
-                connection_connect(con, iochan_man);
-                client_start_search(con->client);
-                goto start;
+                struct session_database *sdb = client_get_database(con->client);
+                if (sdb)
+                {
+                    yaz_mutex_leave(host->mutex);
+                    connection_connect(con, iochan_man);
+                    client_start_search(con->client);
+                }
+                else
+                {
+                    remove_connection_from_host(con);
+                    yaz_mutex_leave(host->mutex);
+                    connection_destroy(con);
+                }
             }
+            /* start all over .. at some point it will be NULL */
+            yaz_mutex_enter(host->mutex);
+            con = host->connections;
         }
         else
         {
@@ -463,13 +450,13 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
         return -1;
     }
 
-    if (sru && *sru)
+    if (sru && *sru && !strstr(host->url, "://"))
     {
-        char http_hostport[512];
-        strcpy(http_hostport, "http://");
-        strcat(http_hostport, host->url);
-        yaz_log(YLOG_LOG, "SRU connect to : %s", http_hostport);
-        ZOOM_connection_connect(con->link, http_hostport, 0);
+        WRBUF w = wrbuf_alloc();
+        wrbuf_puts(w, "http://");
+        wrbuf_puts(w, host->url);
+        ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
+        wrbuf_destroy(w);
     }
     else
     {
@@ -515,7 +502,6 @@ int client_prep_connection(struct client *cl,
         return 0;
 
     co = client_get_connection(cl);
-
     if (co)
     {
         assert(co->host);
@@ -523,7 +509,6 @@ int client_prep_connection(struct client *cl,
         {
             return 2;
         }
-        client_incref(cl);
         connection_release(co);
         co = 0;
     }