Fix logic for recreating connections

This commit is contained in:
syeopite 2021-05-23 04:15:40 -07:00
parent 5180aa1192
commit 26c0765bb0
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82

View file

@ -98,7 +98,7 @@ class HttpClient(QuicConnectionProtocol):
self._http.send_headers(
stream_id=stream_id,
headers=[(k, v)for k, v in headers.items()],
headers=[(k, v) for k, v in headers.items()],
)
self._http.send_data(stream_id=stream_id, data=http_request.content, end_stream=True)
@ -186,21 +186,27 @@ class RequestProcessor:
async def request_worker(self):
configuration = QuicConfiguration(is_client=True, alpn_protocols=H3_ALPN)
while self.recreate_connection_check():
while await self.recreate_connection_check():
async with connect("youtube.com", 443, configuration=configuration, create_protocol=HttpClient) as client:
while client._quic._state is not ConnectionTerminated:
await self._handle_request(client)
while True:
status = await self._handle_request(client)
# Connection has been terminated.
if status is False:
break
async def _handle_request(self, client):
request, storage = await self.requests_to_do.get()
if client._quic._state is ConnectionTerminated:
return False
await perform_http_request(client=client, url=request.url, method=request.method,
headers=request.headers, data=request.content,
store_at=storage)
request.completed.set()
def recreate_connection_check(self):
async def recreate_connection_check(self):
# TODO in the future this code should calculate whether or not to recreate a connection based on the amount
# of connections currently available and the amount of traffic we're currently receiving. For now we'll
# just have it recreate a connection anytime it's broken.