mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-27 16:45:19 +00:00
Handle proxy socket connections using thread executors
This commit is contained in:
parent
1fa6f3b924
commit
2dca5aeac2
@ -20,6 +20,7 @@ import asyncio
|
||||
import ipaddress
|
||||
import logging
|
||||
import socket
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import socks
|
||||
|
||||
@ -38,6 +39,8 @@ class TCP:
|
||||
self.lock = asyncio.Lock()
|
||||
self.loop = asyncio.get_event_loop()
|
||||
|
||||
self.proxy = proxy
|
||||
|
||||
if proxy:
|
||||
hostname = proxy.get("hostname")
|
||||
|
||||
@ -71,6 +74,10 @@ class TCP:
|
||||
self.socket.setblocking(False)
|
||||
|
||||
async def connect(self, address: tuple):
|
||||
if self.proxy:
|
||||
with ThreadPoolExecutor(1) as executor:
|
||||
await self.loop.run_in_executor(executor, self.socket.connect, address)
|
||||
else:
|
||||
try:
|
||||
await asyncio.wait_for(asyncio.get_event_loop().sock_connect(self.socket, address), TCP.TIMEOUT)
|
||||
except asyncio.TimeoutError: # Re-raise as TimeoutError. asyncio.TimeoutError is deprecated in 3.11
|
||||
|
Loading…
Reference in New Issue
Block a user