From f25b84f6a2a68dc7297cac3cfec53d8b367bcd36 Mon Sep 17 00:00:00 2001 From: Oliver Wilkes Date: Sun, 4 Jun 2023 14:29:11 +0100 Subject: [PATCH] fix(strategy): properly match all regions to a node (#93) --- mafic/pool.py | 25 ------------------------- mafic/strategy.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/mafic/pool.py b/mafic/pool.py index 5af0206..b1cf9eb 100644 --- a/mafic/pool.py +++ b/mafic/pool.py @@ -65,8 +65,6 @@ class NodePool(Generic[ClientT]): # Generics as expected, do not work in class variables. # Don't fear, the public methods using this are typed well. _nodes: ClassVar[dict[str, Node[Any]]] = {} - _node_regions: ClassVar[dict[VoiceRegion, set[Node[Any]]]] = {} - _node_shards: ClassVar[dict[int, set[Node[Any]]]] = {} _client: ClientT | None = None def __init__( @@ -218,21 +216,6 @@ async def add_node( .. versionadded:: 2.8 """ - # Add to dictionaries, creating a set or extending it if needed. - if node.regions: - for region in node.regions: - self._node_regions[region] = { - node, - *self._node_regions.get(region, set()), - } - - if node.shard_ids: - for shard_id in node.shard_ids: - self._node_shards[shard_id] = { - node, - *self._node_shards.get(shard_id, set()), - } - _log.info("Created node, connecting it...", extra={"label": node.label}) await node.connect(player_cls=player_cls) @@ -255,14 +238,6 @@ async def remove_node( if isinstance(node, str): node = self._nodes[node] - if node.regions: - for region in node.regions: - self._node_regions[region].remove(node) - - if node.shard_ids: - for shard_id in node.shard_ids: - self._node_shards[shard_id].remove(node) - # Remove prematurely so it is not chosen. del self._nodes[node.label] diff --git a/mafic/strategy.py b/mafic/strategy.py index 7e081c8..b44e777 100644 --- a/mafic/strategy.py +++ b/mafic/strategy.py @@ -11,6 +11,7 @@ from typing import List, Optional from .node import Node +from .region import VoiceRegion from .type_variables import ClientT StrategyCallable = Callable[ @@ -107,6 +108,17 @@ def location_strategy( ) return nodes + try: + voice_region = VoiceRegion(voice_region) + except ValueError: + _log.error( + "Failed to match endpoint %s (match: %s) to a region, " + "defaulting to all nodes.", + endpoint, + voice_region, + ) + return nodes + regional_nodes = list( filter( lambda node: node.regions is not None and voice_region in node.regions,