diff --git a/rivetkit-rust/packages/rivetkit-core/src/engine_process.rs b/rivetkit-rust/packages/rivetkit-core/src/engine_process.rs index 345b8e04dc..c29b992129 100644 --- a/rivetkit-rust/packages/rivetkit-core/src/engine_process.rs +++ b/rivetkit-rust/packages/rivetkit-core/src/engine_process.rs @@ -4,6 +4,7 @@ //! re-exports it for existing in-crate callers. pub use rivetkit_engine_process::{ - EngineProcessError, EngineProcessManager, EngineResolverConfig, ResolvedEngine, engine_db_path, - engine_env, resolve_engine_binary, resolve_engine_binary_path, + EngineProcessError, EngineProcessManager, EngineResolverConfig, ResolvedEngine, + default_engine_instance_path, engine_db_path, engine_env, resolve_engine_binary, + resolve_engine_binary_path, }; diff --git a/rivetkit-rust/packages/rivetkit-core/src/registry/mod.rs b/rivetkit-rust/packages/rivetkit-core/src/registry/mod.rs index 396ec5158b..7bb68a3bfe 100644 --- a/rivetkit-rust/packages/rivetkit-core/src/registry/mod.rs +++ b/rivetkit-rust/packages/rivetkit-core/src/registry/mod.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::env; use std::io::Cursor; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; @@ -342,6 +342,8 @@ fn is_loopback_endpoint(endpoint: &str) -> Result { #[cfg(test)] mod engine_spawn_tests { use super::{EngineSpawnMode, should_manage_engine}; + #[cfg(feature = "native-runtime")] + use super::managed_engine_instance_path; #[test] fn auto_manages_loopback_endpoints() { @@ -362,6 +364,15 @@ mod engine_spawn_tests { assert!(should_manage_engine("https://api.rivet.dev", EngineSpawnMode::Always).unwrap()); assert!(!should_manage_engine("http://127.0.0.1:6420", EngineSpawnMode::Never).unwrap()); } + + #[cfg(feature = "native-runtime")] + #[test] + fn non_default_ports_get_isolated_managed_instance_paths() { + assert_eq!( + managed_engine_instance_path(std::path::Path::new("/tmp/rivetkit/var/engine"), 6421), + std::path::Path::new("/tmp/rivetkit/var/engine/instances").join("6421") + ); + } } #[derive(Debug, Deserialize)] @@ -586,16 +597,7 @@ impl CoreRegistry { let dispatcher = self.into_dispatcher(&config); #[cfg(feature = "native-runtime")] let _engine_process = if should_manage_engine(&config.endpoint, config.engine_spawn)? { - Some( - EngineProcessManager::start_or_reuse(EngineResolverConfig::from_parts( - &config.endpoint, - config.engine_binary_path.clone(), - config.engine_host.clone(), - config.engine_port, - config.engine_auto_download, - )) - .await?, - ) + Some(EngineProcessManager::start_or_reuse(engine_resolver_config(&config)?).await?) } else { None }; @@ -681,6 +683,38 @@ impl CoreRegistry { } } +#[cfg(feature = "native-runtime")] +pub(crate) fn engine_resolver_config(config: &ServeConfig) -> Result { + let mut resolver = EngineResolverConfig::from_parts( + &config.endpoint, + config.engine_binary_path.clone(), + config.engine_host.clone(), + config.engine_port, + config.engine_auto_download, + ); + let port = match config.engine_port { + Some(port) => port, + None => Url::parse(&config.endpoint) + .with_context(|| format!("parse engine endpoint `{}`", config.endpoint))? + .port_or_known_default() + .context("engine endpoint is missing a port")?, + }; + if port != 6420 { + resolver.instance_path = Some(managed_engine_instance_path( + &crate::engine_process::default_engine_instance_path()?, + port, + )); + } + Ok(resolver) +} + +#[cfg(feature = "native-runtime")] +fn managed_engine_instance_path(default_instance_path: &Path, port: u16) -> PathBuf { + default_instance_path + .join("instances") + .join(port.to_string()) +} + impl RegistryDispatcher { pub(crate) fn new( factories: HashMap>, diff --git a/rivetkit-rust/packages/rivetkit-core/src/serverless.rs b/rivetkit-rust/packages/rivetkit-core/src/serverless.rs index 453ac6d056..b65839d260 100644 --- a/rivetkit-rust/packages/rivetkit-core/src/serverless.rs +++ b/rivetkit-rust/packages/rivetkit-core/src/serverless.rs @@ -20,7 +20,9 @@ use url::Url; use crate::actor::factory::ActorFactory; #[cfg(feature = "native-runtime")] -use crate::engine_process::{EngineProcessManager, EngineResolverConfig}; +use crate::engine_process::EngineProcessManager; +#[cfg(feature = "native-runtime")] +use crate::registry::engine_resolver_config; use crate::registry::{ CoreEnvoyHandle, CoreEnvoyStatus, RegistryCallbacks, RegistryDispatcher, ServeConfig, should_manage_engine, @@ -158,16 +160,7 @@ impl CoreServerlessRuntime { ) -> Result { #[cfg(feature = "native-runtime")] let engine_process = if should_manage_engine(&config.endpoint, config.engine_spawn)? { - Some( - EngineProcessManager::start_or_reuse(EngineResolverConfig::from_parts( - &config.endpoint, - config.engine_binary_path.clone(), - config.engine_host.clone(), - config.engine_port, - config.engine_auto_download, - )) - .await?, - ) + Some(EngineProcessManager::start_or_reuse(engine_resolver_config(&config)?).await?) } else { None }; diff --git a/rivetkit-typescript/packages/rivetkit/src/registry/index.ts b/rivetkit-typescript/packages/rivetkit/src/registry/index.ts index 2114d01cb5..f0486c5789 100644 --- a/rivetkit-typescript/packages/rivetkit/src/registry/index.ts +++ b/rivetkit-typescript/packages/rivetkit/src/registry/index.ts @@ -417,7 +417,7 @@ export class Registry { ); } this.#installSignalHandlers(config); - this.#printWelcome(config, "serverful", { + this.#printWelcome(config, "envoy", { port, host: opts.host, publicDir, @@ -614,7 +614,7 @@ export class Registry { this.#installSignalHandlers(config); } if (printWelcome) { - this.#printWelcome(config, "serverful"); + this.#printWelcome(config, "envoy"); } } @@ -803,7 +803,7 @@ export class Registry { } /** - * Starts the serverful registry if needed and waits until its envoy has + * Starts the envoy registry if needed and waits until its envoy has * registered with the Engine. Repeated and concurrent calls share one * startup lifecycle and readiness promise. * @@ -915,7 +915,7 @@ export class Registry { #printWelcome( config: RegistryConfig, - kind: "serverless" | "serverful", + kind: "envoy" | "serverless", listener?: { port: number; host?: string; publicDir?: string }, ): void { if (config.noWelcome || this.#welcomePrinted) return; @@ -927,9 +927,8 @@ export class Registry { }; console.log(); - console.log( - ` RivetKit ${VERSION} (Engine - ${kind === "serverless" ? "Serverless" : "Serverful"})`, - ); + console.log(` RivetKit ${VERSION}`); + logLine("Mode", kind); if (config.namespace !== "default") { logLine("Namespace", config.namespace);