Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 5861058

Browse files
committed
Clean up connection objects better when closed.
Connections need to be reset to their basic state when they get closed, in order to enable their reuse.
1 parent 545dead commit 5861058

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

hyper/http20/connection.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ def __init__(self, host, port=None, **kwargs):
4848
else:
4949
self.host, self.port = host, port
5050

51+
# Create the mutable state.
52+
self.__init_state()
53+
54+
return
55+
56+
def __init_state(self):
57+
"""
58+
Initializes the 'mutable state' portions of the HTTP/2.0 connection
59+
object.
60+
61+
This method exists to enable HTTP20Connection objects to be reused if
62+
they're closed, by resetting the connection object to its basic state
63+
whenever it ends up closed. Any situation that needs to recreate the
64+
connection can call this method and it will be done.
65+
66+
This is one of the only methods in hyper that is truly private, as
67+
users should be strongly discouraged from messing about with connection
68+
objects themselves.
69+
"""
5170
# Streams are stored in a dictionary keyed off their stream IDs. We
5271
# also save the most recent one for easy access without having to walk
5372
# the dictionary.
@@ -156,7 +175,7 @@ def close(self):
156175
# do.
157176
if self._sock is not None:
158177
self._sock.close()
159-
self._sock = None
178+
self.__init_state()
160179

161180
def putrequest(self, method, selector, **kwargs):
162181
"""

test/test_hyper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,26 @@ def test_putrequest_sends_data(self):
821821
assert len(sock.queue) == 2
822822
assert c._out_flow_control_window == 65535 - len(b'hello')
823823

824+
def test_closed_connections_are_reset(self):
825+
c = HTTP20Connection('www.google.com')
826+
c._sock = DummySocket()
827+
encoder = c.encoder
828+
decoder = c.decoder
829+
c.request('GET', '/')
830+
c.close()
831+
832+
assert c._sock is None
833+
assert not c.streams
834+
assert c.recent_stream is None
835+
assert c.next_stream_id == 1
836+
assert c.encoder is not encoder
837+
assert c.decoder is not decoder
838+
assert c._settings == {
839+
SettingsFrame.INITIAL_WINDOW_SIZE: 65535,
840+
}
841+
assert c._out_flow_control_window == 65535
842+
assert c._in_flow_control_window == 65535
843+
824844

825845
class TestHyperStream(object):
826846
def test_streams_have_ids(self):
@@ -1077,6 +1097,9 @@ def send(self, data):
10771097
def recv(self, l):
10781098
return self.buffer.read(l)
10791099

1100+
def close(self):
1101+
pass
1102+
10801103
class DummyStream(object):
10811104
def __init__(self, data):
10821105
self.data = data

0 commit comments

Comments
 (0)