From 57556e09f57ef04795747d3cefb2f7e5d8206ffe Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 14 Jul 2020 00:46:22 -0400 Subject: [PATCH] Change chunk size --- CHANGELOG.md | 4 ++++ jetforce/app/static.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45285b2..9ee97c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Jetforce Changelog +### Unreleased + +- Optimized chunking for streaming large files. + ### v0.5.0 (2020-07-14) #### Spec Changes diff --git a/jetforce/app/static.py b/jetforce/app/static.py index 092e69e..854b793 100644 --- a/jetforce/app/static.py +++ b/jetforce/app/static.py @@ -6,6 +6,8 @@ import subprocess import typing import urllib.parse +from twisted.protocols.basic import FileSender + from .base import JetforceApplication, Request, Response, RoutePattern, Status @@ -23,6 +25,9 @@ class StaticDirectoryApplication(JetforceApplication): listing will be auto-generated. """ + # Chunk size for streaming files, taken from the twisted FileSender class + CHUNK_SIZE = 2 ** 14 + def __init__( self, root_directory: str = "/var/gemini", @@ -160,10 +165,11 @@ class StaticDirectoryApplication(JetforceApplication): Load a file in chunks to allow streaming to the TCP socket. """ with filesystem_path.open("rb") as fp: - data = fp.read(1024) - while data: + while True: + data = fp.read(self.CHUNK_SIZE) + if not data: + break yield data - data = fp.read(1024) def list_directory( self, url_path: pathlib.Path, filesystem_path: pathlib.Path