]> git.ipfire.org Git - dbl.git/commitdiff
ratelimiter: Purge any outdated data from the ratelimiter table
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 16:51:20 +0000 (16:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 16:51:20 +0000 (16:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dbl/__init__.py
src/dbl/api/__init__.py

index a6db0d3afe51394d1218f842077e107f8a2f9bef..f3d0a0a9e213e3507229ccf468c5bdfd607f1724 100644 (file)
@@ -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)
index 2b29f318374de46ff2d0cd1b85bba7c398dca0ad..aba9f50b9c9e2c19177a59a325f93bac59ccc26d 100644 (file)
@@ -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(