Skip to content

Commit

Permalink
feat(node): support unavailable node handling (#98)
Browse files Browse the repository at this point in the history
* feat(node): support unavailable node handling

* chore(docs): add undocumented events
  • Loading branch information
penggin committed Dec 18, 2023
1 parent 1104d6b commit a26cfa4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ Callbacks

:param node: The node that sent the statistics.
:type node: :class:`Node`

.. function:: on_node_ready()

Called when Lavalink node is ready.

:param node: The node that was ready.
:type node: :class:`Node`

.. function:: on_node_unavailable()

Called when Lavalink node becomes unavailable.

:param node: The node that became unavailable.
:type node: :class:`Node`
2 changes: 2 additions & 0 deletions mafic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,9 @@ async def _ws_listener(self) -> None:

if _type is aiohttp.WSMsgType.CLOSED:
self._available = False
self._client.dispatch("node_unavailable", self)
close_code = self._ws.close_code
self._ready.clear()
self._ws = None

wait_time = backoff.delay()
Expand Down
9 changes: 6 additions & 3 deletions mafic/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def label_to_node(cls) -> dict[str, Node[ClientT]]:

@classproperty
def nodes(cls) -> list[Node[ClientT]]:
"""Get the list of all nodes."""
return list(cls._nodes.values())
"""Get the list of all available nodes."""
return list(filter(lambda n: n.available, cls._nodes.values()))

async def create_node(
self,
Expand Down Expand Up @@ -366,7 +366,10 @@ def get_random_node(cls) -> Node[ClientT]:
ValueError
If there are no nodes.
"""
if node := choice(list(cls._nodes.values())):
# It is a classproperty.
nodes = cast(list[Node[ClientT]], cls.nodes) # pyright: ignore # noqa: PGH003

if node := choice(nodes):
return node

raise NoNodesAvailable

0 comments on commit a26cfa4

Please sign in to comment.