From 09ce8a48eb9d2947cd019f9b8adb6513941c2211 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 29 May 2008 21:38:59 +0200 Subject: [PATCH] Allow x-max-sockets parameter for SRU --- src/filter_sru_to_z3950.cpp | 4 ++++ src/filter_z3950_client.cpp | 26 ++++++++++++++++++-------- src/origin.cpp | 10 +++++++++- src/origin.hpp | 6 ++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp index 91191bb..328188d 100644 --- a/src/filter_sru_to_z3950.cpp +++ b/src/filter_sru_to_z3950.cpp @@ -217,6 +217,10 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req) { zurl = std::string(arg->value); } + else if (!strcmp(arg->name, "x-max-sockets")) + { + package.origin().set_max_sockets(atoi(arg->value)); + } // filter acts as sink for SRU explain requests diff --git a/src/filter_z3950_client.cpp b/src/filter_z3950_client.cpp index d0d0442..6b27e7e 100644 --- a/src/filter_z3950_client.cpp +++ b/src/filter_z3950_client.cpp @@ -203,11 +203,18 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) package.move(); return 0; } + + int max_sockets = package.origin().get_max_sockets(); + if (max_sockets == 0) + max_sockets = m_max_sockets; + + std::string host; + it = m_clients.find(package.session()); if (it != m_clients.end()) { it->second->m_queue_len++; - while(true) + while (true) { #if 0 // double init .. NOT working yet @@ -249,7 +256,7 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) target = m_default_target; std::list vhosts; mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo, - vhosts); + vhosts); size_t no_vhosts = vhosts.size(); if (no_vhosts == 1) { @@ -282,17 +289,18 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) return 0; } } - + std::list dblist; - std::string host; mp::util::split_zurl(target, host, dblist); if (dblist.size()) { ; // z3950_client: Databases in vhost ignored } + + // see if we have reached max number of clients (max-sockets) - while (m_max_sockets) + while (max_sockets) { int number = 0; it = m_clients.begin(); @@ -302,16 +310,17 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) if (!strcmp(as->get_hostname(), host.c_str())) number++; } - if (number < m_max_sockets) + yaz_log(YLOG_LOG, "Found %d connections for %s", number, host.c_str()); + if (number < max_sockets) break; boost::xtime xt; xtime_get(&xt, boost::TIME_UTC); - + xt.sec += 15; if (!m_cond_session_ready.timed_wait(lock, xt)) { mp::odr odr; - + package.response() = odr.create_initResponse( apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions"); package.session().close(); @@ -409,6 +418,7 @@ void yf::Z3950Client::Rep::release_assoc(Package &package) delete s; // then manager m_clients.erase(it); } + yaz_log(YLOG_LOG, "Notify all release_assoc"); m_cond_session_ready.notify_all(); } } diff --git a/src/origin.cpp b/src/origin.cpp index db29449..cf932d3 100644 --- a/src/origin.cpp +++ b/src/origin.cpp @@ -26,7 +26,7 @@ namespace mp = metaproxy_1; mp::Origin::Origin(std::string listen_host, unsigned int listen_port) : m_type(API), m_address(""), m_origin_id(0), - m_listen_host(listen_host), m_listen_port(listen_port) + m_listen_host(listen_host), m_listen_port(listen_port), m_max_sockets(0) { } @@ -50,7 +50,15 @@ unsigned int & mp::Origin::listen_port() return m_listen_port; }; +void mp::Origin::set_max_sockets(int max_sockets) +{ + m_max_sockets = max_sockets; +} +int mp::Origin::get_max_sockets() +{ + return m_max_sockets; +} void mp::Origin::set_tcpip_address(std::string addr, unsigned long s) { diff --git a/src/origin.hpp b/src/origin.hpp index 3acfc18..4e735f8 100644 --- a/src/origin.hpp +++ b/src/origin.hpp @@ -51,6 +51,11 @@ namespace metaproxy_1 { /// set client IP info - left val in assignment void set_tcpip_address(std::string addr, unsigned long id); + /// set max sockets (for outgoing connections to a given target) + void set_max_sockets(int max_sockets); + + /// set max sockets (for outgoing connections to a given target) + int get_max_sockets(); private: friend std::ostream& std::operator<<(std::ostream& os, metaproxy_1::Origin& o); @@ -64,6 +69,7 @@ namespace metaproxy_1 { unsigned int m_origin_id; std::string m_listen_host; unsigned int m_listen_port; + int m_max_sockets; }; } -- 1.7.10.4