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");
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);
307 yazpp_1::IPDU_Observable *PDU_Observable,
309 const mp::Package *package,
311 yazpp_1::LimitConnect &limit_http_req,
312 const char *peername)
322 addr.append(peername);
324 addr.append(port->
port);
327 int session_timeout = 300;
329 std::list<IP_Pattern>::const_iterator it =
m_p->session_timeout.begin();
330 for (; it !=
m_p->session_timeout.end(); it++)
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);
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");
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);
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()))
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();
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);
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)
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)
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();
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";
932 m_p->m_ports = ports;
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());
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);