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 # Jetforce Changelog
### Unreleased
- Optimized chunking for streaming large files.
### v0.5.0 (2020-07-14) ### v0.5.0 (2020-07-14)
#### Spec Changes #### Spec Changes

View File

@ -6,6 +6,8 @@ import subprocess
import typing import typing
import urllib.parse import urllib.parse
from twisted.protocols.basic import FileSender
from .base import JetforceApplication, Request, Response, RoutePattern, Status from .base import JetforceApplication, Request, Response, RoutePattern, Status
@ -23,6 +25,9 @@ class StaticDirectoryApplication(JetforceApplication):
listing will be auto-generated. listing will be auto-generated.
""" """
# Chunk size for streaming files, taken from the twisted FileSender class
CHUNK_SIZE = 2 ** 14
def __init__( def __init__(
self, self,
root_directory: str = "/var/gemini", root_directory: str = "/var/gemini",
@ -160,10 +165,11 @@ class StaticDirectoryApplication(JetforceApplication):
Load a file in chunks to allow streaming to the TCP socket. Load a file in chunks to allow streaming to the TCP socket.
""" """
with filesystem_path.open("rb") as fp: with filesystem_path.open("rb") as fp:
data = fp.read(1024) while True:
while data: data = fp.read(self.CHUNK_SIZE)
if not data:
break
yield data yield data
data = fp.read(1024)
def list_directory( def list_directory(
self, url_path: pathlib.Path, filesystem_path: pathlib.Path self, url_path: pathlib.Path, filesystem_path: pathlib.Path