mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
The new file can't be called logging.py because then it would be annoying to import the system logging module within it. (imported from commit 71d116e4be98d45b09dda049a43142a82647b727)
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import logging
|
|
import traceback
|
|
from hashlib import sha256
|
|
from datetime import datetime, timedelta
|
|
|
|
# Adapted http://djangosnippets.org/snippets/2242/ by user s29 (October 25, 2010)
|
|
|
|
class _RateLimitFilter(object):
|
|
last_error = datetime.min
|
|
|
|
def filter(self, record):
|
|
from django.conf import settings
|
|
from django.core.cache import cache
|
|
|
|
# Track duplicate errors
|
|
duplicate = False
|
|
rate = getattr(settings, '%s_LIMIT' % self.__class__.__name__.upper(),
|
|
600) # seconds
|
|
if rate > 0:
|
|
# Test if the cache works
|
|
try:
|
|
cache.set('RLF_TEST_KEY', 1, 1)
|
|
use_cache = cache.get('RLF_TEST_KEY') == 1
|
|
except:
|
|
use_cache = False
|
|
|
|
if use_cache:
|
|
key = self.__class__.__name__.upper()
|
|
duplicate = cache.get(key) == 1
|
|
cache.set(key, 1, rate)
|
|
else:
|
|
min_date = datetime.now() - timedelta(seconds=rate)
|
|
duplicate = (self.last_error >= min_date)
|
|
if not duplicate:
|
|
self.last_error = datetime.now()
|
|
|
|
return not duplicate
|
|
|
|
class HumbugLimiter(_RateLimitFilter):
|
|
pass
|
|
|
|
class EmailLimiter(_RateLimitFilter):
|
|
pass
|
|
|
|
class ReturnTrue(logging.Filter):
|
|
def filter(self, record):
|
|
return True
|