Implementation:Tensorflow Serving source router h
| Knowledge Sources | |
|---|---|
| Domains | Model Serving, Core Framework |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
SourceRouter is an abstract base class that splits aspired-version calls from a single input to multiple numbered output ports based on a routing criterion.
Description
The SourceRouter<T> class template extends TargetBase<T> and acts as a demultiplexer in the serving pipeline. It receives aspired-version calls on a single input (via SetAspiredVersions()) and routes each call to one of N output ports based on a subclass-defined Route() method. Each output port is a Source<T> implemented internally as an IdentitySourceAdapter.
A typical use case is a server hosting multiple kinds of servables (e.g., "apple" and "orange" models) arriving via file system paths. A SourceRouter can be placed between a path-monitoring Source and multiple SourceAdapters, routing each path to the appropriate adapter based on the servable name.
Subclasses must implement:
num_output_ports()- Returns the number of output ports (must be > 0 and constant).Route()- Returns the output port number for a given servable name, orkNoRouteto silently discard.
The output ports are created lazily upon the first call to GetOutputPorts(), and SetAspiredVersions() blocks until the ports are created. Every leaf derived class must call Detach() at the top of its destructor.
Usage
Extend SourceRouter when you need to split a single stream of aspired-version calls into multiple downstream targets based on some routing logic. Use GetOutputPorts() to get the output Source pointers and connect them to downstream targets.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File: tensorflow_serving/core/source_router.h
- Lines: 1-174
Signature
template <typename T>
class SourceRouter : public TargetBase<T> {
public:
static constexpr int kNoRoute = -1;
~SourceRouter() override = 0;
std::vector<Source<T>*> GetOutputPorts();
void SetAspiredVersions(const StringPiece servable_name,
std::vector<ServableData<T>> versions) final;
protected:
SourceRouter() = default;
virtual int num_output_ports() const = 0;
virtual int Route(const StringPiece servable_name,
const std::vector<ServableData<T>>& versions) = 0;
};
Import
#include "tensorflow_serving/core/source_router.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| servable_name | const StringPiece | Yes | Name of the servable stream to route |
| versions | std::vector<ServableData<T>> | Yes | The aspired versions to route to an output port |
Outputs
| Name | Type | Description |
|---|---|---|
| GetOutputPorts() | std::vector<Source<T>*> | Pointers to N output port Sources; each must be connected to a downstream Target |
| Route() | int | Output port number in [0, num_output_ports()-1], or kNoRoute to discard |
Usage Examples
Using a SourceRouter with Downstream Targets
#include "tensorflow_serving/core/source_router.h"
using namespace tensorflow::serving;
// Assume router is a concrete SourceRouter subclass
auto output_ports = router->GetOutputPorts();
// Connect each output port to a different target
ConnectSourceToTarget(output_ports[0], apple_adapter);
ConnectSourceToTarget(output_ports[1], orange_adapter);
// Connect the input source to the router
ConnectSourceToTarget(source, router);