优化: ssh-proxy russh 迁移后代码整理
- 移除未使用的 async-trait 依赖 - 添加断线重连逻辑,会话失效时自动重连 - 修复 get_or_create_session TOCTOU 竞态条件 - 日志智能分级: 慢请求告警、退出码识别 - 用 time crate 替换手写日期计算 (删除40行) - UTF-8 安全截断修复 - 同步优化 mysql-proxy 日志模块
This commit is contained in:
@@ -14,6 +14,7 @@ toml = "0.8"
|
||||
anyhow = "1"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
ureq = "2" # CLI HTTP client (2.x has simpler API)
|
||||
time = { version = "0.3", features = ["formatting", "macros"] }
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// 日志记录器
|
||||
pub struct RequestLogger {
|
||||
@@ -136,6 +135,12 @@ impl LogEntry {
|
||||
|
||||
pub fn with_duration(mut self, ms: u64) -> Self {
|
||||
self.duration_ms = ms;
|
||||
// 慢请求告警: >2s 为 WARN, >5s 为 ERROR
|
||||
if ms > 5000 && self.level == "INFO" {
|
||||
self.level = "ERROR".to_string();
|
||||
} else if ms > 2000 && self.level == "INFO" {
|
||||
self.level = "WARN".to_string();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
@@ -157,62 +162,16 @@ impl LogEntry {
|
||||
}
|
||||
|
||||
fn current_timestamp() -> String {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default();
|
||||
let secs = now.as_secs();
|
||||
let datetime = chrono_timestamp(secs);
|
||||
format!("{}Z", datetime)
|
||||
}
|
||||
|
||||
fn chrono_timestamp(secs: u64) -> String {
|
||||
let days = secs / 86400;
|
||||
let remaining = secs % 86400;
|
||||
let hours = remaining / 3600;
|
||||
let minutes = (remaining % 3600) / 60;
|
||||
let seconds = remaining % 60;
|
||||
|
||||
// 从 1970-01-01 开始计算日期
|
||||
let mut year = 1970;
|
||||
let mut days_left = days;
|
||||
|
||||
loop {
|
||||
let days_in_year = if is_leap_year(year) { 366 } else { 365 };
|
||||
if days_left < days_in_year {
|
||||
break;
|
||||
}
|
||||
days_left -= days_in_year;
|
||||
year += 1;
|
||||
}
|
||||
|
||||
let month_days = if is_leap_year(year) {
|
||||
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
} else {
|
||||
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
};
|
||||
|
||||
let mut month = 1;
|
||||
for &days_in_month in &month_days {
|
||||
if days_left < days_in_month {
|
||||
break;
|
||||
}
|
||||
days_left -= days_in_month;
|
||||
month += 1;
|
||||
}
|
||||
let day = days_left + 1;
|
||||
|
||||
format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}", year, month, day, hours, minutes, seconds)
|
||||
}
|
||||
|
||||
fn is_leap_year(year: u64) -> bool {
|
||||
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
|
||||
time::OffsetDateTime::now_utc()
|
||||
.format(&time::macros::format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]"))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn truncate_string(s: &str, max_len: usize) -> String {
|
||||
if s.len() <= max_len {
|
||||
if s.chars().count() <= max_len {
|
||||
s.to_string()
|
||||
} else {
|
||||
format!("{}... (truncated)", &s[..max_len])
|
||||
format!("{}... (truncated)", s.chars().take(max_len).collect::<String>())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user