Skip to content

Commit

Permalink
Merge pull request #268 from jsocol/add-system-check
Browse files Browse the repository at this point in the history
add system check
  • Loading branch information
jsocol authored Dec 3, 2022
2 parents 94cdb2a + e1fb721 commit 96034df
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
10 changes: 10 additions & 0 deletions django_ratelimit/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.apps import AppConfig


class DjangoRatelimitConfig(AppConfig):
name = 'django_ratelimit'
label = 'ratelimit'
default = True

def ready(self):
from . import checks # noqa: F401
69 changes: 69 additions & 0 deletions django_ratelimit/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from django.conf import settings
from django.core import checks

SUPPORTED_CACHE_BACKENDS = [
'django.core.cache.backends.memcached.PyMemcacheCache',
'django.core.cache.backends.memcached.PyLibMCCache',
'django_redis.cache.RedisCache',
]

CACHE_FAKE = 'is not a real cache'
CACHE_NOT_SHARED = 'is not a shared cache'
CACHE_NOT_ATOMIC = 'does not support atomic increment'

KNOWN_BROKEN_CACHE_BACKENDS = {
'django.core.cache.backends.dummy.DummyCache': CACHE_FAKE,
'django.core.cache.backends.locmem.LocMemCache': CACHE_NOT_SHARED,
'django.core.cache.backends.filebased.FileBasedCache': CACHE_NOT_ATOMIC,
'django.core.cache.backends.db.DatabaseCache': CACHE_NOT_ATOMIC,
}


@checks.register(checks.Tags.caches, 'django_ratelimit')
def check_caches(app_configs, **kwargs):
errors = []
cache_name = getattr(settings, 'RATELIMIT_USE_CACHE', 'default')
caches = getattr(settings, 'CACHES', None)
if caches is None:
errors.append(
checks.Error(
'CACHES is not defined, django_ratelimit will not work',
hint='Configure a default cache using memcached or redis.',
id='django_ratelimit.E001',
)
)
return errors

if cache_name not in caches:
errors.append(
checks.Error(
f'RATELIMIT_USE_CACHE value "{cache_name}"" does not '
f'appear in CACHES dictionary',
hint='RATELIMIT_USE_CACHE must be set to a valid cache',
id='django_ratelimit.E002',
)
)
return errors

cache_config = caches[cache_name]
backend = cache_config['BACKEND']

reason = KNOWN_BROKEN_CACHE_BACKENDS.get(backend, None)
if reason is not None:
errors.append(
checks.Error(
f'cache backend {backend} {reason}',
hint='Use a supported cache backend',
id='django_ratelimit.E003',
)
)

if backend not in SUPPORTED_CACHE_BACKENDS:
errors.append(
checks.Warning(
f'cache backend {backend} is not officially supported',
id='django_ratelimit.W001',
)
)

return errors
1 change: 0 additions & 1 deletion django_ratelimit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ def get_usage(request, group=None, fn=None, key=None, rate=None, method=ALL,
else:
count = cache.get(cache_key, initial_value)

print(f'The count is {count}')
# Getting or setting the count from the cache failed
if count is None or count is False:
if getattr(settings, 'RATELIMIT_FAIL_OPEN', False):
Expand Down
2 changes: 2 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SECRET_KEY = 'ratelimit'

SILENCED_SYSTEM_CHECKS = ['django_ratelimit.E003', 'django_ratelimit.W001']

INSTALLED_APPS = (
'django_ratelimit',
)
Expand Down

0 comments on commit 96034df

Please sign in to comment.