install -D -m 755 omf/reset_xmpp_pubsub_nodes.py $RPM_BUILD_ROOT/usr/bin/reset_xmpp_pubsub_nodes.py
mkdir -p $RPM_BUILD_ROOT/var/log/omf
+# Install ratelimit log
+touch $RPM_BUILD_ROOT/var/log/api_ratelimit.log
+chown apache:apache $RPM_BUILD_ROOT/var/log/api_ratelimit.log
+
%clean
rm -rf $RPM_BUILD_ROOT
def __init__(self):
self.config = Config("/etc/planetlab/plc_config")
+ # FIXME: change with Config values
self.prefix = "ratelimit"
self.minutes = 5 # The time period
self.requests = 50 # Number of allowed requests in that time period
if api_method_source == None or api_method_source[0] == self.config.PLC_API_IP:
return
- mc = memcache.Client(['%s:11211' % self.config.PLC_API_HOST])
+ mc = memcache.Client(["%s:11211" % self.config.PLC_API_HOST])
now = datetime.now()
- current_key = '%s_%s_%s' % (self.prefix, api_method_source[0], now.strftime('%Y%m%d%H%M'))
+ current_key = "%s_%s_%s" % (self.prefix, api_method_source[0], now.strftime("%Y%m%d%H%M"))
- keys_to_check = ['%s_%s_%s' % (self.prefix, api_method_source[0], (now - timedelta(minutes = minute)).strftime('%Y%m%d%H%M')) for minute in range(self.minutes + 1)]
+ keys_to_check = ["%s_%s_%s" % (self.prefix, api_method_source[0], (now - timedelta(minutes = minute)).strftime("%Y%m%d%H%M")) for minute in range(self.minutes + 1)]
try:
mc.incr(current_key)
total_requests += result[i]
if total_requests > self.requests:
+ log = open("/var/log/api_ratelimit.log", "a")
+ date = datetime.now().strftime("%d/%m/%y %H:%M")
+ log.write("%s - %s\n" % (date, api_method_source[0]))
+ log.flush()
raise PLCPermissionDenied, "Maximum allowed number of API calls exceeded"
def after(self, wobj, data, *args, **kwargs):