use std::sync::{Arc, OnceLock};
use dynamo_kv_router::plugins::worker_selection::WorkerSelectionPolicyFactory;
use dynamo_kv_router::plugins::{RouterPluginRegistry, RouterPlugins};
use dynamo_runtime::pipeline::{InstanceSelector, ModeSelectors, PushRouterOptions};
use super::{KvRouter, SelectionPolicySource};
static INSTALLED_PLUGINS: OnceLock<RouterPluginRegistry> = OnceLock::new();
fn host_default_factory() -> WorkerSelectionPolicyFactory {
dynamo_router_policies::default_factory()
}
pub fn install_router_plugin_registry(registry: RouterPluginRegistry) -> bool {
INSTALLED_PLUGINS
.set(
dynamo_router_policies::with_defaults(registry)
.with_default_factory(host_default_factory()),
)
.is_ok()
}
pub fn router_plugin_registry() -> RouterPluginRegistry {
INSTALLED_PLUGINS.get().cloned().unwrap_or_else(|| {
dynamo_router_policies::default_registry().with_default_factory(host_default_factory())
})
}
pub fn mode_selectors() -> ModeSelectors {
Arc::new(|mode| {
let picker = super::routing_host::hosted_picker(mode)
.inspect_err(|error| {
tracing::warn!(?mode, %error, "router-mode plugin unavailable");
})
.ok()?;
Some(Arc::new(picker) as Arc<dyn InstanceSelector>)
})
}
pub fn push_router_options<T, U>() -> PushRouterOptions<T, U> {
PushRouterOptions {
selectors: Some(mode_selectors()),
..PushRouterOptions::default()
}
}
#[derive(Clone, Default)]
pub struct RouterPluginBuilder {
plugins: RouterPlugins,
}
impl RouterPluginBuilder {
pub fn new(plugins: RouterPlugins) -> Self {
Self { plugins }
}
pub(crate) fn validate_config(
&self,
config: &dynamo_kv_router::KvRouterConfig,
) -> anyhow::Result<()> {
if config.request_classifier_config()?.is_some()
&& self.plugins.request_classifier().is_none()
{
anyhow::bail!(
"request_classifier is configured but not installed; supply a resolved RouterPlugins bundle"
);
}
Ok(())
}
pub(crate) fn has_custom_worker_selection(&self) -> bool {
self.plugins.has_custom_worker_selection()
}
pub(crate) fn selection_policy(&self) -> SelectionPolicySource {
self.plugins.worker_selection().cloned().map_or(
SelectionPolicySource::Registry,
SelectionPolicySource::Factory,
)
}
pub(crate) fn install(&self, router: &KvRouter) -> anyhow::Result<()> {
if let Some(factory) = self.plugins.request_classifier() {
router.install_request_classifier(factory(router.request_classifier_context()))?;
}
Ok(())
}
}