File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 3333from .frame_buffer import FrameBuffer
3434from .settings import Settings , SettingCodes
3535from .stream import H2Stream , StreamClosedBy
36- from .utilities import guard_increment_window
36+ from .utilities import SizeLimitDict , guard_increment_window
3737from .windows import WindowManager
3838
3939
@@ -281,6 +281,9 @@ class H2Connection(object):
281281 # The initial default value of SETTINGS_MAX_HEADER_LIST_SIZE.
282282 DEFAULT_MAX_HEADER_LIST_SIZE = 2 ** 16
283283
284+ # Keep in memory limited amount of results for streams closes
285+ MAX_CLOSED_STREAMS = 2 ** 31
286+
284287 def __init__ (self , config = None ):
285288 self .state_machine = H2ConnectionStateMachine ()
286289 self .streams = {}
@@ -355,7 +358,9 @@ def __init__(self, config=None):
355358 # Also used to determine whether we should consider a frame received
356359 # while a stream is closed as either a stream error or a connection
357360 # error.
358- self ._closed_streams = {}
361+ self ._closed_streams = SizeLimitDict (
362+ size_limit = self .MAX_CLOSED_STREAMS
363+ )
359364
360365 # The flow control window manager for the connection.
361366 self ._inbound_flow_control_window_manager = WindowManager (
Original file line number Diff line number Diff line change @@ -617,3 +617,22 @@ def validate_outbound_headers(headers, hdr_validation_flags):
617617 headers = _check_path_header (headers , hdr_validation_flags )
618618
619619 return headers
620+
621+
622+ class SizeLimitDict (collections .OrderedDict ):
623+
624+ def __init__ (self , * args , ** kwargs ):
625+ self ._size_limit = kwargs .pop ("size_limit" , None )
626+ super (SizeLimitDict , self ).__init__ (* args , ** kwargs )
627+
628+ self ._check_size_limit ()
629+
630+ def __setitem__ (self , key , value ):
631+ super (SizeLimitDict , self ).__setitem__ (key , value )
632+
633+ self ._check_size_limit ()
634+
635+ def _check_size_limit (self ):
636+ if self ._size_limit is not None :
637+ while len (self ) > self ._size_limit :
638+ self .popitem (last = False )
You can’t perform that action at this time.
0 commit comments