24 #include <yazpp/timestat.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
32 namespace yf = mp::filter;
48 void process(metaproxy_1::Package & package);
63 yf::Limit::Limit() : m_p(new
Impl)
71 void yf::Limit::configure(
const xmlNode *xmlnode,
bool test_only,
74 m_p->configure(xmlnode);
77 void yf::Limit::process(mp::Package &package)
const
79 m_p->process(package);
85 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
86 m_max_record_retrieve(0)
90 yf::Limit::Impl::~Impl()
94 void yf::Limit::Impl::configure(
const xmlNode *ptr)
96 for (ptr = ptr->children; ptr; ptr = ptr->next)
98 if (ptr->type != XML_ELEMENT_NODE)
100 if (!strcmp((
const char *) ptr->name,
"limit"))
102 const struct _xmlAttr *attr;
103 for (attr = ptr->properties; attr; attr = attr->next)
105 if (!strcmp((
const char *) attr->name,
"bandwidth"))
106 m_bw_max = mp::xml::get_int(attr->children, 0);
107 else if (!strcmp((
const char *) attr->name,
"pdu"))
108 m_pdu_max = mp::xml::get_int(attr->children, 0);
109 else if (!strcmp((
const char *) attr->name,
"search"))
110 m_search_max = mp::xml::get_int(attr->children, 0);
111 else if (!strcmp((
const char *) attr->name,
"retrieve"))
112 m_max_record_retrieve =
113 mp::xml::get_int(attr->children, 0);
115 throw mp::filter::FilterException(
116 "Bad attribute " + std::string((
const char *)
122 throw mp::filter::FilterException(
"Bad element "
123 + std::string((
const char *)
129 void yf::Limit::Impl::process(mp::Package &package)
133 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
135 yf::Limit::Ses *ses = 0;
137 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
138 m_sessions.find(package.session());
139 if (it != m_sessions.end())
143 ses =
new yf::Limit::Ses;
144 m_sessions[package.session()] = ses;
148 Z_GDU *gdu = package.request().get();
149 if (gdu && gdu->which == Z_GDU_Z3950)
151 sz += package.request().get_size();
153 Z_APDU *apdu = gdu->u.z3950;
154 if (apdu->which == Z_APDU_searchRequest)
155 ses->search_stat.add_bytes(1);
156 if (m_max_record_retrieve)
158 if (apdu->which == Z_APDU_presentRequest)
160 Z_PresentRequest *pr = apdu->u.presentRequest;
161 if (pr->numberOfRecordsRequested &&
162 *pr->numberOfRecordsRequested > m_max_record_retrieve)
163 *pr->numberOfRecordsRequested = m_max_record_retrieve;
171 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
173 yf::Limit::Ses *ses = 0;
175 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
176 m_sessions.find(package.session());
177 if (it != m_sessions.end())
181 ses =
new yf::Limit::Ses;
182 m_sessions[package.session()] = ses;
185 sz += package.response().get_size();
187 ses->bw_stat.add_bytes(sz);
188 ses->pdu_stat.add_bytes(1);
190 int bw_total = ses->bw_stat.get_total();
191 int pdu_total = ses->pdu_stat.get_total();
192 int search_total = ses->search_stat.get_total();
195 reduce += search_total / m_search_max;
197 reduce += (bw_total/m_bw_max);
200 if (pdu_total > m_pdu_max)
202 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
203 reduce = (reduce > nreduce) ? reduce : nreduce;
206 if (package.session().is_closed())
208 m_sessions.erase(package.session());
214 std::ostringstream os;
220 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
222 Sleep(reduce * 1000);
232 return new mp::filter::Limit;
struct metaproxy_1_filter_struct metaproxy_1_filter_limit
static mp::filter::Base * filter_creator()