Skip to content

Commit bd49842

Browse files
committed
feat: add node- prefix to auto-generated client instance id for better identification
1 parent 6b59c6e commit bd49842

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎src/config.rs‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,23 @@ fn default_data_dir() -> Option<String> {
9797
/// 生成客户端实例 ID
9898
///
9999
/// # 规则
100-
/// - 使用主机名作为实例 ID
101-
/// - 如果主机名为空或获取失败,使用 "unknown-node"
100+
/// - 添加 `node-` 前缀以明确标识节点实例
101+
/// - 使用主机名作为实例标识(物理机为实际主机名,容器为容器 ID)
102+
/// - 如果主机名为空或获取失败,使用 `node-unknown`
102103
/// - 保证每台服务器有唯一的实例 ID
104+
///
105+
/// # 示例
106+
/// - 物理机: `node-prod-server-01`
107+
/// - Docker: `node-636b25ce7983`
108+
/// - Fallback: `node-unknown`
103109
pub fn generate_client_instance_id() -> String {
104-
hostname::get()
110+
let host = hostname::get()
105111
.ok()
106112
.and_then(|h| h.into_string().ok())
107113
.filter(|h| !h.is_empty())
108-
.unwrap_or_else(|| "unknown-node".to_string())
114+
.unwrap_or_else(|| "unknown".to_string());
115+
116+
format!("node-{}", host)
109117
}
110118

111119
impl NodeTokenConfig {
@@ -421,11 +429,17 @@ max_concurrent_tasks = 4
421429
fn test_generate_client_instance_id() {
422430
// 测试自动生成实例 ID
423431
let instance_id = generate_client_instance_id();
424-
425-
// 应该不为空
426-
assert!(!instance_id.is_empty());
427-
428-
// 应该是有效的主机名或 fallback 值
429-
assert!(instance_id == "unknown-node" || !instance_id.contains(char::is_whitespace));
432+
433+
// 应该以 "node-" 前缀开头
434+
assert!(instance_id.starts_with("node-"));
435+
436+
// 应该不为空(至少有 "node-" 前缀)
437+
assert!(instance_id.len() > 5);
438+
439+
// 不应该包含空格
440+
assert!(!instance_id.contains(char::is_whitespace));
441+
442+
// 应该是 "node-unknown" 或 "node-{hostname}"
443+
assert!(instance_id == "node-unknown" || instance_id.len() > 5);
430444
}
431445
}

‎src/main.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ async fn main() -> Result<()> {
3535
let client_instance_id = node_token::config::generate_client_instance_id();
3636
info!("Configuration loaded successfully");
3737
info!("Server URL: {}", config.server_url);
38-
info!("Client instance ID: {} (auto-generated from hostname)", client_instance_id);
38+
info!(
39+
"Client instance ID: {} (auto-generated from hostname)",
40+
client_instance_id
41+
);
3942
info!("Display name: {}", config.display_name);
4043
info!("Ollama URL: {}", config.ollama_url);
4144

‎src/runtime/register.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ pub async fn register_node(
4646

4747
// 3. 构建注册请求
4848
let client_instance_id = crate::config::generate_client_instance_id();
49-
info!("Using client instance ID: {} (auto-generated from hostname)", client_instance_id);
50-
49+
info!(
50+
"Using client instance ID: {} (auto-generated from hostname)",
51+
client_instance_id
52+
);
53+
5154
let req = NodeRegisterRequest {
5255
protocol_version: "node.v1".to_string(),
5356
client_instance_id,

0 commit comments

Comments
 (0)