close sessions

This commit is contained in:
Roy Olav Purser 2021-05-14 23:09:16 +02:00
parent 0c171e84c8
commit d3df1c593a
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -89,12 +89,10 @@ class AsyncSessionData():
self.current_proxies = current_proxies self.current_proxies = current_proxies
self.current_proxy = current_proxy self.current_proxy = current_proxy
class AsyncSession(): class AsyncSession():
def __init__(self, session, resp, current_proxies, current_proxy): def __init__(self, resp, current_proxies, current_proxy):
self.session = session
self.sdata = AsyncSessionData(resp, current_proxies, current_proxy) self.sdata = AsyncSessionData(resp, current_proxies, current_proxy)
async def task(self): async def task(self):
resp = await self.sdata.resp resp = await self.sdata.resp
await self.session.close()
return AsyncSessionData(resp, self.sdata.current_proxies, self.sdata.current_proxy) return AsyncSessionData(resp, self.sdata.current_proxies, self.sdata.current_proxy)
proxies = {} proxies = {}
@ -150,12 +148,14 @@ class UpstreamHandler():
proxy_iter = proxy_list_orig.copy() proxy_iter = proxy_list_orig.copy()
if isinstance(proxy_list, list): if isinstance(proxy_list, list):
futures = [] futures = []
sessions = []
for i in proxy_iter: for i in proxy_iter:
current_list = proxy_list.copy() current_list = proxy_list.copy()
current = proxy_list.pop() current = proxy_list.pop()
proxy_list = [current] + proxy_list proxy_list = [current] + proxy_list
session = current.session() session = current.session()
future = AsyncSession(session, session.head(src), current_list, current) sessions.append(session)
future = AsyncSession(session.head(src), current_list, current)
task = asyncio.create_task(future.task()) task = asyncio.create_task(future.task())
futures.append(task) futures.append(task)
done = False done = False
@ -180,6 +180,8 @@ class UpstreamHandler():
for future in futures: for future in futures:
if not future.done(): if not future.done():
future.cancel() future.cancel()
for session in sessions:
await session.close()
async def meta(self): async def meta(self):
data = [] data = []