Skip to content

Commit

Permalink
fix: try to fix session unclosed issue with debrid stream proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Jul 4, 2024
1 parent cf35fca commit 1f63113
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ async def playback(request: Request, b64config: str, hash: str, index: str):

async def stream_content(headers: dict):
async with aiohttp.ClientSession() as session:
response = await session.get(download_link, headers=headers)
while True:
chunk = await response.content.read(
settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK
) # 10 MB chunks
if not chunk:
break
yield chunk
async with session.get(download_link, headers=headers) as response:
while True:
chunk = await response.content.read(
settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK
) # 10 MB chunks
if not chunk:
break
yield chunk

range = None
range_header = request.headers.get("range")
Expand All @@ -314,19 +314,19 @@ async def stream_content(headers: dict):
end = int(end) if end else ""
range = f"bytes={start}-{end}"

response = await session.get(
download_link, headers={"Range": f"bytes={start}-{end}"}
)
if response.status == 206:
return StreamingResponse(
stream_content({"Range": range}),
status_code=206,
headers={
"Content-Range": response.headers["Content-Range"],
"Content-Length": response.headers["Content-Length"],
"Accept-Ranges": "bytes",
},
)
async with await session.get(download_link, headers={"Range": f"bytes={start}-{end}"}) as response:
await session.close()

if response.status == 206:
return StreamingResponse(
stream_content({"Range": range}),
status_code=206,
headers={
"Content-Range": response.headers["Content-Range"],
"Content-Length": response.headers["Content-Length"],
"Accept-Ranges": "bytes",
},
)

return

Expand Down

0 comments on commit 1f63113

Please sign in to comment.