55from datetime import datetime , timedelta
66from operator import attrgetter
77from typing import TYPE_CHECKING , Any , Callable , List , Optional , Tuple , Union
8- from urllib .parse import urlparse
8+ from urllib .parse import parse_qs , urlparse
99from uuid import uuid4
1010
1111from redis .asyncio import ConnectionPool , Redis
@@ -29,6 +29,7 @@ class RedisSettings:
2929
3030 host : Union [str , List [Tuple [str , int ]]] = 'localhost'
3131 port : int = 6379
32+ unix_socket_path : Optional [str ] = None
3233 database : int = 0
3334 username : Optional [str ] = None
3435 password : Optional [str ] = None
@@ -49,14 +50,21 @@ class RedisSettings:
4950 @classmethod
5051 def from_dsn (cls , dsn : str ) -> 'RedisSettings' :
5152 conf = urlparse (dsn )
52- assert conf .scheme in {'redis' , 'rediss' }, 'invalid DSN scheme'
53+ assert conf .scheme in {'redis' , 'rediss' , 'unix' }, 'invalid DSN scheme'
54+ query_db = parse_qs (conf .query ).get ('db' )
55+ if query_db :
56+ # e.g. redis://localhost:6379?db=1
57+ database = int (query_db [0 ])
58+ else :
59+ database = int (conf .path .lstrip ('/' )) if conf .path else 0
5360 return RedisSettings (
5461 host = conf .hostname or 'localhost' ,
5562 port = conf .port or 6379 ,
5663 ssl = conf .scheme == 'rediss' ,
5764 username = conf .username ,
5865 password = conf .password ,
59- database = int ((conf .path or '0' ).strip ('/' )),
66+ database = database ,
67+ unix_socket_path = conf .path if conf .scheme == 'unix' else None ,
6068 )
6169
6270 def __repr__ (self ) -> str :
@@ -230,6 +238,7 @@ def pool_factory(*args: Any, **kwargs: Any) -> ArqRedis:
230238 ArqRedis ,
231239 host = settings .host ,
232240 port = settings .port ,
241+ unix_socket_path = settings .unix_socket_path ,
233242 socket_connect_timeout = settings .conn_timeout ,
234243 ssl = settings .ssl ,
235244 ssl_keyfile = settings .ssl_keyfile ,
0 commit comments