fix: 修复语法问题

This commit is contained in:
tbphp
2025-06-09 22:27:30 +08:00
parent 9562f0f6dd
commit 6b33f7054d
11 changed files with 124 additions and 39 deletions

View File

@@ -243,7 +243,16 @@ func parseBoolean(value string, defaultValue bool) bool {
if value == "" {
return defaultValue
}
return strings.ToLower(value) == "true"
lowerValue := strings.ToLower(value)
switch lowerValue {
case "true", "1", "yes", "on":
return true
case "false", "0", "no", "off":
return false
default:
return defaultValue
}
}
// parseArray parses array environment variable (comma-separated)

View File

@@ -121,9 +121,9 @@ func IsRetryable(err error) bool {
// Common error instances
var (
ErrNoAPIKeysAvailable = NewAppError(ErrNoKeysAvailable, "No API keys available")
ErrAllAPIKeysBlacklisted = NewAppError(ErrAllKeysBlacklisted, "All API keys are blacklisted")
ErrInvalidConfiguration = NewAppError(ErrConfigInvalid, "Invalid configuration")
ErrNoAPIKeysAvailable = NewAppError(ErrNoKeysAvailable, "No API keys available")
ErrAllAPIKeysBlacklisted = NewAppError(ErrAllKeysBlacklisted, "All API keys are blacklisted")
ErrInvalidConfiguration = NewAppError(ErrConfigInvalid, "Invalid configuration")
ErrAuthenticationRequired = NewAppError(ErrAuthMissing, "Authentication required")
ErrInvalidAuthToken = NewAppError(ErrAuthInvalid, "Invalid authentication token")
ErrInvalidAuthToken = NewAppError(ErrAuthInvalid, "Invalid authentication token")
)

View File

@@ -39,12 +39,20 @@ func (h *Handler) Health(c *gin.Context) {
httpStatus = http.StatusServiceUnavailable
}
// Calculate uptime (this should be tracked from server start time)
uptime := "unknown"
if startTime, exists := c.Get("serverStartTime"); exists {
if st, ok := startTime.(time.Time); ok {
uptime = time.Since(st).String()
}
}
c.JSON(httpStatus, gin.H{
"status": status,
"timestamp": time.Now().UTC().Format(time.RFC3339),
"healthy_keys": stats.HealthyKeys,
"total_keys": stats.TotalKeys,
"uptime": time.Since(time.Now()).String(), // This would need to be tracked properly
"uptime": uptime,
})
}

View File

@@ -306,12 +306,20 @@ func (km *Manager) GetBlacklist() []types.BlacklistEntry {
// setupMemoryCleanup sets up periodic memory cleanup
func (km *Manager) setupMemoryCleanup() {
km.cleanupTicker = time.NewTicker(5 * time.Minute)
// Reduce GC frequency to every 15 minutes to avoid performance impact
km.cleanupTicker = time.NewTicker(15 * time.Minute)
go func() {
for {
select {
case <-km.cleanupTicker.C:
runtime.GC()
// Only trigger GC if memory usage is high
var m runtime.MemStats
runtime.ReadMemStats(&m)
// Trigger GC only if allocated memory is above 100MB
if m.Alloc > 100*1024*1024 {
runtime.GC()
logrus.Debugf("Manual GC triggered, memory usage: %d MB", m.Alloc/1024/1024)
}
case <-km.stopCleanup:
return
}

View File

@@ -62,8 +62,12 @@ func Logger(config types.LogConfig) gin.HandlerFunc {
retryInfo = fmt.Sprintf(" - Retry[%d]", retryCount)
}
// Filter health check logs
if path == "/health" {
// Filter health check and other monitoring endpoint logs to reduce noise
if isMonitoringEndpoint(path) {
// Only log errors for monitoring endpoints
if statusCode >= 400 {
logrus.Warnf("%s %s - %d - %v", method, fullPath, statusCode, latency)
}
return
}
@@ -257,3 +261,14 @@ func ErrorHandler() gin.HandlerFunc {
}
}
}
// isMonitoringEndpoint checks if the path is a monitoring endpoint
func isMonitoringEndpoint(path string) bool {
monitoringPaths := []string{"/health", "/stats", "/blacklist", "/reset-keys"}
for _, monitoringPath := range monitoringPaths {
if path == monitoringPath {
return true
}
}
return false
}

View File

@@ -369,8 +369,8 @@ func (ps *ProxyServer) handleStreamingResponse(c *gin.Context, resp *http.Respon
return
}
// Copy streaming data
buffer := make([]byte, 4096)
// Copy streaming data with optimized buffer size
buffer := make([]byte, 32*1024) // 32KB buffer for better performance
for {
n, err := resp.Body.Read(buffer)
if n > 0 {