For sort, re-use non-idle clients
[pazpar2-moved-to-github.git] / src / session.c
index 425d6ca..78aad61 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2011 Index Data
+   Copyright (C) 2006-2012 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
@@ -523,13 +523,14 @@ static void select_targets_callback(struct session *se,
     {
         cl = client_create(db->database->id);
         client_set_database(cl, db);
-        client_set_session(cl, se);
 
         l = xmalloc(sizeof(*l));
         l->client = cl;
         l->next = se->clients_cached;
         se->clients_cached = l;
     }
+    /* set session always. If may be 0 if client is not active */
+    client_set_session(cl, se);
 
     l = xmalloc(sizeof(*l));
     l->client = cl;
@@ -550,6 +551,11 @@ static void session_reset_active_clients(struct session *se,
     while (l)
     {
         struct client_list *l_next = l->next;
+
+        client_lock(l->client); 
+        client_set_session(l->client, 0); /* mark client inactive */
+        client_unlock(l->client);
+
         xfree(l);
         l = l_next;
     }
@@ -643,10 +649,11 @@ void session_sort(struct session *se, const char *field, int increasing)
     {
         struct client *cl = l->client;
         struct timeval tval;
-        if (client_prep_connection(cl, se->service->z3950_operation_timeout,
-                                   se->service->z3950_session_timeout,
-                                   se->service->server->iochan_man,
-                                   &tval))
+        int ret = client_prep_connection(cl, se->service->z3950_operation_timeout,
+                                         se->service->z3950_session_timeout,
+                                         se->service->server->iochan_man,
+                                         &tval, 1);
+        if (ret)
             client_start_search(cl);
     }
     session_leave(se);
@@ -737,7 +744,7 @@ enum pazpar2_error_code session_search(struct session *se,
                 client_prep_connection(cl, se->service->z3950_operation_timeout,
                                        se->service->z3950_session_timeout,
                                        se->service->server->iochan_man,
-                                       &tval);
+                                       &tval, 0);
             if (parse_ret == 1 && r == 2)
             {
                 session_log(se, YLOG_LOG, "client REUSE %s", client_get_id(cl));