#include "config.hpp"
+#if HAVE_GETRLIMIT
+#include <sys/resource.h>
+#endif
#include <sstream>
#include <iomanip>
#include <metaproxy/util.hpp>
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<Port> m_ports;
int m_listen_duration;
int m_session_timeout;
int m_connect_max;
+ std::list<IP_Pattern> http_req_max;
std::string m_msg_config;
std::string m_stat_req;
yazpp_1::SocketManager mySocketManager;
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:
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:
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;
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<IP_Pattern>::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);
}
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;
}
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;
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;
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; i<m_p->m_ports.size(); i++)
{
+ 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);
{
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);