Change chunk size

This commit is contained in:
Michael Lazar 2020-07-14 00:46:22 -04:00
parent f59de7e9f0
commit 57556e09f5
2 changed files with 13 additions and 3 deletions

View File

@ -1,5 +1,9 @@
# Jetforce Changelog
### Unreleased
- Optimized chunking for streaming large files.
### v0.5.0 (2020-07-14)
#### Spec Changes

View File

@ -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