1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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)
73 m_p->configure(xmlnode);
76 void yf::Limit::process(mp::Package &package) const
78 m_p->process(package);
82 // define Implementation stuff
84 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
85 m_max_record_retrieve(0)
89 yf::Limit::Impl::~Impl()
93 void yf::Limit::Impl::configure(const xmlNode *ptr)
95 for (ptr = ptr->children; ptr; ptr = ptr->next)
97 if (ptr->type != XML_ELEMENT_NODE)
99 if (!strcmp((const char *) ptr->name, "limit"))
101 const struct _xmlAttr *attr;
102 for (attr = ptr->properties; attr; attr = attr->next)
104 if (!strcmp((const char *) attr->name, "bandwidth"))
105 m_bw_max = mp::xml::get_int(attr->children, 0);
106 else if (!strcmp((const char *) attr->name, "pdu"))
107 m_pdu_max = mp::xml::get_int(attr->children, 0);
108 else if (!strcmp((const char *) attr->name, "search"))
109 m_search_max = mp::xml::get_int(attr->children, 0);
110 else if (!strcmp((const char *) attr->name, "retrieve"))
111 m_max_record_retrieve =
112 mp::xml::get_int(attr->children, 0);
114 throw mp::filter::FilterException(
115 "Bad attribute " + std::string((const char *)
121 throw mp::filter::FilterException("Bad element "
122 + std::string((const char *)
128 void yf::Limit::Impl::process(mp::Package &package)
132 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
134 yf::Limit::Ses *ses = 0;
136 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
137 m_sessions.find(package.session());
138 if (it != m_sessions.end())
142 ses = new yf::Limit::Ses;
143 m_sessions[package.session()] = ses;
147 Z_GDU *gdu = package.request().get();
148 if (gdu && gdu->which == Z_GDU_Z3950)
150 sz += package.request().get_size();
151 // we're getting a Z39.50 package
152 Z_APDU *apdu = gdu->u.z3950;
153 if (apdu->which == Z_APDU_searchRequest)
154 ses->search_stat.add_bytes(1);
155 if (m_max_record_retrieve)
157 if (apdu->which == Z_APDU_presentRequest)
159 Z_PresentRequest *pr = apdu->u.presentRequest;
160 if (pr->numberOfRecordsRequested &&
161 *pr->numberOfRecordsRequested > m_max_record_retrieve)
162 *pr->numberOfRecordsRequested = m_max_record_retrieve;
170 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
172 yf::Limit::Ses *ses = 0;
174 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
175 m_sessions.find(package.session());
176 if (it != m_sessions.end())
180 ses = new yf::Limit::Ses;
181 m_sessions[package.session()] = ses;
184 sz += package.response().get_size();
186 ses->bw_stat.add_bytes(sz);
187 ses->pdu_stat.add_bytes(1);
189 int bw_total = ses->bw_stat.get_total();
190 int pdu_total = ses->pdu_stat.get_total();
191 int search_total = ses->search_stat.get_total();
194 reduce += search_total / m_search_max;
196 reduce += (bw_total/m_bw_max);
199 if (pdu_total > m_pdu_max)
201 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
202 reduce = (reduce > nreduce) ? reduce : nreduce;
205 if (package.session().is_closed())
207 m_sessions.erase(package.session());
213 yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
215 Sleep(reduce * 1000);
223 static mp::filter::Base* filter_creator()
225 return new mp::filter::Limit;
229 struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
240 * c-file-style: "Stroustrup"
241 * indent-tabs-mode: nil
243 * vim: shiftwidth=4 tabstop=8 expandtab