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 <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)
134 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
136 yf::Limit::Ses *ses = 0;
138 std::map<mp::Session,yf::Limit::Ses *>::iterator it =
139 m_sessions.find(package.session());
140 if (it != m_sessions.end())
144 ses = new yf::Limit::Ses;
145 m_sessions[package.session()] = ses;
148 int sz = package.request().get_size() + package.response().get_size();
150 ses->bw_stat.add_bytes(sz);
151 ses->pdu_stat.add_bytes(1);
153 Z_GDU *gdu = package.request().get();
154 if (gdu && gdu->which == Z_GDU_Z3950)
156 // we're getting a Z39.50 package
157 Z_APDU *apdu = gdu->u.z3950;
158 if (apdu->which == Z_APDU_searchRequest)
159 ses->search_stat.add_bytes(1);
160 if (m_max_record_retrieve)
162 if (apdu->which == Z_APDU_presentRequest)
164 Z_PresentRequest *pr = apdu->u.presentRequest;
165 if (pr->numberOfRecordsRequested &&
166 *pr->numberOfRecordsRequested > m_max_record_retrieve)
167 *pr->numberOfRecordsRequested = m_max_record_retrieve;
172 int bw_total = ses->bw_stat.get_total();
173 int pdu_total = ses->pdu_stat.get_total();
174 int search_total = ses->search_stat.get_total();
177 reduce += search_total / m_search_max;
179 reduce += (bw_total/m_bw_max);
182 if (pdu_total > m_pdu_max)
184 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
185 reduce = (reduce > nreduce) ? reduce : nreduce;
188 if (package.session().is_closed())
190 m_sessions.erase(package.session());
196 yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
198 Sleep(reduce * 1000);
206 static mp::filter::Base* filter_creator()
208 return new mp::filter::Limit;
212 struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
223 * c-file-style: "Stroustrup"
224 * indent-tabs-mode: nil
226 * vim: shiftwidth=4 tabstop=8 expandtab