strangler_router

DEPRECATED (Phase T3, 2026-06-02) — retained for historical reference only.

During the monolith-to-microservices migration the strangler-fig router decided, per channel/guild, whether a message was served by the legacy monolith or routed over the event bus to the new services. The monolith has since been retired and all traffic flows through the microservices, so this module is no longer imported or instantiated by any service.

Do not import or use StranglerRouter in new code; it exists only to document the migration history. See core.strangler_router for the equivalent core-package copy.

class strangler_router.StranglerRouter(redis)[source]

Bases: object

Deprecated strangler-fig router that chose monolith vs. microservice.

During the monolith-to-microservices migration this resolved, per channel/guild/platform, whether a message was served by the legacy monolith or routed over the event bus to the new services, supporting a percentage-based rollout and a default fallback. Rules live in the sg:strangler:routes Redis hash and are cached in memory; a pub/sub listener on sg:pubsub:config flushes that cache when operator scripts rewrite the rules, and a CircuitBreaker guards the cache-miss Redis read so a flaky Redis degrades to the safe monolith default rather than raising.

Retired in Phase T3 once the monolith was confirmed dead: no live service constructs or imports this class anymore, and the only remaining callers are the migration test suite (tests/core/migration/test_strangler_router.py). Do not use it in new code; see core.strangler_router for the package copy.

__init__(redis)[source]

Initialize the router with a Redis client and an empty route cache.

Sets up the in-memory _cache dict that holds routing rules pulled from Redis, a CircuitBreaker (failure_threshold=3, reset_timeout=30.0) that guards the cache-miss path against a flaky Redis, an unset _listen_task handle for the background invalidation listener, and a _default_route of "monolith" used as the safe fallback.

This stores the passed redis handle but performs no I/O; the cache is populated lazily on the first _fetch_route() call. As this module is deprecated (Phase T3), no live service constructs this class; the only callers are historical and the test suite.

Parameters:

redis – An async Redis client (e.g. redis.asyncio.Redis) used for hgetall of the routes hash and for pub/sub subscription to config-change notifications.

async start()[source]

Launch the background cache-invalidation listener.

Schedules _listen_for_invalidations() as a fire-and-forget asyncio task whose handle is stored on self._listen_task so stop() can cancel it later. Returns immediately without awaiting the listener.

This calls asyncio.create_task(); the spawned task subscribes to the sg:pubsub:config Redis channel. Historically invoked by the owning GatewayService during startup, but that wiring was removed in Phase T3, so no live caller remains (only the test suite exercises it directly).

async stop()[source]

Cancel the background invalidation listener and await its teardown.

If a listener task was started, requests cancellation and awaits it, swallowing the resulting asyncio.CancelledError so shutdown completes cleanly. A no-op when start() was never called.

This cancels the task created in start() (which runs _listen_for_invalidations()). Historically called by GatewayService on shutdown; that wiring was removed in Phase T3, so only the test suite calls it now.

async get_route(channel=None, guild=None, platform=None)[source]

Resolve the routing target for a message, degrading safely on failure.

Public entry point for routing decisions: delegates to _fetch_route() through the circuit breaker so a struggling Redis trips the breaker instead of blocking, and converts every failure mode into the safe self._default_route ("monolith"). A tripped breaker logs strangler_circuit_open_fallback_monolith and any other exception logs strangler_route_error before falling back. As the module is deprecated (Phase T3), no live service calls this; only the migration test suite exercises it.

Parameters:
  • channel (str) – Optional channel identifier (highest-priority rule).

  • guild (str) – Optional guild/server identifier (second priority).

  • platform (str) – Optional platform name such as "discord" (third priority).

Returns:

The resolved route — typically "monolith", "microservice", or "shadow" — falling back to the default route on any error.

Return type:

str