From: Michael Tremer Date: Tue, 3 Mar 2026 16:51:20 +0000 (+0000) Subject: ratelimiter: Purge any outdated data from the ratelimiter table X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a0de6227ad7ae511c3347a4bf02b2cc6f59daa13;p=dbl.git ratelimiter: Purge any outdated data from the ratelimiter table Signed-off-by: Michael Tremer --- diff --git a/src/dbl/__init__.py b/src/dbl/__init__.py index a6db0d3..f3d0a0a 100644 --- a/src/dbl/__init__.py +++ b/src/dbl/__init__.py @@ -137,6 +137,26 @@ class Backend(object): return task + def run_periodic_task(self, delay, callback, *args): + """ + Calls the given callback periodically in the background + """ + self.run_task(self._periodic_task, delay, callback, *args) + + async def _periodic_task(self, delay, callback, *args): + """ + Helper function for run_periodic_task() that will call the given + callback regulary after the timer has expired. + """ + log.debug("Periodic callback %r started" % callback) + + while True: + # Run the callback in a separate task + self.run_task(callback, *args) + + # Wait a little moment + await asyncio.sleep(delay) + @functools.cached_property def auth(self): return auth.Auth(self) diff --git a/src/dbl/api/__init__.py b/src/dbl/api/__init__.py index 2b29f31..aba9f50 100644 --- a/src/dbl/api/__init__.py +++ b/src/dbl/api/__init__.py @@ -50,6 +50,9 @@ backend = app.state.backend = Backend( debug = True, ) +# Register background tasks +backend.run_periodic_task(600, backend.ratelimiter.cleanup) + api_key_header = fastapi.security.APIKeyHeader(name="X-API-Key") async def require_current_user(