cql_rpn.xml \
frontend_net.xml \
http_file.xml \
+ limit.xml \
load_balance.xml \
log.xml \
multi.xml \
cql_rpn.3mp \
frontend_net.3mp \
http_file.3mp \
+ limit.3mp \
load_balance.3mp \
log.3mp \
multi.3mp query_rewrite.3mp \
--- /dev/null
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V4.1//EN"
+ "http://www.oasis-open.org/docbook/xml/4.1/docbookx.dtd" [
+ <!ENTITY copyright SYSTEM "copyright.xml">
+ <!ENTITY % idcommon SYSTEM "common/common.ent">
+ %idcommon;
+]>
+<refentry id="ref-limit">
+ <refmeta>
+ <refentrytitle>limit</refentrytitle>
+ <manvolnum>3mp</manvolnum>
+ <refmiscinfo>Metaproxy Module</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>limit</refname>
+ <refpurpose> Metaproxy Module for imposing resource limits </refpurpose>
+ </refnamediv>
+
+ <refsect1><title>DESCRIPTION</title>
+ <para>
+ This filter offers a way to limit access for a single session to
+ a resource (target) in order to obtain a fair resource sharing.
+ </para>
+ <para>
+ The limit section specifies bandwidth/pdu requests limits for an active
+ session. The filter records bandwidth/pdu requests during the last 60
+ seconds (1 minute). The limit may include the elements bandwidth, pdu,
+ retrieve and search. The bandwidth measures the number of bytes
+ transferred within the last minute. The pdu is the number of requests
+ in the last minute. The retrieve holds the maximum records to which may
+ be retrieved in one Present Request.
+ The search is the maximum number of searches within the last minute.
+ </para>
+ </refsect1>
+
+ <refsect1><title>EXAMPLES</title>
+ <para>
+ Configuration:
+ <screen><![CDATA[
+ <filter type="limit">
+ <limit bandwidth="50000" pdu="100" search="5" retrieve="50"/>
+ </filter>
+]]>
+ </screen>
+ </para>
+ </refsect1>
+
+ <refsect1><title>SEE ALSO</title>
+ <para>
+ <citerefentry>
+ <refentrytitle>metaproxy</refentrytitle>
+ <manvolnum>1</manvolnum>
+ </citerefentry>
+ </para>
+ </refsect1>
+
+ ©right;
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+sgml-parent-document:nil
+sgml-local-catalogs: nil
+sgml-namecase-general:t
+End:
+-->
<message>F</message>
<category init-options="true" apdu="false"/>
</filter>
+ <filter type="limit">
+ <limit bandwidth="50000" pdu="100" search="5" retrieve="50"/>
+ </filter>
<filter type="session_shared">
<resultset ttl="10" max="3"/>
<session ttl="30"/>
sru_util.cpp sru_util.hpp \
thread_pool_observer.cpp thread_pool_observer.hpp \
util.cpp util.hpp \
- xmlutil.cpp xmlutil.hpp
+ xmlutil.cpp xmlutil.hpp \
+ filter_limit.cpp filter_limit.hpp
libmetaproxy_la_LIBADD = $(YAZPPLALIB) $(BOOST_LIB) $(BOOST_THREAD_LIB)
#include "filter_virt_db.hpp"
#include "filter_z3950_client.hpp"
#include "filter_zeerex_explain.hpp"
+#include "filter_limit.hpp"
namespace mp = metaproxy_1;
&metaproxy_1_filter_virt_db,
&metaproxy_1_filter_z3950_client,
&metaproxy_1_filter_zeerex_explain,
+ &metaproxy_1_filter_limit,
0
};
int i;
--- /dev/null
+/* This file is part of Metaproxy.
+ Copyright (C) 2005-2009 Index Data
+
+Metaproxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "config.hpp"
+#include "filter_limit.hpp"
+
+#include <time.h>
+#include <yaz/log.h>
+#include "package.hpp"
+#include "util.hpp"
+
+namespace mp = metaproxy_1;
+namespace yf = mp::filter;
+
+namespace metaproxy_1 {
+ namespace filter {
+ class Limit::Ses {
+ public:
+ Yaz_bw bw_stat;
+ Yaz_bw pdu_stat;
+ Yaz_bw search_stat;
+ Ses() : bw_stat(60), pdu_stat(60), search_stat(60) {};
+ };
+ class Limit::Impl {
+ public:
+ Impl();
+ ~Impl();
+ void process(metaproxy_1::Package & package);
+ void configure(const xmlNode * ptr);
+ private:
+
+ 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;
+ };
+ }
+}
+
+// define Pimpl wrapper forwarding to Impl
+
+yf::Limit::Limit() : m_p(new Impl)
+{
+}
+
+yf::Limit::~Limit()
+{ // must have a destructor because of boost::scoped_ptr
+}
+
+void yf::Limit::configure(const xmlNode *xmlnode, bool test_only)
+{
+ m_p->configure(xmlnode);
+}
+
+void yf::Limit::process(mp::Package &package) const
+{
+ m_p->process(package);
+}
+
+
+// define Implementation stuff
+
+yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
+ m_max_record_retrieve(0)
+{
+}
+
+yf::Limit::Impl::~Impl()
+{
+}
+
+void yf::Limit::Impl::configure(const xmlNode *ptr)
+{
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "limit"))
+ {
+ const struct _xmlAttr *attr;
+ for (attr = ptr->properties; attr; attr = attr->next)
+ {
+ if (!strcmp((const char *) attr->name, "bandwidth"))
+ m_bw_max = mp::xml::get_int(attr->children, 0);
+ else if (!strcmp((const char *) attr->name, "pdu"))
+ m_pdu_max = mp::xml::get_int(attr->children, 0);
+ else if (!strcmp((const char *) attr->name, "search"))
+ m_search_max = mp::xml::get_int(attr->children, 0);
+ else if (!strcmp((const char *) attr->name, "retrieve"))
+ m_max_record_retrieve =
+ mp::xml::get_int(attr->children, 0);
+ else
+ throw mp::filter::FilterException(
+ "Bad attribute " + std::string((const char *)
+ attr->name));
+ }
+ }
+ else
+ {
+ throw mp::filter::FilterException("Bad element "
+ + std::string((const char *)
+ ptr->name));
+ }
+ }
+}
+
+void yf::Limit::Impl::process(mp::Package &package)
+{
+ package.move();
+ int reduce = 0;
+
+ {
+ boost::mutex::scoped_lock scoped_lock(m_session_mutex);
+
+ yf::Limit::Ses *ses = 0;
+
+ std::map<mp::Session,yf::Limit::Ses *>::iterator it =
+ m_sessions.find(package.session());
+ if (it != m_sessions.end())
+ ses = it->second;
+ else
+ {
+ ses = new yf::Limit::Ses;
+ m_sessions[package.session()] = ses;
+ }
+
+ int sz = package.request().get_size() + package.response().get_size();
+
+ ses->bw_stat.add_bytes(sz);
+ ses->pdu_stat.add_bytes(1);
+
+ Z_GDU *gdu = package.request().get();
+ if (gdu && gdu->which == Z_GDU_Z3950)
+ {
+ // we're getting a Z39.50 package
+ Z_APDU *apdu = gdu->u.z3950;
+ if (apdu->which == Z_APDU_searchRequest)
+ ses->search_stat.add_bytes(1);
+ if (m_max_record_retrieve)
+ {
+ if (apdu->which == Z_APDU_presentRequest)
+ {
+ Z_PresentRequest *pr = apdu->u.presentRequest;
+ if (pr->numberOfRecordsRequested &&
+ *pr->numberOfRecordsRequested > m_max_record_retrieve)
+ *pr->numberOfRecordsRequested = m_max_record_retrieve;
+ }
+ }
+ }
+
+ yaz_log(YLOG_LOG, "sz = %d . total = %d", sz,
+ ses->bw_stat.get_total());
+
+ int bw_total = ses->bw_stat.get_total();
+ int pdu_total = ses->pdu_stat.get_total();
+ int search_total = ses->search_stat.get_total();
+
+ if (m_search_max)
+ reduce += search_total / m_search_max;
+ if (m_bw_max)
+ reduce += (bw_total/m_bw_max);
+ if (m_pdu_max)
+ {
+ if (pdu_total > m_pdu_max)
+ {
+ int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
+ reduce = (reduce > nreduce) ? reduce : nreduce;
+ }
+ }
+ if (package.session().is_closed())
+ m_sessions.erase(package.session());
+ }
+ if (reduce)
+ {
+ yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
+ sleep(reduce);
+ }
+}
+
+
+static mp::filter::Base* filter_creator()
+{
+ return new mp::filter::Limit;
+}
+
+extern "C" {
+ struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
+ 0,
+ "limit",
+ filter_creator
+ };
+}
+
+// bandwidth class (taken from YAZ Proxy)
+
+Yaz_bw::Yaz_bw(int sz)
+{
+ m_sec = 0;
+ m_size = sz;
+ m_bucket = new int[m_size];
+ m_ptr = 0;
+}
+
+Yaz_bw::~Yaz_bw()
+{
+ delete [] m_bucket;
+}
+
+int Yaz_bw::get_total()
+{
+ add_bytes(0);
+ int bw = 0;
+ int i;
+ for (i = 0; i<m_size; i++)
+ bw += m_bucket[i];
+ return bw;
+}
+
+void Yaz_bw::add_bytes(int b)
+{
+ long now = time(0);
+
+ if (now >= m_sec)
+ {
+ int d = now - m_sec;
+ if (d > m_size)
+ d = m_size;
+ while (--d >= 0)
+ {
+ if (++m_ptr == m_size)
+ m_ptr = 0;
+ m_bucket[m_ptr] = 0;
+ }
+ m_bucket[m_ptr] += b;
+ }
+ m_sec = now;
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+
--- /dev/null
+/* This file is part of Metaproxy.
+ Copyright (C) 2005-2009 Index Data
+
+Metaproxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef FILTER_LIMIT_HPP
+#define FILTER_LIMIT_HPP
+
+#include <boost/scoped_ptr.hpp>
+
+#include "filter.hpp"
+
+namespace metaproxy_1 {
+ namespace filter {
+ class Limit : public Base {
+ class Impl;
+ class Ses;
+ boost::scoped_ptr<Impl> m_p;
+ public:
+ Limit();
+ ~Limit();
+ void process(metaproxy_1::Package & package) const;
+ void configure(const xmlNode * ptr, bool test_only);
+ };
+ }
+}
+
+extern "C" {
+ extern struct metaproxy_1_filter_struct metaproxy_1_filter_limit;
+}
+
+class Yaz_bw {
+ public:
+ Yaz_bw(int sz);
+ ~Yaz_bw();
+ void add_bytes(int m);
+ int get_total();
+ private:
+ long m_sec; // time of most recent bucket
+ int *m_bucket;
+ int m_ptr;
+ int m_size;
+};
+#endif
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+
$(OBJDIR)\filter_virt_db.obj \
$(OBJDIR)\filter_z3950_client.obj \
$(OBJDIR)\filter_zeerex_explain.obj \
+ $(OBJDIR)\filter_limit.obj \
$(OBJDIR)\gduutil.obj \
$(OBJDIR)\origin.obj \
$(OBJDIR)\package.obj \
| filter_sru_z3950
| filter_virt_db
| filter_z3950_client
+ | filter_limit
# | filter_zeerex_explain
}
element mp:default_target { xsd:string }?,
element mp:force_target { xsd:string }?
+filter_limit =
+ attribute type { "limit" },
+ element mp:limit {
+ attribute bandwidth { xsd:integer }?,
+ attribute pdu { xsd:integer }?,
+ attribute search { xsd:integer }?,
+ attribute retrieve { xsd:integer }?
+ }?
+
#filter_zeerex_explain =
# attribute type { "zeerex_explain" },
# attribute id { xsd:NCName }?,
<ref name="filter_sru_z3950"/>
<ref name="filter_virt_db"/>
<ref name="filter_z3950_client"/>
+ <ref name="filter_limit"/>
</choice>
<!-- | filter_zeerex_explain -->
</element>
</element>
</optional>
</define>
+ <define name="filter_limit">
+ <attribute name="type">
+ <value>limit</value>
+ </attribute>
+ <optional>
+ <element name="mp:limit">
+ <optional>
+ <attribute name="bandwidth">
+ <data type="integer"/>
+ </attribute>
+ </optional>
+ <optional>
+ <attribute name="pdu">
+ <data type="integer"/>
+ </attribute>
+ </optional>
+ <optional>
+ <attribute name="search">
+ <data type="integer"/>
+ </attribute>
+ </optional>
+ <optional>
+ <attribute name="retrieve">
+ <data type="integer"/>
+ </attribute>
+ </optional>
+ </element>
+ </optional>
+ </define>
</grammar>
<!--
filter_zeerex_explain =
<xs:group ref="mp:filter_sru_z3950"/>
<xs:group ref="mp:filter_virt_db"/>
<xs:group ref="mp:filter_z3950_client"/>
+ <xs:group ref="mp:filter_limit"/>
</xs:choice>
<xs:attribute name="type">
<xs:simpleType>
<xs:enumeration value="sru_z3950"/>
<xs:enumeration value="virt_db"/>
<xs:enumeration value="z3950_client"/>
+ <xs:enumeration value="limit"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="id" type="xs:NCName"/>
<xs:attribute name="name" type="xs:NCName"/>
</xs:attributeGroup>
+ <xs:group name="filter_limit">
+ <xs:sequence>
+ <xs:element minOccurs="0" ref="mp:limit"/>
+ </xs:sequence>
+ </xs:group>
+ <xs:element name="limit">
+ <xs:complexType>
+ <xs:attribute name="bandwidth" type="xs:integer"/>
+ <xs:attribute name="pdu" type="xs:integer"/>
+ <xs:attribute name="search" type="xs:integer"/>
+ <xs:attribute name="retrieve" type="xs:integer"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:attributeGroup name="filter_limit">
+ <xs:attribute name="type" use="required">
+ <xs:simpleType>
+ <xs:restriction base="xs:token">
+ <xs:enumeration value="limit"/>
+ </xs:restriction>
+ </xs:simpleType>
+ </xs:attribute>
+ </xs:attributeGroup>
</xs:schema>
<!--
filter_zeerex_explain =