Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 75784e0

Browse files
committed
Simplify _reset logic
1 parent abe13b1 commit 75784e0

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

urllib3/sync_connection.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -463,33 +463,16 @@ def _reset(self):
463463
state machine and connection or not, and if not, closes the socket and
464464
state machine.
465465
"""
466-
# The logic here is as follows. Once we've got EndOfMessage, only two
467-
# things can be true. Either a) the connection is suitable for
468-
# connection re-use per RFC 7230, or b) it is not. h11 signals this
469-
# difference by what happens when you call `next_event()`.
470-
#
471-
# If the connection is safe to re-use, when we call `next_event()`
472-
# we'll get back a h11.NEED_DATA and the state machine will be reset to
473-
# (IDLE, IDLE). If it's not, we'll get either ConnectionClosed or we'll
474-
# find that our state is MUST_CLOSE, and then we should close the
475-
# connection accordingly.
476-
continue_states = (h11.IDLE, h11.DONE)
477-
event = self._state_machine.next_event()
478-
our_state = self._state_machine.our_state
479-
their_state = self._state_machine.their_state
480-
must_close = (
481-
event is not h11.NEED_DATA or
482-
our_state not in continue_states or
483-
their_state not in continue_states
484-
)
485-
if must_close:
486-
self.close()
487-
elif our_state is h11.DONE and their_state is h11.DONE:
466+
try:
488467
self._state_machine.start_next_cycle()
489-
# This connection can be returned to the connection pool, and
490-
# eventually we'll take it out again and want to know if it's been
491-
# dropped.
492-
self._sock.set_readable_watch_state(True)
468+
except h11.LocalProtocolError:
469+
# Not re-usable
470+
self.close()
471+
else:
472+
# This connection can be returned to the connection pool, and
473+
# eventually we'll take it out again and want to know if it's been
474+
# dropped.
475+
self._sock.set_readable_watch_state(True)
493476

494477
@property
495478
def complete(self):

0 commit comments

Comments
 (0)