mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-16 14:47:25 +08:00
refactor: replace ConfigSource with bool parameter (#1516)
This commit is contained in:
@@ -29,7 +29,7 @@ use easytier::{
|
||||
connector::create_connector_by_url,
|
||||
defer,
|
||||
instance_manager::NetworkInstanceManager,
|
||||
launcher::{add_proxy_network_to_config, ConfigSource},
|
||||
launcher::add_proxy_network_to_config,
|
||||
proto::common::{CompressionAlgoPb, NatType},
|
||||
rpc_service::ApiRpcServer,
|
||||
tunnel::{IpVersion, PROTO_PORT_OFFSET},
|
||||
@@ -1236,7 +1236,7 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
||||
println!("############### TOML ###############\n");
|
||||
println!("{}", cfg.dump());
|
||||
println!("-----------------------------------");
|
||||
manager.run_network_instance(cfg, ConfigSource::File)?;
|
||||
manager.run_network_instance(cfg, true)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1249,7 +1249,7 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
||||
println!("############### TOML ###############\n");
|
||||
println!("{}", cfg.dump());
|
||||
println!("-----------------------------------");
|
||||
manager.run_network_instance(cfg, ConfigSource::Cli)?;
|
||||
manager.run_network_instance(cfg, true)?;
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::{
|
||||
global_ctx::{EventBusSubscriber, GlobalCtxEvent},
|
||||
scoped_task::ScopedTask,
|
||||
},
|
||||
launcher::{ConfigSource, NetworkInstance, NetworkInstanceRunningInfo},
|
||||
launcher::{NetworkInstance, NetworkInstanceRunningInfo},
|
||||
proto::{self},
|
||||
rpc_service::InstanceRpcService,
|
||||
};
|
||||
@@ -37,32 +37,18 @@ impl NetworkInstanceManager {
|
||||
}
|
||||
|
||||
fn start_instance_task(&self, instance_id: uuid::Uuid) -> Result<(), anyhow::Error> {
|
||||
if tokio::runtime::Handle::try_current().is_err() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"tokio runtime not found, cannot start instance task"
|
||||
));
|
||||
}
|
||||
|
||||
let instance = self
|
||||
.instance_map
|
||||
.get(&instance_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("instance {} not found", instance_id))?;
|
||||
|
||||
match instance.get_config_source() {
|
||||
ConfigSource::FFI | ConfigSource::GUI => {
|
||||
// FFI and GUI have no tokio runtime, so we don't need to spawn a task
|
||||
return Ok(());
|
||||
}
|
||||
_ => {
|
||||
if tokio::runtime::Handle::try_current().is_err() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"tokio runtime not found, cannot start instance task"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let instance_stop_notifier = instance.get_stop_notifier();
|
||||
let instance_event_receiver = match instance.get_config_source() {
|
||||
ConfigSource::Cli | ConfigSource::File | ConfigSource::Web => {
|
||||
Some(instance.subscribe_event())
|
||||
}
|
||||
ConfigSource::GUI | ConfigSource::FFI => None,
|
||||
};
|
||||
let instance_event_receiver = instance.subscribe_event();
|
||||
|
||||
let instance_map = self.instance_map.clone();
|
||||
let instance_stop_tasks = self.instance_stop_tasks.clone();
|
||||
@@ -76,7 +62,6 @@ impl NetworkInstanceManager {
|
||||
return;
|
||||
};
|
||||
let _t = instance_event_receiver
|
||||
.flatten()
|
||||
.map(|event| ScopedTask::from(handle_event(instance_id, event)));
|
||||
instance_stop_notifier.notified().await;
|
||||
if let Some(instance) = instance_map.get(&instance_id) {
|
||||
@@ -97,18 +82,20 @@ impl NetworkInstanceManager {
|
||||
pub fn run_network_instance(
|
||||
&self,
|
||||
cfg: TomlConfigLoader,
|
||||
source: ConfigSource,
|
||||
watch_event: bool,
|
||||
) -> Result<uuid::Uuid, anyhow::Error> {
|
||||
let instance_id = cfg.get_id();
|
||||
if self.instance_map.contains_key(&instance_id) {
|
||||
anyhow::bail!("instance {} already exists", instance_id);
|
||||
}
|
||||
|
||||
let mut instance = NetworkInstance::new(cfg, source);
|
||||
let mut instance = NetworkInstance::new(cfg);
|
||||
instance.start()?;
|
||||
|
||||
self.instance_map.insert(instance_id, instance);
|
||||
self.start_instance_task(instance_id)?;
|
||||
if watch_event {
|
||||
self.start_instance_task(instance_id)?;
|
||||
}
|
||||
Ok(instance_id)
|
||||
}
|
||||
|
||||
@@ -429,32 +416,20 @@ mod tests {
|
||||
c.set_listeners(vec![format!("tcp://0.0.0.0:{}", port).parse().unwrap()]);
|
||||
})
|
||||
.unwrap(),
|
||||
ConfigSource::Cli,
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
let instance_id2 = manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::File,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true)
|
||||
.unwrap();
|
||||
let instance_id3 = manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::GUI,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false)
|
||||
.unwrap();
|
||||
let instance_id4 = manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::Web,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true)
|
||||
.unwrap();
|
||||
let instance_id5 = manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::FFI,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false)
|
||||
.unwrap();
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; // to make instance actually started
|
||||
@@ -489,16 +464,10 @@ mod tests {
|
||||
let port = crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
||||
|
||||
assert!(manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::Cli,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
|
||||
.is_err());
|
||||
assert!(manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::File,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
|
||||
.is_err());
|
||||
assert!(manager
|
||||
.run_network_instance(
|
||||
@@ -507,20 +476,14 @@ mod tests {
|
||||
c.set_listeners(vec![format!("tcp://0.0.0.0:{}", port).parse().unwrap()]);
|
||||
})
|
||||
.unwrap(),
|
||||
ConfigSource::GUI,
|
||||
false,
|
||||
)
|
||||
.is_ok());
|
||||
assert!(manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::Web,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), true,)
|
||||
.is_err());
|
||||
assert!(manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str).unwrap(),
|
||||
ConfigSource::FFI,
|
||||
)
|
||||
.run_network_instance(TomlConfigLoader::new_from_str(cfg_str).unwrap(), false,)
|
||||
.is_ok());
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_secs(1)); // wait instance actually started
|
||||
@@ -546,7 +509,8 @@ mod tests {
|
||||
let free_tcp_port =
|
||||
crate::utils::find_free_tcp_port(10012..65534).expect("no free tcp port found");
|
||||
|
||||
for config_source in [ConfigSource::Cli, ConfigSource::File] {
|
||||
// Test with event watching enabled (for CLI/File/RPC usage) - instance should auto-stop on error
|
||||
for watch_event in [true] {
|
||||
let _port_holder =
|
||||
std::net::TcpListener::bind(format!("0.0.0.0:{}", free_tcp_port)).unwrap();
|
||||
|
||||
@@ -561,7 +525,7 @@ mod tests {
|
||||
manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
|
||||
config_source.clone(),
|
||||
watch_event,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -570,11 +534,14 @@ mod tests {
|
||||
assert_eq!(manager.list_network_instance_ids().len(), 1);
|
||||
}
|
||||
_ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {
|
||||
panic!("instance manager with single failed instance({:?}) should not running", config_source);
|
||||
panic!("instance manager with single failed instance({:?}) should not running", watch_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
for config_source in [ConfigSource::Web, ConfigSource::GUI, ConfigSource::FFI] {
|
||||
|
||||
// Test without event watching (for FFI usage) - instance should remain even if failed
|
||||
{
|
||||
let watch_event = false;
|
||||
let _port_holder =
|
||||
std::net::TcpListener::bind(format!("0.0.0.0:{}", free_tcp_port)).unwrap();
|
||||
|
||||
@@ -589,7 +556,7 @@ mod tests {
|
||||
manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
|
||||
config_source.clone(),
|
||||
watch_event,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -616,7 +583,7 @@ mod tests {
|
||||
manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
|
||||
ConfigSource::Cli,
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -625,7 +592,7 @@ mod tests {
|
||||
manager
|
||||
.run_network_instance(
|
||||
TomlConfigLoader::new_from_str(cfg_str.as_str()).unwrap(),
|
||||
ConfigSource::Cli,
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -281,35 +281,19 @@ impl Drop for EasyTierLauncher {
|
||||
|
||||
pub type NetworkInstanceRunningInfo = crate::proto::api::manage::NetworkInstanceRunningInfo;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ConfigSource {
|
||||
Cli,
|
||||
File,
|
||||
Web,
|
||||
GUI,
|
||||
FFI,
|
||||
}
|
||||
|
||||
pub struct NetworkInstance {
|
||||
config: TomlConfigLoader,
|
||||
launcher: Option<EasyTierLauncher>,
|
||||
|
||||
config_source: ConfigSource,
|
||||
}
|
||||
|
||||
impl NetworkInstance {
|
||||
pub fn new(config: TomlConfigLoader, source: ConfigSource) -> Self {
|
||||
pub fn new(config: TomlConfigLoader) -> Self {
|
||||
Self {
|
||||
config,
|
||||
launcher: None,
|
||||
config_source: source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_source(&self) -> ConfigSource {
|
||||
self.config_source.clone()
|
||||
}
|
||||
|
||||
pub fn is_easytier_running(&self) -> bool {
|
||||
self.launcher.is_some() && self.launcher.as_ref().unwrap().running()
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::sync::Arc;
|
||||
use crate::{
|
||||
common::config::ConfigLoader,
|
||||
instance_manager::NetworkInstanceManager,
|
||||
launcher::ConfigSource,
|
||||
proto::{
|
||||
api::manage::*,
|
||||
rpc_types::{self, controller::BaseController},
|
||||
@@ -47,7 +46,7 @@ impl WebClientService for InstanceManageRpcService {
|
||||
if let Some(inst_id) = req.inst_id {
|
||||
cfg.set_id(inst_id.into());
|
||||
}
|
||||
self.manager.run_network_instance(cfg, ConfigSource::Web)?;
|
||||
self.manager.run_network_instance(cfg, true)?;
|
||||
println!("instance {} started", id);
|
||||
Ok(RunNetworkInstanceResponse {
|
||||
inst_id: Some(id.into()),
|
||||
|
||||
Reference in New Issue
Block a user