catch reconnection failures, do exponential backoff
This commit is contained in:
parent
8d3681eba1
commit
ab17645d83
1 changed files with 16 additions and 6 deletions
|
@ -1,4 +1,4 @@
|
||||||
import asyncio
|
import asyncio, traceback
|
||||||
import anyio
|
import anyio
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
|
@ -13,20 +13,30 @@ class Bot(IBot):
|
||||||
self.servers: Dict[str, Server] = {}
|
self.servers: Dict[str, Server] = {}
|
||||||
self._server_queue: asyncio.Queue[Server] = asyncio.Queue()
|
self._server_queue: asyncio.Queue[Server] = asyncio.Queue()
|
||||||
|
|
||||||
# methods designed to be overridden
|
|
||||||
def create_server(self, name: str):
|
def create_server(self, name: str):
|
||||||
return Server(self, name)
|
return Server(self, name)
|
||||||
|
|
||||||
async def disconnected(self, server: IServer):
|
async def disconnected(self, server: IServer):
|
||||||
if (server.name in self.servers and
|
if (server.name in self.servers and
|
||||||
server.params is not None and
|
server.params is not None and
|
||||||
server.disconnected):
|
server.disconnected):
|
||||||
await asyncio.sleep(server.params.reconnect)
|
|
||||||
await self.add_server(server.name, server.params)
|
reconnect = server.params.reconnect
|
||||||
# /methods designed to be overridden
|
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(reconnect)
|
||||||
|
try:
|
||||||
|
await self.add_server(server.name, server.params)
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
# let's try again, exponential backoff up to 5 mins
|
||||||
|
reconnect = min(reconnect*2, 300)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
async def disconnect(self, server: IServer):
|
async def disconnect(self, server: IServer):
|
||||||
await server.disconnect()
|
|
||||||
del self.servers[server.name]
|
del self.servers[server.name]
|
||||||
|
await server.disconnect()
|
||||||
|
|
||||||
async def add_server(self, name: str, params: ConnectionParams) -> Server:
|
async def add_server(self, name: str, params: ConnectionParams) -> Server:
|
||||||
server = self.create_server(name)
|
server = self.create_server(name)
|
||||||
|
|
Loading…
Reference in a new issue