update to aiorpcx 0.17
This commit is contained in:
@@ -5,7 +5,7 @@ protobuf
|
|||||||
dnspython
|
dnspython
|
||||||
jsonrpclib-pelix
|
jsonrpclib-pelix
|
||||||
qdarkstyle<2.6
|
qdarkstyle<2.6
|
||||||
aiorpcx>=0.9,<0.11
|
aiorpcx>=0.17,<0.18
|
||||||
aiohttp>=3.3.0
|
aiohttp>=3.3.0
|
||||||
aiohttp_socks
|
aiohttp_socks
|
||||||
certifi
|
certifi
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ from ipaddress import IPv4Network, IPv6Network, ip_address
|
|||||||
|
|
||||||
import aiorpcx
|
import aiorpcx
|
||||||
from aiorpcx import RPCSession, Notification
|
from aiorpcx import RPCSession, Notification
|
||||||
|
from aiorpcx.curio import timeout_after, TaskTimeout
|
||||||
import certifi
|
import certifi
|
||||||
|
|
||||||
from .util import PrintError, ignore_exceptions, log_exceptions, bfh, SilentTaskGroup
|
from .util import PrintError, ignore_exceptions, log_exceptions, bfh, SilentTaskGroup
|
||||||
@@ -72,10 +73,10 @@ class NotificationSession(RPCSession):
|
|||||||
super(NotificationSession, self).__init__(*args, **kwargs)
|
super(NotificationSession, self).__init__(*args, **kwargs)
|
||||||
self.subscriptions = defaultdict(list)
|
self.subscriptions = defaultdict(list)
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
self.in_flight_requests_semaphore = asyncio.Semaphore(100)
|
|
||||||
self.default_timeout = NetworkTimeout.Generic.NORMAL
|
self.default_timeout = NetworkTimeout.Generic.NORMAL
|
||||||
self._msg_counter = 0
|
self._msg_counter = 0
|
||||||
self.interface = None # type: Optional[Interface]
|
self.interface = None # type: Optional[Interface]
|
||||||
|
self.cost_hard_limit = 0 # disable aiorpcx resource limits
|
||||||
|
|
||||||
def _get_and_inc_msg_counter(self):
|
def _get_and_inc_msg_counter(self):
|
||||||
# runs in event loop thread, no need for lock
|
# runs in event loop thread, no need for lock
|
||||||
@@ -84,35 +85,40 @@ class NotificationSession(RPCSession):
|
|||||||
|
|
||||||
async def handle_request(self, request):
|
async def handle_request(self, request):
|
||||||
self.maybe_log(f"--> {request}")
|
self.maybe_log(f"--> {request}")
|
||||||
# note: if server sends malformed request and we raise, the superclass
|
try:
|
||||||
# will catch the exception, count errors, and at some point disconnect
|
if isinstance(request, Notification):
|
||||||
if isinstance(request, Notification):
|
params, result = request.args[:-1], request.args[-1]
|
||||||
params, result = request.args[:-1], request.args[-1]
|
key = self.get_hashable_key_for_rpc_call(request.method, params)
|
||||||
key = self.get_hashable_key_for_rpc_call(request.method, params)
|
if key in self.subscriptions:
|
||||||
if key in self.subscriptions:
|
self.cache[key] = result
|
||||||
self.cache[key] = result
|
for queue in self.subscriptions[key]:
|
||||||
for queue in self.subscriptions[key]:
|
await queue.put(request.args)
|
||||||
await queue.put(request.args)
|
else:
|
||||||
|
raise Exception(f'unexpected notification')
|
||||||
else:
|
else:
|
||||||
raise Exception('unexpected request: {}'.format(repr(request)))
|
raise Exception(f'unexpected request. not a notification')
|
||||||
|
except Exception as e:
|
||||||
|
self.interface.print_error(f"error handling request {request}. exc: {repr(e)}")
|
||||||
|
await self.close()
|
||||||
|
|
||||||
async def send_request(self, *args, timeout=None, **kwargs):
|
async def send_request(self, *args, timeout=None, **kwargs):
|
||||||
# note: the timeout starts after the request touches the wire!
|
# note: semaphores/timeouts/backpressure etc are handled by
|
||||||
if timeout is None:
|
# aiorpcx. the timeout arg here in most cases should not be set
|
||||||
timeout = self.default_timeout
|
msg_id = self._get_and_inc_msg_counter()
|
||||||
# note: the semaphore implementation guarantees no starvation
|
self.maybe_log(f"<-- {args} {kwargs} (id: {msg_id})")
|
||||||
async with self.in_flight_requests_semaphore:
|
try:
|
||||||
msg_id = self._get_and_inc_msg_counter()
|
response = await asyncio.wait_for(
|
||||||
self.maybe_log(f"<-- {args} {kwargs} (id: {msg_id})")
|
super().send_request(*args, **kwargs),
|
||||||
try:
|
timeout)
|
||||||
response = await asyncio.wait_for(
|
except (TaskTimeout, asyncio.TimeoutError) as e:
|
||||||
super().send_request(*args, **kwargs),
|
raise RequestTimedOut(f'request timed out: {args} (id: {msg_id})') from e
|
||||||
timeout)
|
else:
|
||||||
except asyncio.TimeoutError as e:
|
self.maybe_log(f"--> {response} (id: {msg_id})")
|
||||||
raise RequestTimedOut(f'request timed out: {args} (id: {msg_id})') from e
|
return response
|
||||||
else:
|
|
||||||
self.maybe_log(f"--> {response} (id: {msg_id})")
|
def set_default_timeout(self, timeout):
|
||||||
return response
|
self.sent_request_timeout = timeout
|
||||||
|
self.max_send_delay = timeout
|
||||||
|
|
||||||
async def subscribe(self, method: str, params: List, queue: asyncio.Queue):
|
async def subscribe(self, method: str, params: List, queue: asyncio.Queue):
|
||||||
# note: until the cache is written for the first time,
|
# note: until the cache is written for the first time,
|
||||||
@@ -212,10 +218,11 @@ class Interface(PrintError):
|
|||||||
auth = None
|
auth = None
|
||||||
else:
|
else:
|
||||||
auth = aiorpcx.socks.SOCKSUserAuth(username, pw)
|
auth = aiorpcx.socks.SOCKSUserAuth(username, pw)
|
||||||
|
addr = "{}:{}".format(proxy['host'], proxy['port'])
|
||||||
if proxy['mode'] == "socks4":
|
if proxy['mode'] == "socks4":
|
||||||
self.proxy = aiorpcx.socks.SOCKSProxy((proxy['host'], int(proxy['port'])), aiorpcx.socks.SOCKS4a, auth)
|
self.proxy = aiorpcx.socks.SOCKSProxy(addr, aiorpcx.socks.SOCKS4a, auth)
|
||||||
elif proxy['mode'] == "socks5":
|
elif proxy['mode'] == "socks5":
|
||||||
self.proxy = aiorpcx.socks.SOCKSProxy((proxy['host'], int(proxy['port'])), aiorpcx.socks.SOCKS5, auth)
|
self.proxy = aiorpcx.socks.SOCKSProxy(addr, aiorpcx.socks.SOCKS5, auth)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError # http proxy not available with aiorpcx
|
raise NotImplementedError # http proxy not available with aiorpcx
|
||||||
else:
|
else:
|
||||||
@@ -408,7 +415,7 @@ class Interface(PrintError):
|
|||||||
ssl=sslc, proxy=self.proxy) as session:
|
ssl=sslc, proxy=self.proxy) as session:
|
||||||
self.session = session # type: NotificationSession
|
self.session = session # type: NotificationSession
|
||||||
self.session.interface = self
|
self.session.interface = self
|
||||||
self.session.default_timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Generic)
|
self.session.set_default_timeout(self.network.get_network_timeout_seconds(NetworkTimeout.Generic))
|
||||||
try:
|
try:
|
||||||
ver = await session.send_request('server.version', [self.client_name(), version.PROTOCOL_VERSION])
|
ver = await session.send_request('server.version', [self.client_name(), version.PROTOCOL_VERSION])
|
||||||
except aiorpcx.jsonrpc.RPCError as e:
|
except aiorpcx.jsonrpc.RPCError as e:
|
||||||
@@ -620,9 +627,9 @@ class Interface(PrintError):
|
|||||||
def ip_addr(self) -> Optional[str]:
|
def ip_addr(self) -> Optional[str]:
|
||||||
session = self.session
|
session = self.session
|
||||||
if not session: return None
|
if not session: return None
|
||||||
peer_addr = session.peer_address()
|
peer_addr = session.remote_address()
|
||||||
if not peer_addr: return None
|
if not peer_addr: return None
|
||||||
return peer_addr[0]
|
return str(peer_addr.host)
|
||||||
|
|
||||||
def bucket_based_on_ipaddress(self) -> str:
|
def bucket_based_on_ipaddress(self) -> str:
|
||||||
def do_bucket():
|
def do_bucket():
|
||||||
|
|||||||
Reference in New Issue
Block a user