Use TimeStat rather than Yaz_bw
[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 <yazpp/timestat.h>
25 #include "package.hpp"
26 #include "util.hpp"
27
28 namespace mp = metaproxy_1;
29 namespace yf = mp::filter;
30
31 namespace metaproxy_1 {
32     namespace filter {
33         class Limit::Ses {
34         public:
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) {};
39         };
40
41         class Limit::Impl {
42         public:
43             Impl();
44             ~Impl();
45             void process(metaproxy_1::Package & package);
46             void configure(const xmlNode * ptr);
47         private:
48             boost::mutex m_session_mutex;
49             std::map<mp::Session,Limit::Ses *> m_sessions;
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         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();
172         
173         if (m_search_max)
174             reduce += search_total / m_search_max;
175         if (m_bw_max)
176             reduce += (bw_total/m_bw_max);
177         if (m_pdu_max)
178         {
179             if (pdu_total > m_pdu_max)
180             {
181                 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
182                 reduce = (reduce > nreduce) ? reduce : nreduce;
183             }
184         }
185         if (package.session().is_closed())
186             m_sessions.erase(package.session());
187     }
188     if (reduce)
189     {
190         yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
191         sleep(reduce);
192     }
193 }
194
195
196 static mp::filter::Base* filter_creator()
197 {
198     return new mp::filter::Limit;
199 }
200
201 extern "C" {
202     struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
203         0,
204         "limit",
205         filter_creator
206     };
207 }
208
209
210 /*
211  * Local variables:
212  * c-basic-offset: 4
213  * c-file-style: "Stroustrup"
214  * indent-tabs-mode: nil
215  * End:
216  * vim: shiftwidth=4 tabstop=8 expandtab
217  */
218