Skip to main content

dynamo_runtime/routing_policy/
selector.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! How `PushRouter` picks an instance for its routing modes.
5//!
6//! The runtime ships no selection algorithm for round-robin, random or least-loaded. Whoever builds a `PushRouter` passes [`ModeSelectors`] in
7//! [`PushRouterOptions`](crate::pipeline::PushRouterOptions); Dynamo's hosts back them with
8//! the router-mode plugins.
9
10use std::sync::Arc;
11
12use crate::pipeline::RouterMode;
13
14/// Picks one instance for a `PushRouter` routing mode.
15///
16/// `PushRouter` calls [`select_instance`](Self::select_instance) inside its admission lock
17/// when [`reads_active_requests`](Self::reads_active_requests) is true, so it must not block.
18pub trait InstanceSelector: Send + Sync {
19    /// Whether selection reads in-flight request counts. `PushRouter` then tracks them and
20    /// reserves the chosen instance.
21    fn reads_active_requests(&self) -> bool;
22
23    /// Pick one of `instance_ids`, which are ascending and non-empty. A preview must not
24    /// advance any state, such as a rotation.
25    fn select_instance(
26        &self,
27        instance_ids: &[u64],
28        active_requests: &dyn Fn(u64) -> u64,
29        preview: bool,
30    ) -> Option<u64>;
31}
32
33/// Builds a fresh selector for a routing mode, or `None` when it has none for that mode.
34pub type ModeSelectors = Arc<dyn Fn(RouterMode) -> Option<Arc<dyn InstanceSelector>> + Send + Sync>;