1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "filter_limit.hpp"
24 #include <yazpp/timestat.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
31 namespace mp = metaproxy_1;
32 namespace yf = mp::filter;
34 namespace metaproxy_1 {
38 yazpp_1::TimeStat bw_stat;
39 yazpp_1::TimeStat pdu_stat;
40 yazpp_1::TimeStat search_stat;
41 Ses() : bw_stat(60), pdu_stat(60), search_stat(60) {};
48 void process(metaproxy_1::Package & package);
49 void configure(const xmlNode * ptr);
51 boost::mutex m_session_mutex;
52 std::map<mp::Session,Limit::Ses *> m_sessions;
56 int m_max_record_retrieve;
61 // define Pimpl wrapper forwarding to Impl
63 yf::Limit::Limit() : m_p(new Impl)
68 { // must have a destructor because of boost::scoped_ptr
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);
83 // define Implementation stuff
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();
152 // we're getting a Z39.50 package
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 yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
216 Sleep(reduce * 1000);
224 static mp::filter::Base* filter_creator()
226 return new mp::filter::Limit;
230 struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
241 * c-file-style: "Stroustrup"
242 * indent-tabs-mode: nil
244 * vim: shiftwidth=4 tabstop=8 expandtab