From 758a5bb04f5cb537a1afb712f837b434513a8157 Mon Sep 17 00:00:00 2001 From: David Buckley Date: Wed, 21 Aug 2024 18:00:26 +0100 Subject: [PATCH] add ConstantRouter --- lib/carbon/routers.py | 23 +++++++++++++++++++++++ lib/carbon/tests/test_routers.py | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/carbon/routers.py b/lib/carbon/routers.py index 9b8d8ef1..72d93cfa 100644 --- a/lib/carbon/routers.py +++ b/lib/carbon/routers.py @@ -31,6 +31,29 @@ def getDestinations(self, key): raise NotImplementedError() +class ConstantRouter(DatapointRouter): + plugin_name = 'constant' + + def __init__(self, settings): + self.destinations = set() + + def addDestination(self, destination): + self.destinations.add(destination) + + def removeDestination(self, destination): + self.destinations.discard(destination) + + def hasDestination(self, destination): + return destination in self.destinations + + def countDestinations(self): + return len(self.destinations) + + def getDestinations(self, key): + for destination in self.destinations: + yield destination + + class RelayRulesRouter(DatapointRouter): plugin_name = 'rules' diff --git a/lib/carbon/tests/test_routers.py b/lib/carbon/tests/test_routers.py index f6309e8f..8cc3e476 100644 --- a/lib/carbon/tests/test_routers.py +++ b/lib/carbon/tests/test_routers.py @@ -54,5 +54,5 @@ def testBasic(self): router.addDestination(parseDestination(destination)) self.assertEqual( len(list(router.getDestinations('foo.bar'))), - settings['REPLICATION_FACTOR'] + len(DESTINATIONS) if plugin == 'constant' else settings['REPLICATION_FACTOR'] )