Skip to content

Commit 0cf51dd

Browse files
committed
Extract finish_body() method on HTTP parser
Refactor the body-drain logic out of Parser.__next__() into a public finish_body() method. No behavior change — __next__() calls it in the same place. This makes the drain available for future use (e.g. explicit keepalive body discard once thread pool exhaustion protection is added). Ported from gunicorn b43dc6d.
1 parent e7ddd1a commit 0cf51dd

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

plain/plain/server/http/parser.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,24 @@ def __init__(
4242
def __iter__(self) -> Iterator[Request]:
4343
return self
4444

45+
def finish_body(self) -> None:
46+
"""Discard any unread body of the current message.
47+
48+
Call before returning a keepalive connection to the poller so the
49+
socket doesn't appear readable due to leftover body bytes.
50+
"""
51+
if self.mesg and self.mesg.body:
52+
data = self.mesg.body.read(8192)
53+
while data:
54+
data = self.mesg.body.read(8192)
55+
4556
def __next__(self) -> Request:
4657
# Stop if HTTP dictates a stop.
4758
if self.mesg and self.mesg.should_close():
4859
raise StopIteration()
4960

5061
# Discard any unread body of the previous message
51-
if self.mesg and self.mesg.body:
52-
data = self.mesg.body.read(8192)
53-
while data:
54-
data = self.mesg.body.read(8192)
62+
self.finish_body()
5563

5664
# Parse the next request
5765
self.req_count += 1

0 commit comments

Comments
 (0)