X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_frontend_net.cpp;h=2e2ada228645ba4bf1371bf8f92eaa870bc15bfa;hb=1fe3038c15740cf21a2eb7b00edad8d216384c2e;hp=7ef28c89a84a9fe18134c25c67f450da5e7ca63b;hpb=8b5b1897bd1223b6c8608685e0cceb82b47e5aed;p=metaproxy-moved-to-github.git diff --git a/src/filter_frontend_net.cpp b/src/filter_frontend_net.cpp index 7ef28c8..2e2ada2 100644 --- a/src/filter_frontend_net.cpp +++ b/src/filter_frontend_net.cpp @@ -18,6 +18,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "config.hpp" +#if HAVE_GETRLIMIT +#include +#endif #include #include #include @@ -55,10 +58,13 @@ namespace metaproxy_1 { friend class FrontendNet; int m_no_threads; + int m_max_threads; + int m_stack_size; std::vector m_ports; int m_listen_duration; int m_session_timeout; int m_connect_max; + int m_http_req_max; std::string m_msg_config; std::string m_stat_req; yazpp_1::SocketManager mySocketManager; @@ -91,7 +97,8 @@ namespace metaproxy_1 { mp::ThreadPoolSocketObserver *m_thread_pool_observer, const mp::Package *package, Port *port, - Rep *rep); + Rep *rep, + yazpp_1::LimitConnect &limit_connect); int m_no_requests; Port *m_port; private: @@ -110,6 +117,7 @@ namespace metaproxy_1 { bool m_delete_flag; const mp::Package *m_package; Rep *m_p; + yazpp_1::LimitConnect &m_limit_connect; }; class FrontendNet::ThreadPoolPackage : public mp::IThreadPoolMsg { public: @@ -267,8 +275,9 @@ yf::FrontendNet::ZAssocChild::ZAssocChild( yazpp_1::IPDU_Observable *PDU_Observable, mp::ThreadPoolSocketObserver *my_thread_pool, const mp::Package *package, - Port *port, Rep *rep) - : Z_Assoc(PDU_Observable), m_p(rep) + Port *port, Rep *rep, + yazpp_1::LimitConnect &limit_connect) + : Z_Assoc(PDU_Observable), m_p(rep), m_limit_connect(limit_connect) { m_thread_pool_observer = my_thread_pool; m_no_requests = 0; @@ -387,6 +396,20 @@ void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len) report(hreq); return; } + std::string peername = p->origin().get_address(); + + m_limit_connect.add_connect(peername.c_str()); + m_limit_connect.cleanup(false); + int con_sz = m_limit_connect.get_total(peername.c_str()); + + if (m_p->m_http_req_max && con_sz >= m_p->m_http_req_max) + { + mp::odr o; + Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 500); + int len; + send_GDU(gdu_res, &len); + return; + } } ThreadPoolPackage *tp = new ThreadPoolPackage(p, this, m_p); @@ -476,7 +499,7 @@ yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocServer::sessionNotify( } ZAssocChild *my = new ZAssocChild(the_PDU_Observable, m_thread_pool_observer, - m_package, m_port, m_p); + m_package, m_port, m_p, limit_connect); return my; } @@ -506,7 +529,8 @@ yf::FrontendNet::FrontendNet() : m_p(new Rep) yf::FrontendNet::Rep::Rep() { - m_no_threads = 5; + m_max_threads = m_no_threads = 5; + m_stack_size = 0; m_listen_duration = 0; m_session_timeout = 300; // 5 minutes m_connect_max = 0; @@ -564,6 +588,16 @@ void yf::FrontendNet::stop(int signo) const m_p->m_stop_signo = signo; } +void yf::FrontendNet::start() const +{ +#if HAVE_GETRLIMIT + struct rlimit limit_data; + getrlimit(RLIMIT_NOFILE, &limit_data); + yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld", + (long) limit_data.rlim_cur, (long) limit_data.rlim_max); +#endif +} + bool yf::FrontendNet::My_Timer_Thread::timeout() { return m_timeout; @@ -596,7 +630,9 @@ void yf::FrontendNet::process(mp::Package &package) const tt = new My_Timer_Thread(&m_p->mySocketManager, m_p->m_listen_duration); - ThreadPoolSocketObserver tp(&m_p->mySocketManager, m_p->m_no_threads); + ThreadPoolSocketObserver tp(&m_p->mySocketManager, m_p->m_no_threads, + m_p->m_max_threads, + m_p->m_stack_size); for (i = 0; im_ports.size(); i++) { @@ -685,6 +721,24 @@ void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only, + threads_str); m_p->m_no_threads = threads; } + else if (!strcmp((const char *) ptr->name, "max-threads")) + { + std::string threads_str = mp::xml::get_text(ptr); + int threads = atoi(threads_str.c_str()); + if (threads < 1) + throw yf::FilterException("Bad value for max-threads: " + + threads_str); + m_p->m_max_threads = threads; + } + else if (!strcmp((const char *) ptr->name, "stack-size")) + { + std::string sz_str = mp::xml::get_text(ptr); + int sz = atoi(sz_str.c_str()); + if (sz < 0) + throw yf::FilterException("Bad value for stack-size: " + + sz_str); + m_p->m_stack_size = sz * 1024; + } else if (!strcmp((const char *) ptr->name, "timeout")) { std::string timeout_str = mp::xml::get_text(ptr); @@ -698,6 +752,10 @@ void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only, { m_p->m_connect_max = mp::xml::get_int(ptr, 0); } + else if (!strcmp((const char *) ptr->name, "http-req-max")) + { + m_p->m_http_req_max = mp::xml::get_int(ptr, 0); + } else if (!strcmp((const char *) ptr->name, "message")) { m_p->m_msg_config = mp::xml::get_text(ptr);