django-defender
django-defender copied to clipboard
Standardize redis url parsing
Hi,
At the moment I'm not really sure if django-defender is compatible with redis through unix socket. django-defender uses a custom parse_redis_url function (https://github.com/jazzband/django-defender/blob/bb122f24b9e93b2116459fb8f4ea17cdf46ad557/defender/connection.py#L47).
This results in the following parsing error when using DEFENDER_REDIS_URL = "unix://run/redis/redis.sock" :
def parse_redis_url(url, password_quote=None):
"""Parses a redis URL."""
# create config with some sane defaults
redis_config = {
"DB": 0,
"USERNAME": "default",
"PASSWORD": None,
"HOST": "localhost",
"PORT": 6379,
"SSL": False,
}
if not url:
return redis_config
url = urlparse.urlparse(url)
# Remove query strings.
path = url.path[1:]
path = path.split("?", 2)[0]
if path:
> redis_config.update({"DB": int(path)})
E ValueError: invalid literal for int() with base 10: 'redis/redis.sock'
Shouldn't you use redis.Redis.from_url method instead ? This should require redis-py >=2.7.0.
If you're ok with this approach I can do the PR.
I think that makes sense. If you submit the PR, please also add the unit tests to cover socket and URL based redis urls.
I have to check which redis version we require today, to see if that is a big jump or not. If too big I wonder if we make a larger bump to the version so people are aware it is a bigger change.
Should be solved by https://github.com/jazzband/django-defender/pull/234
Thanks, closing