feat: 十轮网关优化 - 安全加固/可观测性/性能/可靠性
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
CI / security-scan (push) Has been cancelled

- SSE Keepalive Ping (15s心跳防止代理断连)
- Timing HTTP 头 (X-Timing-Queue/Inference/Total-Ms)
- Adapter Request-ID 传播到后端
- Session 清理日志回调
- Server 安全加固 (ReadHeaderTimeout/MaxHeaderBytes 防 slowloris)
- Usage Tracker 数据保留清理 (retentionDays + 定期清理)
- Config Reload 后 Adapter Registry 更新 (RegisterIfAbsent + RWMutex)
- Rate Limiter 空闲 Bucket 清理 (30分钟过期)
- Shutdown Drain 超时可配置 (ShutdownDrainSeconds)
- Config 模型字段校验增强 (provider/endpoint/actual_model)
- Auth 过期 Key 自动清理 (5分钟扫描)
- Admin API Rate Limiting
- Adapter Health Check 独立超时 (每个 adapter 3s)
- TCP 连接阶段超时 (DialContext 5s + KeepAlive 30s)
- 幂等键缓存、审计日志、Gzip 中间件、CORS Expose Headers
- Backpressure 响应头、熔断器 Prometheus 指标
- 连接池优化、Trace-ID 全链路传播
This commit is contained in:
selfrelease
2026-08-03 15:43:11 +08:00
parent e7e98271d4
commit da9c8334d8
27 changed files with 3002 additions and 309 deletions
+56 -9
View File
@@ -7,27 +7,27 @@ import (
// CircuitBreaker implements a sliding-window circuit breaker for adapter health.
type CircuitBreaker struct {
mu sync.Mutex
mu sync.Mutex
errorRateThreshold float64
minRequests int
windowSeconds int
openDuration time.Duration
halfOpenMax int
minRequests int
windowSeconds int
openDuration time.Duration
halfOpenMax int
// sliding window state
requests []time.Time
requests []time.Time
errors []time.Time
// breaker state
state breakerState
openedAt time.Time
state breakerState
openedAt time.Time
halfOpenCount int
}
type breakerState int
const (
breakerClosed breakerState = iota
breakerClosed breakerState = iota
breakerOpen
breakerHalfOpen
)
@@ -126,6 +126,53 @@ func (cb *CircuitBreaker) State() string {
return "unknown"
}
// BreakerStats 熔断器统计信息。
type BreakerStats struct {
State string `json:"state"`
TotalRequests int `json:"total_requests"`
TotalErrors int `json:"total_errors"`
ErrorRate float64 `json:"error_rate"`
WindowSeconds int `json:"window_seconds"`
OpenDuration string `json:"open_duration"`
Threshold float64 `json:"error_rate_threshold"`
MinRequests int `json:"min_requests"`
}
// Stats 返回熔断器的详细统计信息。
func (cb *CircuitBreaker) Stats() BreakerStats {
cb.mu.Lock()
defer cb.mu.Unlock()
now := time.Now()
cb.prune(now)
total := len(cb.requests)
errors := len(cb.errors)
var errorRate float64
if total > 0 {
errorRate = float64(errors) / float64(total)
}
stateName := "closed"
switch cb.state {
case breakerOpen:
stateName = "open"
case breakerHalfOpen:
stateName = "half_open"
}
return BreakerStats{
State: stateName,
TotalRequests: total,
TotalErrors: errors,
ErrorRate: errorRate,
WindowSeconds: cb.windowSeconds,
OpenDuration: cb.openDuration.String(),
Threshold: cb.errorRateThreshold,
MinRequests: cb.minRequests,
}
}
// prune removes entries outside the sliding window.
func (cb *CircuitBreaker) prune(now time.Time) {
cutoff := now.Add(-time.Duration(cb.windowSeconds) * time.Second)