Implement limit filter (bug #2697, bug #2698)
[metaproxy-moved-to-github.git] / src / filter_limit.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include "filter_limit.hpp"
21
22 #include <time.h>
23 #include <yaz/log.h>
24 #include "package.hpp"
25 #include "util.hpp"
26
27 namespace mp = metaproxy_1;
28 namespace yf = mp::filter;
29
30 namespace metaproxy_1 {
31     namespace filter {
32         class Limit::Ses {
33         public:
34             Yaz_bw bw_stat;
35             Yaz_bw pdu_stat;
36             Yaz_bw search_stat;
37             Ses() : bw_stat(60), pdu_stat(60), search_stat(60) {};
38         };
39         class Limit::Impl {
40         public:
41             Impl();
42             ~Impl();
43             void process(metaproxy_1::Package & package);
44             void configure(const xmlNode * ptr);
45         private:
46             
47             boost::mutex m_session_mutex;
48             std::map<mp::Session,Limit::Ses *> m_sessions;
49
50             int m_bw_max;
51             int m_pdu_max;
52             int m_search_max;
53             int m_max_record_retrieve;
54         };
55     }
56 }
57
58 // define Pimpl wrapper forwarding to Impl
59  
60 yf::Limit::Limit() : m_p(new Impl)
61 {
62 }
63
64 yf::Limit::~Limit()
65 {  // must have a destructor because of boost::scoped_ptr
66 }
67
68 void yf::Limit::configure(const xmlNode *xmlnode, bool test_only)
69 {
70     m_p->configure(xmlnode);
71 }
72
73 void yf::Limit::process(mp::Package &package) const
74 {
75     m_p->process(package);
76 }
77
78
79 // define Implementation stuff
80
81 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
82                           m_max_record_retrieve(0)
83 {
84 }
85
86 yf::Limit::Impl::~Impl()
87
88 }
89
90 void yf::Limit::Impl::configure(const xmlNode *ptr)
91 {
92     for (ptr = ptr->children; ptr; ptr = ptr->next)
93     {
94         if (ptr->type != XML_ELEMENT_NODE)
95             continue;
96         if (!strcmp((const char *) ptr->name, "limit"))
97         {
98             const struct _xmlAttr *attr;
99             for (attr = ptr->properties; attr; attr = attr->next)
100             {
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);
110                 else
111                     throw mp::filter::FilterException(
112                         "Bad attribute " + std::string((const char *)
113                                                        attr->name));
114             }
115         }
116         else
117         {
118             throw mp::filter::FilterException("Bad element " 
119                                                + std::string((const char *)
120                                                              ptr->name));
121         }
122     }
123 }
124
125 void yf::Limit::Impl::process(mp::Package &package)
126 {
127     package.move();
128     int reduce = 0;
129
130     {
131         boost::mutex::scoped_lock scoped_lock(m_session_mutex);
132
133         yf::Limit::Ses *ses = 0;
134
135         std::map<mp::Session,yf::Limit::Ses *>::iterator it = 
136             m_sessions.find(package.session());
137         if (it != m_sessions.end())
138             ses = it->second;
139         else
140         {
141             ses = new yf::Limit::Ses;
142             m_sessions[package.session()] = ses;
143         }
144
145         int sz = package.request().get_size() + package.response().get_size();
146         
147         ses->bw_stat.add_bytes(sz);
148         ses->pdu_stat.add_bytes(1);
149         
150         Z_GDU *gdu = package.request().get();
151         if (gdu && gdu->which == Z_GDU_Z3950)
152         {
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)
158             {
159                 if (apdu->which == Z_APDU_presentRequest)
160                 {
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;
165                 }
166             }
167         }
168         
169         yaz_log(YLOG_LOG, "sz = %d . total = %d", sz,
170                 ses->bw_stat.get_total());
171         
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();
175         
176         if (m_search_max)
177             reduce += search_total / m_search_max;
178         if (m_bw_max)
179             reduce += (bw_total/m_bw_max);
180         if (m_pdu_max)
181         {
182             if (pdu_total > m_pdu_max)
183             {
184                 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
185                 reduce = (reduce > nreduce) ? reduce : nreduce;
186             }
187         }
188         if (package.session().is_closed())
189             m_sessions.erase(package.session());
190     }
191     if (reduce)
192     {
193         yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
194         sleep(reduce);
195     }
196 }
197
198
199 static mp::filter::Base* filter_creator()
200 {
201     return new mp::filter::Limit;
202 }
203
204 extern "C" {
205     struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
206         0,
207         "limit",
208         filter_creator
209     };
210 }
211
212 // bandwidth class (taken from YAZ Proxy)
213
214 Yaz_bw::Yaz_bw(int sz)
215 {
216     m_sec = 0;
217     m_size = sz;
218     m_bucket = new int[m_size];
219     m_ptr = 0;
220 }
221
222 Yaz_bw::~Yaz_bw()
223 {
224     delete [] m_bucket;
225 }
226
227 int Yaz_bw::get_total()
228 {
229     add_bytes(0);
230     int bw = 0;
231     int i;
232     for (i = 0; i<m_size; i++)
233         bw += m_bucket[i];
234     return bw;
235 }
236
237 void Yaz_bw::add_bytes(int b)
238 {
239     long now = time(0);
240
241     if (now >= m_sec)
242     {
243         int d = now - m_sec;
244         if (d > m_size)
245             d = m_size;
246         while (--d >= 0)
247         {
248             if (++m_ptr == m_size)
249                 m_ptr = 0;
250             m_bucket[m_ptr] = 0;
251         }
252         m_bucket[m_ptr] += b;
253     }
254     m_sec = now;
255 }
256
257 /*
258  * Local variables:
259  * c-basic-offset: 4
260  * c-file-style: "Stroustrup"
261  * indent-tabs-mode: nil
262  * End:
263  * vim: shiftwidth=4 tabstop=8 expandtab
264  */
265