metaproxy  1.21.0
Public Member Functions | Private Attributes | List of all members
metaproxy_1::filter::Limit::Impl Class Reference
Collaboration diagram for metaproxy_1::filter::Limit::Impl:
Collaboration graph

Public Member Functions

 Impl ()
 
 ~Impl ()
 
void process (metaproxy_1::Package &package)
 
void configure (const xmlNode *ptr)
 

Private Attributes

boost::mutex m_session_mutex
 
std::map< mp::Session, Limit::Ses * > m_sessions
 
int m_bw_max
 
int m_pdu_max
 
int m_search_max
 
int m_max_record_retrieve
 

Detailed Description

Definition at line 44 of file filter_limit.cpp.

Constructor & Destructor Documentation

◆ Impl()

mp::filter::Limit::Impl::Impl ( )

◆ ~Impl()

mp::filter::Limit::Impl::~Impl ( )

Definition at line 90 of file filter_limit.cpp.

91 {
92 }

Member Function Documentation

◆ configure()

void mp::filter::Limit::Impl::configure ( const xmlNode *  ptr)

Definition at line 94 of file filter_limit.cpp.

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 }

◆ process()

void mp::filter::Limit::Impl::process ( metaproxy_1::Package &  package)

Definition at line 129 of file filter_limit.cpp.

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 }
std::map< mp::Session, Limit::Ses * > m_sessions

Member Data Documentation

◆ m_bw_max

int metaproxy_1::filter::Limit::Impl::m_bw_max
private

Definition at line 53 of file filter_limit.cpp.

◆ m_max_record_retrieve

int metaproxy_1::filter::Limit::Impl::m_max_record_retrieve
private

Definition at line 56 of file filter_limit.cpp.

◆ m_pdu_max

int metaproxy_1::filter::Limit::Impl::m_pdu_max
private

Definition at line 54 of file filter_limit.cpp.

◆ m_search_max

int metaproxy_1::filter::Limit::Impl::m_search_max
private

Definition at line 55 of file filter_limit.cpp.

◆ m_session_mutex

boost::mutex metaproxy_1::filter::Limit::Impl::m_session_mutex
private

Definition at line 51 of file filter_limit.cpp.

◆ m_sessions

std::map<mp::Session,Limit::Ses *> metaproxy_1::filter::Limit::Impl::m_sessions
private

Definition at line 52 of file filter_limit.cpp.


The documentation for this class was generated from the following file: