Skip to content

Commit

Permalink
[connection] Constant-time stream ID allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
XeCycle committed Sep 15, 2023
1 parent b097478 commit 19ba58d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/aioquic/quic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ def __init__(

if self._is_client:
self._original_destination_connection_id = self._peer_cid.cid
self._next_uni_id = 2
self._next_bidi_id = 0
else:
self._original_destination_connection_id = (
original_destination_connection_id
)
self._next_uni_id = 3
self._next_bidi_id = 1

# logging
self._logger = QuicConnectionAdapter(
Expand Down Expand Up @@ -623,10 +627,10 @@ def get_next_available_stream_id(self, is_unidirectional=False) -> int:
"""
Return the stream ID for the next stream created by this endpoint.
"""
stream_id = (int(is_unidirectional) << 1) | int(not self._is_client)
while stream_id in self._streams or stream_id in self._streams_finished:
stream_id += 4
return stream_id
if is_unidirectional:
return self._next_uni_id
else:
return self._next_bidi_id

def get_timer(self) -> Optional[float]:
"""
Expand Down Expand Up @@ -1291,12 +1295,17 @@ def _get_or_create_stream_for_send(self, stream_id: int) -> QuicStream:
streams_blocked = self._streams_blocked_bidi

# create stream
is_unidirectional = stream_is_unidirectional(stream_id)
stream = self._streams[stream_id] = QuicStream(
stream_id=stream_id,
max_stream_data_local=max_stream_data_local,
max_stream_data_remote=max_stream_data_remote,
readable=not stream_is_unidirectional(stream_id),
readable=not is_unidirectional
)
if is_unidirectional:
self._next_uni_id = stream_id + 4
else:
self._next_bidi_id = stream_id + 4

# mark stream as blocked if needed
if stream_id // 4 >= max_streams:
Expand Down

0 comments on commit 19ba58d

Please sign in to comment.