Skip to content

Commit

Permalink
optimize sendfile, avoid large read size
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Oct 13, 2023
1 parent a1f98fb commit e134cff
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tremolo/lib/http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async def write(self, data, buffer_size=16 * 1024, **kwargs):
async def sendfile(
self,
path,
buffer_size=16384,
buffer_size=16 * 1024,
content_type=b'application/octet-stream'
):
try:
Expand Down Expand Up @@ -341,9 +341,12 @@ async def sendfile(
b'Content-Range', b'bytes %d-%d/%d' % (
start, end, file_size)
)

handle.seek(start)
await self.write(handle.read(size), chunked=False)

while size > 0:
await self.write(handle.read(min(size, buffer_size)),
chunked=False)
size -= buffer_size
else:
client = self._request.client

Expand All @@ -369,9 +372,13 @@ async def sendfile(
b'Content-Range: bytes %d-%d/%d\r\n\r\n' % (
boundary, content_type, start, end, file_size)
)

handle.seek(start)
await self.write(b'%s\r\n' % handle.read(size))

while size > 0:
await self.write(handle.read(min(size, buffer_size)))
size -= buffer_size

await self.write(b'\r\n')

await self.write(b'--%s--\r\n' % boundary)
await self.write(b'')
Expand Down

0 comments on commit e134cff

Please sign in to comment.