217void yf::FrontendNet::ThreadPoolPackage::result(
const char *t_info)
219 m_assoc_child->m_no_requests--;
221 yazpp_1::GDU *gdu = &m_package->response();
226 m_assoc_child->send_GDU(gdu->get(), &len);
228 yaz_timing_stop(timer);
229 double duration = yaz_timing_get_real(timer);
232 while (
m_p->m_duration_lim[ent] != 0.0 && duration >
m_p->m_duration_lim[ent])
234 m_p->m_duration_freq[ent]++;
236 m_p->m_duration_total += duration;
238 if (
m_p->m_duration_max < duration)
239 m_p->m_duration_max = duration;
241 if (
m_p->m_duration_min == 0.0 ||
m_p->m_duration_min > duration)
242 m_p->m_duration_min = duration;
244 if (
m_p->m_msg_config.length())
246 Z_GDU *z_gdu = gdu->get();
248 std::ostringstream os;
249 os <<
m_p->m_msg_config <<
" "
251 << std::fixed << std::setprecision (6) << duration <<
" ";
258 yaz_log(YLOG_LOG,
"%s %s", os.str().c_str(), t_info);
261 else if (!m_package->session().is_closed())
265 yazpp_1::GDU *gdu_req = &m_package->request();
266 Z_GDU *z_gdu = gdu_req->get();
267 if (z_gdu && z_gdu->which == Z_GDU_Z3950)
272 Z_APDU *apdu_response = odr.create_close(
273 z_gdu->u.z3950, Z_Close_systemProblem,
274 "unhandled Z39.50 request");
276 m_assoc_child->send_Z_PDU(apdu_response, &len);
278 else if (z_gdu && z_gdu->which == Z_GDU_HTTP_Request)
284 = odr.create_HTTP_Response(m_package->session(),
285 z_gdu->u.HTTP_Request, 500);
286 m_assoc_child->send_GDU(zgdu_res, &len);
288 m_package->session().close();
291 if (m_assoc_child->m_no_requests == 0 && m_package->session().is_closed())
293 m_assoc_child->close();
306yf::FrontendNet::ZAssocChild::ZAssocChild(
307 yazpp_1::IPDU_Observable *PDU_Observable,
309 const mp::Package *package,
311 yazpp_1::LimitConnect &limit_http_req,
312 const char *peername)
313 : Z_Assoc(PDU_Observable),
m_p(rep), m_limit_http_req(limit_http_req)
322 addr.append(peername);
324 addr.append(port->
port);
327 int session_timeout = 300;
331 if (mp::util::match_ip(it->pattern, peername))
334 yaz_log(YLOG_LOG,
"timeout pattern=%s ip=%s value=%d",
336 peername, it->value);
337 session_timeout = it->value;
341 timeout(session_timeout);
355void yf::FrontendNet::ZAssocChild::report(Z_HTTP_Request *hreq)
359 Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
361 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
365 int number_total = 0;
367 for (i = 0;
m_p->m_duration_lim[i] != 0.0; i++)
368 number_total +=
m_p->m_duration_freq[i];
369 number_total +=
m_p->m_duration_freq[i];
371 wrbuf_puts(w,
"<?xml version=\"1.0\"?>\n");
372 wrbuf_puts(w,
"<frontend_net>\n");
373 wrbuf_printf(w,
" <responses frequency=\"%d\">\n", number_total);
374 for (i = 0;
m_p->m_duration_lim[i] != 0.0; i++)
376 if (
m_p->m_duration_freq[i] > 0)
378 w,
" <response duration_start=\"%f\" "
379 "duration_end=\"%f\" frequency=\"%d\"/>\n",
380 i > 0 ?
m_p->m_duration_lim[i - 1] : 0.0,
381 m_p->m_duration_lim[i],
m_p->m_duration_freq[i]);
384 if (
m_p->m_duration_freq[i] > 0)
386 w,
" <response duration_start=\"%f\" frequency=\"%d\"/>\n",
387 m_p->m_duration_lim[i - 1],
m_p->m_duration_freq[i]);
389 if (
m_p->m_duration_max != 0.0)
391 w,
" <response duration_max=\"%f\"/>\n",
392 m_p->m_duration_max);
393 if (
m_p->m_duration_min != 0.0)
395 w,
" <response duration_min=\"%f\"/>\n",
396 m_p->m_duration_min);
397 if (
m_p->m_duration_total != 0.0)
399 w,
" <response duration_average=\"%f\"/>\n",
400 m_p->m_duration_total / number_total);
402 wrbuf_puts(w,
" </responses>\n");
406 m_thread_pool_observer->get_thread_info(thread_busy, thread_total);
408 wrbuf_printf(w,
" <thread_info busy=\"%d\" total=\"%d\"/>\n",
409 thread_busy, thread_total);
411 wrbuf_malloc_info(w);
415 if (nmem_get_status(buf,
sizeof(buf) - 1) == 0)
418 wrbuf_puts(w,
"</frontend_net>\n");
420 hres->content_len = w.len();
421 hres->content_buf = (
char *) w.buf();
424 send_GDU(gdu_res, &len);
427void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu,
int len)
431 mp::Package *p =
new mp::Package(m_session, m_origin);
433 if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
435 Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
437 const char *f = z_HTTP_header_lookup(hreq->headers,
"X-Forwarded-For");
439 p->origin().set_tcpip_address(std::string(f), m_session.id());
441 if (
m_p->m_stat_req.length()
442 && !strcmp(hreq->path,
m_p->m_stat_req.c_str()))
451 p->copy_route(*m_package);
452 p->request() = yazpp_1::GDU(z_pdu);
454 if (
m_p->m_msg_config.length())
458 std::ostringstream os;
459 os <<
m_p->m_msg_config <<
" "
463 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
466 if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
468 Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
469 std::string peername = p->origin().get_address();
471 m_limit_http_req.cleanup(
false);
472 int con_sz = m_limit_http_req.get_total(peername.c_str());
473 std::list<IP_Pattern>::const_iterator it =
m_p->http_req_max.begin();
474 for (; it !=
m_p->http_req_max.end(); it++)
476 if (mp::util::match_ip(it->pattern, peername))
478 if (it->verbose > 1 ||
479 (it->value && con_sz >= it->value && it->verbose > 0))
480 yaz_log(YLOG_LOG,
"http-req-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername.c_str(), con_sz, it->value);
481 if (con_sz < it->value)
484 Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 500);
486 send_GDU(gdu_res, &len);
492 m_limit_http_req.add_connect(peername.c_str());
495 m_thread_pool_observer->put(tp);
498void yf::FrontendNet::ZAssocChild::failNotify()
501 if (m_session.is_closed())
503 if (m_no_requests == 0)
511 mp::Package *p =
new mp::Package(m_session, m_origin);
514 p->copy_route(*m_package);
515 m_thread_pool_observer->cleanup(tp, &m_session);
516 m_thread_pool_observer->put(tp);
551yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocServer::sessionNotify(
552 yazpp_1::IPDU_Observable *the_PDU_Observable,
int fd)
555 const char *peername = the_PDU_Observable->getpeername();
557 peername =
"unknown";
560 const char *cp = strchr(peername,
':');
566 int total_sz =
m_p->m_peerStat.get(peername);
567 std::list<IP_Pattern>::const_iterator it =
m_p->connect_total.begin();
568 for (; it !=
m_p->connect_total.end(); it++)
570 if (mp::util::match_ip(it->pattern, peername))
572 if (it->verbose > 1 ||
573 (it->value && total_sz >= it->value && it->verbose > 0))
574 yaz_log(YLOG_LOG,
"connect-total pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, total_sz, it->value);
575 if (total_sz < it->value)
580 limit_connect.cleanup(
false);
581 int con_sz = limit_connect.get_total(peername);
582 it =
m_p->connect_max.begin();
583 for (; it !=
m_p->connect_max.end(); it++)
585 if (mp::util::match_ip(it->pattern, peername))
587 if (it->verbose > 1 ||
588 (it->value && con_sz >= it->value && it->verbose > 0))
589 yaz_log(YLOG_LOG,
"connect-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, con_sz, it->value);
590 if (con_sz < it->value)
595 limit_connect.add_connect(peername);
598 m_thread_pool_observer,
599 m_package, m_port,
m_p, limit_http_req,
628yf::FrontendNet::Rep::Rep()
630 m_max_threads = m_no_threads = 5;
632 m_listen_duration = 0;
635 for (i = 0; i < 22; i++)
636 m_duration_freq[i] = 0;
637 m_duration_lim[0] = 0.000001;
638 m_duration_lim[1] = 0.00001;
639 m_duration_lim[2] = 0.0001;
640 m_duration_lim[3] = 0.001;
641 m_duration_lim[4] = 0.01;
642 m_duration_lim[5] = 0.1;
643 m_duration_lim[6] = 0.2;
644 m_duration_lim[7] = 0.3;
645 m_duration_lim[8] = 0.5;
646 m_duration_lim[9] = 1.0;
647 m_duration_lim[10] = 1.5;
648 m_duration_lim[11] = 2.0;
649 m_duration_lim[12] = 3.0;
650 m_duration_lim[13] = 4.0;
651 m_duration_lim[14] = 5.0;
652 m_duration_lim[15] = 6.0;
653 m_duration_lim[16] = 8.0;
654 m_duration_lim[17] = 10.0;
655 m_duration_lim[18] = 15.0;
656 m_duration_lim[19] = 20.0;
657 m_duration_lim[20] = 30.0;
658 m_duration_lim[21] = 0.0;
659 m_duration_max = 0.0;
660 m_duration_min = 0.0;
661 m_duration_total = 0.0;
702yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
703 yazpp_1::ISocketObservable *obs,
705 m_obs(obs), m_pipe(9123), m_timeout(false)
708 obs->maskObserver(
this, yazpp_1::SOCKET_OBSERVE_READ);
709 obs->timeoutObserver(
this, duration);
718void yf::FrontendNet::process(mp::Package &package)
const
725 if (
m_p->m_listen_duration)
727 m_p->m_listen_duration);
734 for (i = 0; i<
m_p->m_ports.size(); i++)
736 yaz_log(YLOG_LOG,
"listening on %s",
m_p->m_ports[i].port.c_str());
737 m_p->az[i]->set_package(&package);
738 m_p->az[i]->set_thread_pool(tp);
740 while (
m_p->mySocketManager.processEvent() > 0)
742 if (
m_p->m_stop_signo == SIGTERM)
744 yaz_log(YLOG_LOG,
"metaproxy received SIGTERM");
748 for (i = 0; i <
m_p->m_ports.size(); i++)
750 m_p->pdu[i]->shutdown();
751 m_p->az[i]->server(
"");
758 if (
m_p->m_stop_signo == SIGUSR1)
760 yaz_log(YLOG_LOG,
"metaproxy received SIGUSR1");
761 m_p->m_stop_signo = 0;
765 for (i = 0; i <
m_p->m_ports.size(); i++)
766 m_p->az[i]->server(
"");
771 int no =
m_p->mySocketManager.getNumberOfObservers();
781void yf::FrontendNet::configure(
const xmlNode * ptr,
bool test_only,
784 if (!ptr || !ptr->children)
786 throw yf::FilterException(
"No ports for Frontend");
788 std::vector<Port> ports;
789 for (ptr = ptr->children; ptr; ptr = ptr->next)
791 if (ptr->type != XML_ELEMENT_NODE)
793 if (!strcmp((
const char *) ptr->name,
"port"))
797 const char *names[5] = {
"route",
"max_recv_bytes",
"port",
799 std::string values[4];
801 mp::xml::parse_attr(ptr, names, values);
802 port.
route = values[0];
803 if (values[1].length() > 0)
807 if (values[2].length() > 0)
808 port.
port = values[2];
810 port.
port = mp::xml::get_text(ptr);
812 ports.push_back(port);
814 else if (!strcmp((
const char *) ptr->name,
"threads"))
816 std::string threads_str = mp::xml::get_text(ptr);
817 int threads = atoi(threads_str.c_str());
819 throw yf::FilterException(
"Bad value for threads: "
821 m_p->m_no_threads = threads;
823 else if (!strcmp((
const char *) ptr->name,
"max-threads"))
825 std::string threads_str = mp::xml::get_text(ptr);
826 int threads = atoi(threads_str.c_str());
828 throw yf::FilterException(
"Bad value for max-threads: "
830 m_p->m_max_threads = threads;
832 else if (!strcmp((
const char *) ptr->name,
"stack-size"))
834 std::string sz_str = mp::xml::get_text(ptr);
835 int sz = atoi(sz_str.c_str());
837 throw yf::FilterException(
"Bad value for stack-size: "
839 m_p->m_stack_size = sz * 1024;
841 else if (!strcmp((
const char *) ptr->name,
"timeout"))
843 const char *names[3] = {
"ip",
"verbose", 0};
844 std::string values[2];
846 mp::xml::parse_attr(ptr, names, values);
848 m.
value = mp::xml::get_int(ptr, 0);
850 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
851 m_p->session_timeout.push_back(m);
853 else if (!strcmp((
const char *) ptr->name,
"connect-max"))
855 const char *names[3] = {
"ip",
"verbose", 0};
856 std::string values[2];
858 mp::xml::parse_attr(ptr, names, values);
860 m.
value = mp::xml::get_int(ptr, INT_MAX);
862 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
863 m_p->connect_max.push_back(m);
865 else if (!strcmp((
const char *) ptr->name,
"connect-total"))
867 const char *names[3] = {
"ip",
"verbose", 0};
868 std::string values[2];
870 mp::xml::parse_attr(ptr, names, values);
872 m.
value = mp::xml::get_int(ptr, INT_MAX);
874 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
875 m_p->connect_total.push_back(m);
877 else if (!strcmp((
const char *) ptr->name,
"http-req-max"))
879 const char *names[3] = {
"ip",
"verbose", 0};
880 std::string values[2];
882 mp::xml::parse_attr(ptr, names, values);
884 m.
value = mp::xml::get_int(ptr, INT_MAX);
886 m.
verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
887 m_p->http_req_max.push_back(m);
889 else if (!strcmp((
const char *) ptr->name,
"message"))
891 m_p->m_msg_config = mp::xml::get_text(ptr);
893 else if (!strcmp((
const char *) ptr->name,
"stat-req"))
895 m_p->m_stat_req = mp::xml::get_text(ptr);
899 throw yf::FilterException(
"Bad element "
900 + std::string((
const char *)
904 if (
m_p->m_msg_config.length() > 0 &&
m_p->m_stat_req.length() == 0)
906 m_p->m_stat_req =
"/fn_stat";
930void yf::FrontendNet::set_ports(std::vector<Port> &ports)
932 m_p->m_ports = ports;
934 m_p->az =
new yf::FrontendNet::ZAssocServer *[
m_p->m_ports.size()];
935 m_p->pdu =
new yazpp_1::PDU_Assoc *[
m_p->m_ports.size()];
939 for (i = 0; i <
m_p->m_ports.size(); i++)
941 for (i = 0; i <
m_p->m_ports.size(); i++)
944 yazpp_1::PDU_Assoc *as =
new yazpp_1::PDU_Assoc(&
m_p->mySocketManager);
946 if (
m_p->m_ports[i].cert_fname.length())
947 as->set_cert_fname(
m_p->m_ports[i].cert_fname.c_str());
950 m_p->az[i] =
new yf::FrontendNet::ZAssocServer(
951 as, &
m_p->m_ports[i],
m_p.get());
952 if (
m_p->az[i]->server(
m_p->m_ports[i].port.c_str()))
954 throw yf::FilterException(
"Unable to bind to address "
955 + std::string(
m_p->m_ports[i].port));
957 COMSTACK cs = as->get_comstack();
959 if (cs &&
m_p->m_ports[i].max_recv_bytes)
960 cs_set_max_recv_bytes(cs,
m_p->m_ports[i].max_recv_bytes);