Skip to content

Commit 82bc68c

Browse files
senfomatrodjafalkoschindler
authored
Fix support for Redis Unix-Domainsocket-connections (#5214)
### Motivation I usually use Redis with UnixDomainsocket-connections, using connection-URLs beginning with `unix://`. When trying to use this for persistence in NiceGUI I discovered, that this throws the error 'Could not load data from Redis with key ...'. The reason is, that the option `socket_keepalive` is not present when `redis.from_url()` leads to the use of Redis' class `UnixDomainSocketConnection`. ### Implementation The fix builds a connection-parameter-dict in the class `RedisPersistentDict` and adds the option 'socket_keepalive' ony when the URL is not beginning with `unix://`. Furthermore it reuses this dict for the Redis-connection in the same Class on `initialize_sync()`. ### Progress - [x] I chose a meaningful title that completes the sentence: "If applied, this PR will..." - [x] The implementation is complete. - [x] Pytests are are not necessary. - [x] Documentation is not necessary. --------- Co-authored-by: Rodja Trappe <rodja@zauberzeug.com> Co-authored-by: Falko Schindler <falko@zauberzeug.com>
1 parent 6d5b751 commit 82bc68c

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

nicegui/persistence/redis_persistent_dict.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def __init__(self, *, url: str, id: str, key_prefix: str = 'nicegui:') -> None:
1717
if not optional_features.has('redis'):
1818
raise ImportError('Redis is not installed. Please run "pip install nicegui[redis]".')
1919
self.url = url
20-
self.redis_client = redis.from_url(
21-
url,
22-
health_check_interval=10,
23-
socket_connect_timeout=5,
24-
retry_on_timeout=True,
25-
socket_keepalive=True,
26-
)
20+
self._redis_client_params = {
21+
'health_check_interval': 10,
22+
'socket_connect_timeout': 5,
23+
'retry_on_timeout': True,
24+
**({'socket_keepalive': True} if not url.startswith('unix://') else {}),
25+
}
26+
self.redis_client = redis.from_url(self.url, **self._redis_client_params)
2727
self.pubsub = self.redis_client.pubsub()
2828
self.key = key_prefix + id
2929
self._should_listen = True
@@ -40,13 +40,7 @@ async def initialize(self) -> None:
4040

4141
def initialize_sync(self) -> None:
4242
"""Load initial data from Redis and start listening for changes in a synchronous context."""
43-
with redis_sync.from_url(
44-
self.url,
45-
health_check_interval=10,
46-
socket_connect_timeout=5,
47-
retry_on_timeout=True,
48-
socket_keepalive=True,
49-
) as redis_client_sync:
43+
with redis_sync.from_url(self.url, **self._redis_client_params) as redis_client_sync:
5044
try:
5145
data = redis_client_sync.get(self.key)
5246
self.update(json.loads(data) if data else {})

0 commit comments

Comments
 (0)