X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_frontend_net.cpp;h=02638d6899142c17aaefaa50531f7a1c7b32a3fd;hb=4a46c6ad125dde75e1e35a09f8388f43eab5bb39;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..02638d6 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 @@ -51,14 +54,23 @@ namespace metaproxy_1 { std::string cert_fname; int max_recv_bytes; }; + class FrontendNet::IP_Pattern { + friend class Rep; + friend class FrontendNet; + std::string pattern; + int value; + }; class FrontendNet::Rep { 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; + std::list http_req_max; std::string m_msg_config; std::string m_stat_req; yazpp_1::SocketManager mySocketManager; @@ -91,7 +103,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 +123,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 +281,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 +402,25 @@ 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()); + std::list::const_iterator it = m_p->http_req_max.begin(); + for (; it != m_p->http_req_max.end(); it++) + { + if (mp::util::match_ip(it->pattern, peername)) + { + if (con_sz < it->value) + break; + 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 +510,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 +540,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 +599,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 +641,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 +732,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 +763,17 @@ 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")) + { + const char *names[2] = {"ip", 0}; + std::string values[1]; + + mp::xml::parse_attr(ptr, names, values); + IP_Pattern m; + m.value = mp::xml::get_int(ptr, 0); + m.pattern = values[0]; + m_p->http_req_max.push_back(m); + } else if (!strcmp((const char *) ptr->name, "message")) { m_p->m_msg_config = mp::xml::get_text(ptr);