Skip to content

Commit

Permalink
Merge pull request #253 from minrk/redis-cursor
Browse files Browse the repository at this point in the history
fix initial cursor in delete script
  • Loading branch information
manics authored Aug 22, 2024
2 parents ec1aa7b + 7f41373 commit b68c0fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 13 additions & 7 deletions jupyterhub_traefik_proxy/redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Redis backend"""

import asyncio
from functools import partial
from urllib.parse import urlparse

from traitlets import Any, Dict, Unicode, default
Expand Down Expand Up @@ -69,7 +70,12 @@ async def _cleanup(self):
f = super()._cleanup()
if f is not None:
await f
await self.redis.close()
if hasattr(self.redis, 'aclose'):
aclose = self.redis.aclose
else:
# redis < 5.0.1
aclose = self.redis.close
await aclose()

def _setup_traefik_static_config(self):
self.log.debug("Setting up the redis provider in the traefik static config")
Expand Down Expand Up @@ -115,7 +121,7 @@ def _register_delete_script(self):
"""
_delete_lua = """
local all_keys = {};
local cursor = "";
local cursor = "0";
repeat
local result = redis.call("SCAN", cursor, "match", ARGV[1], "count", ARGV[2])
cursor = result[1];
Expand Down Expand Up @@ -144,11 +150,11 @@ async def _kv_atomic_delete(self, *keys):
prefix = key + "*"
self.log.debug("Deleting redis tree %s", prefix)
f = asyncio.ensure_future(self._delete_script(args=[prefix, 100]))
f.add_done_callback(
lambda f: self.log.debug(
"Deleted %i keys in %s", f.result(), prefix
)
)

def _log_delete(_prefix, f):
self.log.debug("Deleted %i keys in %s", f.result(), _prefix)

f.add_done_callback(partial(_log_delete, prefix))
futures.append(f)
else:
to_delete.append(key)
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ async def external_redis_proxy(launch_traefik_redis, proxy_args):
)
await proxy._start_future
yield proxy
await proxy._cleanup()


@pytest.fixture
Expand Down Expand Up @@ -801,6 +802,7 @@ async def _wait_for_redis():
from redis.asyncio import Redis

async def _check_redis():
r = None
try:
r = Redis(
port=Config.redis_port,
Expand All @@ -811,6 +813,10 @@ async def _check_redis():
except redis.exceptions.ConnectionError as e:
print(e)
return False
finally:
if r is not None:
await r.aclose()

return True

await exponential_backoff(
Expand Down

0 comments on commit b68c0fb

Please sign in to comment.