Overview
ServiceSwitcher is a specialized parallel pipeline that enables dynamic switching between multiple service instances at runtime. This is useful when you need to change between different STT providers, TTS providers, or other frame processors based on user preferences, costs, performance requirements, or other runtime conditions.
The switcher uses a strategy pattern to determine which service is active. Two built-in strategies are provided: manual switching for explicit control, and automatic failover for handling service errors.
How It Works
ServiceSwitcher wraps multiple services in a parallel pipeline where only the active service processes frames. Each service is “sandwiched” between two filters that check if it’s the currently active service before allowing frames to pass through. When you switch services, the filters update to redirect frame flow to the newly active service.
Constructor
List of service instances to switch between. Can be any frame processors (STT,
TTS, or custom processors).
The strategy class to use for switching logic. Pass the class itself, not an
instance. Defaults to
ServiceSwitcherStrategyManual.Switching Strategies
ServiceSwitcherStrategyManual
The manual strategy allows explicit control over which service is active by pushingManuallySwitchServiceFrame frames into the pipeline.
Initial State: The first service in the list is active by default.
Switching: Push a ManuallySwitchServiceFrame with the desired service instance.
ServiceSwitcherStrategyFailover
The failover strategy automatically switches to the next available service when the active service reports a non-fatal error. This enables automatic recovery from service failures without manual intervention. Initial State: The first service in the list is active by default. Automatic Failover: When the active service pushes a non-fatalErrorFrame, the strategy automatically switches to the next service in the list (wrapping around to the first service if needed).
Recovery: The failed service remains in the list and can be switched back to manually or via application logic in the on_service_switched event handler. This allows implementing custom recovery policies.
Custom Strategies
You can create your own switching strategy by subclassingServiceSwitcherStrategy and implementing the handle_frame and/or handle_error methods.
handle_frame(frame, direction): Called for control frames (likeManuallySwitchServiceFrame). Should return the newly active service if a switch occurred, orNoneotherwise.handle_error(error): Called when the active service reports a non-fatal error. Override this to implement custom error-handling logic. Should return the newly active service if a switch occurred, orNoneotherwise.
ServiceSwitcherStrategyManual or ServiceSwitcherStrategyFailover, respectively.
Usage Examples
Switching Between TTS Services
Event Handlers
| Event | Description |
|---|---|
on_service_switched | Active service was switched |
| Parameter | Type | Description |
|---|---|---|
switcher | ServiceSwitcherStrategy | The switcher instance |
service | FrameProcessor | The newly active service |