fix: 优化配置

This commit is contained in:
tbphp
2025-06-06 22:09:20 +08:00
parent 1faa0b1b73
commit 3a9ea5b33e
5 changed files with 215 additions and 17 deletions

View File

@@ -74,6 +74,14 @@ type PerformanceConfig struct {
BufferSize int `json:"bufferSize"`
}
// LogConfig 日志配置
type LogConfig struct {
Level string `json:"level"` // debug, info, warn, error
Format string `json:"format"` // text, json
EnableFile bool `json:"enableFile"` // 是否启用文件日志
FilePath string `json:"filePath"` // 日志文件路径
}
// Config 应用配置
type Config struct {
Server ServerConfig `json:"server"`
@@ -82,6 +90,7 @@ type Config struct {
Auth AuthConfig `json:"auth"`
CORS CORSConfig `json:"cors"`
Performance PerformanceConfig `json:"performance"`
Log LogConfig `json:"log"`
}
// Global config instance
@@ -123,6 +132,12 @@ func LoadConfig() (*Config, error) {
DisableCompression: parseBoolean(os.Getenv("DISABLE_COMPRESSION"), true),
BufferSize: parseInteger(os.Getenv("BUFFER_SIZE"), 32*1024),
},
Log: LogConfig{
Level: getEnvOrDefault("LOG_LEVEL", "info"),
Format: getEnvOrDefault("LOG_FORMAT", "text"),
EnableFile: parseBoolean(os.Getenv("LOG_ENABLE_FILE"), false),
FilePath: getEnvOrDefault("LOG_FILE_PATH", "logs/app.log"),
},
}
// 验证配置
@@ -205,6 +220,19 @@ func DisplayConfig(config *Config) {
}
logrus.Infof(" CORS: %s", corsStatus)
logrus.Infof(" 连接池: %d/%d", config.Performance.MaxSockets, config.Performance.MaxFreeSockets)
keepAliveStatus := "已启用"
if !config.Performance.EnableKeepAlive {
keepAliveStatus = "已禁用"
}
logrus.Infof(" Keep-Alive: %s", keepAliveStatus)
compressionStatus := "已启用"
if config.Performance.DisableCompression {
compressionStatus = "已禁用"
}
logrus.Infof(" 压缩: %s", compressionStatus)
logrus.Infof(" 缓冲区大小: %d bytes", config.Performance.BufferSize)
}
// 辅助函数

View File

@@ -51,10 +51,15 @@ func NewProxyServer() (*ProxyServer, error) {
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCompression: true, // 禁用压缩以减少CPU开销让上游处理
DisableCompression: config.AppConfig.Performance.DisableCompression,
ForceAttemptHTTP2: true,
WriteBufferSize: 32 * 1024, // 32KB写缓冲
ReadBufferSize: 32 * 1024, // 32KB读缓冲
WriteBufferSize: config.AppConfig.Performance.BufferSize,
ReadBufferSize: config.AppConfig.Performance.BufferSize,
}
// 配置 Keep-Alive
if !config.AppConfig.Performance.EnableKeepAlive {
transport.DisableKeepAlives = true
}
httpClient := &http.Client{