def __call__(self, request): for k, v in request.items(): info("%s:%s" % (k, v)) info("---") return PolicyResponse().dunno()
def quota_for(self, db, cur, item, quota_keys): if item['key'] == '*': k = quota_keys[item['key_type']] else: k = item['key'] if item['limit_type'] == 'size': increment = quota_keys['size'] else: increment = 1.0 if item['elapsed_time'] >= item['interval_'] \ or item['elapsed_time'] is None \ or item['counter'] is None: counter = increment else: counter = (1-(item['elapsed_time']/item['interval_']))*item['counter'] if counter > item['limit_']: info('REJECTED %s:%s score:%.2f policy:%d/%dh' % ( item['key_type'], k, counter, item['limit_'], item['interval_']//3600) ) cur.close() db.close() return PolicyResponse().reject(item['msg']) else: counter = counter + increment cur.execute('''UPDATE quota_tracking SET counter=%s, last_seen=NOW() WHERE quota_id=%s AND key_type=%s AND key=%s''', (counter, item['id'], item['key_type'], k)) #info('key:%s increment: %s' % (k, item['counter'])) #info('key: %s elapsed:%s interval:%s counter=%s old_counter=%s' % ( # k,item['elapsed_time'],item['interval_'],counter, item['counter'])) db.commit() return PolicyResponse().accept()