]> git.ipfire.org Git - pbs.git/commitdiff
registry: Stream blobs from a separate thread
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 Feb 2025 13:26:23 +0000 (13:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 Feb 2025 13:26:23 +0000 (13:26 +0000)
We need this to not block the main thread on really large blobs.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/web/registry.py

index 62359261dd5982bb15181f34426a84ee810e036f..0bda117e87ff484c7fbef83dc878f5e6b13e82a5 100644 (file)
@@ -26,6 +26,7 @@ import json
 import tornado.web
 
 from . import base
+from ..decorators import run_in_thread
 
 class BaseHandler(base.BaseHandler):
        """
@@ -50,6 +51,18 @@ class BaseHandler(base.BaseHandler):
                                if blob:
                                        return blob
 
+       @run_in_thread
+       def stream_blob(self, blob):
+               """
+                       Streams the blob from a separate thread
+               """
+               while True:
+                       chunk = blob.read(1024 * 1024)
+                       if not chunk:
+                               break
+
+                       self.write(chunk)
+
        async def write_error(self, *args, **kwargs):
                pass # Don't send any body
 
@@ -152,13 +165,5 @@ class BlobOrManifestHandler(BaseHandler):
                # determine the length of the file without reading the whole thing first.
 
                # Done if we should not send the body
-               if not send_body:
-                       return
-
-               # Send the file
-               while True:
-                       chunk = blob.read(1024 * 1024)
-                       if not chunk:
-                               break
-
-                       self.write(chunk)
+               if send_body:
+                       await self.stream_blob(blob)