metaproxy  1.13.0
filter_limit.cpp
Go to the documentation of this file.
1 /* This file is part of Metaproxy.
2  Copyright (C) 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 <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30 
31 namespace mp = metaproxy_1;
32 namespace yf = mp::filter;
33 
34 namespace metaproxy_1 {
35  namespace filter {
36  class Limit::Ses {
37  public:
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) {};
42  };
43 
44  class Limit::Impl {
45  public:
46  Impl();
47  ~Impl();
48  void process(metaproxy_1::Package & package);
49  void configure(const xmlNode * ptr);
50  private:
51  boost::mutex m_session_mutex;
52  std::map<mp::Session,Limit::Ses *> m_sessions;
53  int m_bw_max;
54  int m_pdu_max;
57  };
58  }
59 }
60 
61 // define Pimpl wrapper forwarding to Impl
62 
63 yf::Limit::Limit() : m_p(new Impl)
64 {
65 }
66 
67 yf::Limit::~Limit()
68 { // must have a destructor because of boost::scoped_ptr
69 }
70 
71 void yf::Limit::configure(const xmlNode *xmlnode, bool test_only,
72  const char *path)
73 {
74  m_p->configure(xmlnode);
75 }
76 
77 void yf::Limit::process(mp::Package &package) const
78 {
79  m_p->process(package);
80 }
81 
82 
83 // define Implementation stuff
84 
85 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
87 {
88 }
89 
90 yf::Limit::Impl::~Impl()
91 {
92 }
93 
94 void yf::Limit::Impl::configure(const xmlNode *ptr)
95 {
96  for (ptr = ptr->children; ptr; ptr = ptr->next)
97  {
98  if (ptr->type != XML_ELEMENT_NODE)
99  continue;
100  if (!strcmp((const char *) ptr->name, "limit"))
101  {
102  const struct _xmlAttr *attr;
103  for (attr = ptr->properties; attr; attr = attr->next)
104  {
105  if (!strcmp((const char *) attr->name, "bandwidth"))
106  m_bw_max = mp::xml::get_int(attr->children, 0);
107  else if (!strcmp((const char *) attr->name, "pdu"))
108  m_pdu_max = mp::xml::get_int(attr->children, 0);
109  else if (!strcmp((const char *) attr->name, "search"))
110  m_search_max = mp::xml::get_int(attr->children, 0);
111  else if (!strcmp((const char *) attr->name, "retrieve"))
113  mp::xml::get_int(attr->children, 0);
114  else
115  throw mp::filter::FilterException(
116  "Bad attribute " + std::string((const char *)
117  attr->name));
118  }
119  }
120  else
121  {
122  throw mp::filter::FilterException("Bad element "
123  + std::string((const char *)
124  ptr->name));
125  }
126  }
127 }
128 
129 void yf::Limit::Impl::process(mp::Package &package)
130 {
131  int sz = 0;
132  {
133  boost::mutex::scoped_lock scoped_lock(m_session_mutex);
134 
135  yf::Limit::Ses *ses = 0;
136 
137  std::map<mp::Session,yf::Limit::Ses *>::iterator it =
138  m_sessions.find(package.session());
139  if (it != m_sessions.end())
140  ses = it->second;
141  else
142  {
143  ses = new yf::Limit::Ses;
144  m_sessions[package.session()] = ses;
145  }
146 
147 
148  Z_GDU *gdu = package.request().get();
149  if (gdu && gdu->which == Z_GDU_Z3950)
150  {
151  sz += package.request().get_size();
152  // we're getting a Z39.50 package
153  Z_APDU *apdu = gdu->u.z3950;
154  if (apdu->which == Z_APDU_searchRequest)
155  ses->search_stat.add_bytes(1);
157  {
158  if (apdu->which == Z_APDU_presentRequest)
159  {
160  Z_PresentRequest *pr = apdu->u.presentRequest;
161  if (pr->numberOfRecordsRequested &&
162  *pr->numberOfRecordsRequested > m_max_record_retrieve)
163  *pr->numberOfRecordsRequested = m_max_record_retrieve;
164  }
165  }
166  }
167  }
168  package.move();
169  int reduce = 0;
170  {
171  boost::mutex::scoped_lock scoped_lock(m_session_mutex);
172 
173  yf::Limit::Ses *ses = 0;
174 
175  std::map<mp::Session,yf::Limit::Ses *>::iterator it =
176  m_sessions.find(package.session());
177  if (it != m_sessions.end())
178  ses = it->second;
179  else
180  {
181  ses = new yf::Limit::Ses;
182  m_sessions[package.session()] = ses;
183  }
184 
185  sz += package.response().get_size();
186 
187  ses->bw_stat.add_bytes(sz);
188  ses->pdu_stat.add_bytes(1);
189 
190  int bw_total = ses->bw_stat.get_total();
191  int pdu_total = ses->pdu_stat.get_total();
192  int search_total = ses->search_stat.get_total();
193 
194  if (m_search_max)
195  reduce += search_total / m_search_max;
196  if (m_bw_max)
197  reduce += (bw_total/m_bw_max);
198  if (m_pdu_max)
199  {
200  if (pdu_total > m_pdu_max)
201  {
202  int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
203  reduce = (reduce > nreduce) ? reduce : nreduce;
204  }
205  }
206  if (package.session().is_closed())
207  {
208  m_sessions.erase(package.session());
209  delete ses;
210  }
211  }
212  if (reduce)
213  {
214  std::ostringstream os;
215  os << "S" << " "
216  << package
217  << " sleeping "
218  << reduce
219  << " seconds";
220  yaz_log(YLOG_LOG, "%s", os.str().c_str());
221 #ifdef WIN32
222  Sleep(reduce * 1000);
223 #else
224  sleep(reduce);
225 #endif
226  }
227 }
228 
229 
230 static mp::filter::Base* filter_creator()
231 {
232  return new mp::filter::Limit;
233 }
234 
235 extern "C" {
236  struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
237  0,
238  "limit",
240  };
241 }
242 
243 
244 /*
245  * Local variables:
246  * c-basic-offset: 4
247  * c-file-style: "Stroustrup"
248  * indent-tabs-mode: nil
249  * End:
250  * vim: shiftwidth=4 tabstop=8 expandtab
251  */
252 
std::map< mp::Session, Limit::Ses * > m_sessions
boost::scoped_ptr< Impl > m_p
void configure(const xmlNode *ptr, bool test_only, const char *path)
static mp::filter::Base * filter_creator()
void process(metaproxy_1::Package &package) const
struct metaproxy_1_filter_struct metaproxy_1_filter_limit