1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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 "package.hpp"
28 namespace mp = metaproxy_1;
29 namespace yf = mp::filter;
31 namespace metaproxy_1 {
35 yazpp_1::TimeStat bw_stat;
36 yazpp_1::TimeStat pdu_stat;
37 yazpp_1::TimeStat search_stat;
38 Ses() : bw_stat(60), pdu_stat(60), search_stat(60) {};
45 void process(metaproxy_1::Package & package);
46 void configure(const xmlNode * ptr);
48 boost::mutex m_session_mutex;
49 std::map<mp::Session,Limit::Ses *> m_sessions;
53 int m_max_record_retrieve;
58 // define Pimpl wrapper forwarding to Impl
60 yf::Limit::Limit() : m_p(new Impl)
65 { // must have a destructor because of boost::scoped_ptr
68 void yf::Limit::configure(const xmlNode *xmlnode, bool test_only)
70 m_p->configure(xmlnode);
73 void yf::Limit::process(mp::Package &package) const
75 m_p->process(package);
79 // define Implementation stuff
81 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
82 m_max_record_retrieve(0)
86 yf::Limit::Impl::~Impl()
90 void yf::Limit::Impl::configure(const xmlNode *ptr)
92 for (ptr = ptr->children; ptr; ptr = ptr->next)
94 if (ptr->type != XML_ELEMENT_NODE)
96 if (!strcmp((const char *) ptr->name, "limit"))
98 const struct _xmlAttr *attr;
99 for (attr = ptr->properties; attr; attr = attr->next)
101 if (!strcmp((const char *) attr->name, "bandwidth"))
102 m_bw_max = mp::xml::get_int(attr->children, 0);
103 else if (!strcmp((const char *) attr->name, "pdu"))
104 m_pdu_max = mp::xml::get_int(attr->children, 0);
105 else if (!strcmp((const char *) attr->name, "search"))
106 m_search_max = mp::xml::get_int(attr->children, 0);
107 else if (!strcmp((const char *) attr->name, "retrieve"))
108 m_max_record_retrieve =
109 mp::xml::get_int(attr->children, 0);
111 throw mp::filter::FilterException(
112 "Bad attribute " + std::string((const char *)
118 throw mp::filter::FilterException("Bad element "
119 + std::string((const char *)
125 void yf::Limit::Impl::process(mp::Package &package)
131 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
133 yf::Limit::Ses *ses = 0;
135 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
136 m_sessions.find(package.session());
137 if (it != m_sessions.end())
141 ses = new yf::Limit::Ses;
142 m_sessions[package.session()] = ses;
145 int sz = package.request().get_size() + package.response().get_size();
147 ses->bw_stat.add_bytes(sz);
148 ses->pdu_stat.add_bytes(1);
150 Z_GDU *gdu = package.request().get();
151 if (gdu && gdu->which == Z_GDU_Z3950)
153 // we're getting a Z39.50 package
154 Z_APDU *apdu = gdu->u.z3950;
155 if (apdu->which == Z_APDU_searchRequest)
156 ses->search_stat.add_bytes(1);
157 if (m_max_record_retrieve)
159 if (apdu->which == Z_APDU_presentRequest)
161 Z_PresentRequest *pr = apdu->u.presentRequest;
162 if (pr->numberOfRecordsRequested &&
163 *pr->numberOfRecordsRequested > m_max_record_retrieve)
164 *pr->numberOfRecordsRequested = m_max_record_retrieve;
169 int bw_total = ses->bw_stat.get_total();
170 int pdu_total = ses->pdu_stat.get_total();
171 int search_total = ses->search_stat.get_total();
174 reduce += search_total / m_search_max;
176 reduce += (bw_total/m_bw_max);
179 if (pdu_total > m_pdu_max)
181 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
182 reduce = (reduce > nreduce) ? reduce : nreduce;
185 if (package.session().is_closed())
187 m_sessions.erase(package.session());
193 yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
199 static mp::filter::Base* filter_creator()
201 return new mp::filter::Limit;
205 struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
216 * c-file-style: "Stroustrup"
217 * indent-tabs-mode: nil
219 * vim: shiftwidth=4 tabstop=8 expandtab