Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor keepalive #207

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions tremolo/lib/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def data_received(self, data):

if 1 < header_size <= self.options['client_max_header_size']:
# this will keep blocking on bodyless requests forever, unless
# Response.close is called (resumed in _handle_keepalive)
# Response.close is called (resumed in _send_data)
self.transport.pause_reading()

header = HTTPHeader(self._header_buf,
Expand Down Expand Up @@ -379,9 +379,16 @@ async def _send_data(self):
if data is None:
# close the transport, unless keepalive is enabled
if self.request is not None:
if self.request.http_continue:
self.request.http_continue = False
self.transport.resume_reading()
continue

if self.request.http_keepalive:
self.request.http_keepalive = False

await self._handle_keepalive()
self.transport.resume_reading()
continue

self.request.clear_body()
Expand Down Expand Up @@ -431,20 +438,15 @@ async def _handle_keepalive(self):
self.close()
return

if self.request.http_continue:
self.request.http_continue = False
elif self.request.upgraded:
if 'request' not in self._waiters:
self.close()
return

if self.request.upgraded:
self._waiters.setdefault('receive', self._waiters.pop('request'))
else:
# reset. so the next data in data_received will be considered as
# a fresh http request (not a continuation data)
if 'receive' in self._waiters:
# waits for all incoming data to enter the queue
await self._waiters.pop('receive')

# reset. so the next data in data_received will be considered as
# a fresh http request (not a continuation data)
self._header_buf = bytearray()
self.request.clear_body()

Expand All @@ -460,8 +462,6 @@ async def _handle_keepalive(self):
# this data is supposed to be the next header
self.data_received(self.queue[0].get_nowait())

self.transport.resume_reading()

def connection_lost(self, _):
while self.tasks:
task = self.tasks.pop()
Expand Down
Loading