62 Commits

Author SHA1 Message Date
xfy
f33117b940 fix(handler,http2,loadbalance,logging,resolver,ssl): fix high severity issues
- handler/static.go: add sync.RWMutex to StaticHandler; protect Handle
  with RLock and all setters with Lock to prevent data races
- http2/server.go: delete empty connection slice keys from pool map to
  prevent memory leak under high client churn
- loadbalance/slow_start.go: recreate stopCh in Start() to support
  Start-Stop-Start cycles; guard double-close in Stop()
- resolver/resolver.go: recreate stopCh in Start() to support restart
- logging/logging.go: save *os.File handles from getOutput so Close()
  actually closes log files; exclude os.Stdout/os.Stderr from closing
- ssl/session_tickets.go: protect started/rotateTimer access in
  scheduleRotation with mu; support Start-Stop-Start cycles
- ssl/ssl.go: cache parsed default certificate to avoid re-parsing on
  every TLS handshake for OCSP stapling
2026-06-11 17:03:17 +08:00
xfy
f12ffd180f chore: release v0.4.0
- Update CHANGELOG.md for v0.4.0
- Update Makefile FALLBACK_VERSION to 0.4.0
- Fix lint warnings (godoc comments, goconst)
- Clean up code formatting
2026-06-09 15:59:36 +08:00
xfy
503daf65d3 perf(loadbalance): add benchmarks for Least Time and Sticky
- Benchmark Select and Record operations
- Concurrent benchmark for realistic load testing
- Baseline performance:
  - LeastTime.Select: ~33ns/op, 0 allocs
  - LeastTime.Record: ~5.6ns/op, 0 allocs
  - StickySession.Select: ~205ns/op (with cookie lookup)
2026-06-08 18:21:03 +08:00
xfy
ef871f1d39 test(loadbalance): add integration tests for Least Time and Sticky
- Verify Least Time picks faster target consistently
- Verify Sticky fallback when target becomes unhealthy
- Test cookie encoding and session persistence
2026-06-08 18:19:20 +08:00
xfy
cb1f86298e fix: add missing test coverage for Task 4 config integration
- Add validation tests for least_time and sticky configs
- Add algorithm tests for least_time and sticky
- Add SameSite validation in validateProxy
2026-06-08 18:01:21 +08:00
xfy
88a2c1fc1b feat(config): add Least Time and Sticky configuration support
- Add least_time and sticky to valid algorithms list
- Add LeastTimeConfig and StickyConfig structures
- Update default config generation with new options
- Add configuration validation for new fields
2026-06-08 17:57:06 +08:00
xfy
a73da4e14a fix(sticky): recreate stopCh on Start to support restart 2026-06-08 17:52:20 +08:00
xfy
0a5443f6cf fix(sticky): guard against double Stop, nil fallback, and multiple Start calls
- Add sync.Once to prevent double close of stopCh in Stop()
- Add nil fallback guard in NewStickySession (defaults to RoundRobin)
- Add atomic.Bool to make Start() idempotent
- Add tests for double Stop() and nil fallback scenarios
2026-06-08 17:47:37 +08:00
xfy
360fd0da9d fix(sticky): check cookie expiration in Select method
- Fix Select to check if cookie is expired before routing
- Add TestStickySession_ExpiredCookie test
- Expired cookies now trigger fallback + new cookie set
2026-06-08 17:40:54 +08:00
xfy
66752a47f0 fix(sticky): fix cookie format, shard keying, and tests
- Encode cookie as base64(target_url + | + timestamp) per spec
- Use cookie value (not targetURL) for shard key and session map keys
- Add missing sticky.Start() calls in tests
- Fix time precision in cookie encode/decode tests
2026-06-08 17:36:41 +08:00
xfy
f69a11ea05 feat(loadbalance): implement Session Sticky balancer
- Add 256-shard lock map for concurrent session routing
- Cookie-based session persistence with base64 encoding
- TTL expiration with background cleanup goroutine
- Support Secure, HttpOnly, SameSite cookie attributes
- Fallback to configured balancer when session target unavailable
2026-06-08 17:30:06 +08:00
xfy
fa95b2a76e feat(loadbalance): implement Least Time balancer
- Add atomic EWMA Stats field to Target
- Implement LeastTime balancer with header_time and last_byte metrics
- Support Select and SelectExcluding with zero-lock design
- Add ResponseTimeRecorder interface for proxy integration
2026-06-08 17:21:20 +08:00
xfy
c6bb75cffe feat(loadbalance): add atomic EWMA statistics core
- Zero-lock atomic EWMA implementation using fixed-point arithmetic
- Supports header_time and last_byte_time tracking
- Concurrent-safe with CAS retry loop
2026-06-08 17:13:08 +08:00
xfy
71bfb895e5 perf(loadbalance): delegate fnvHash64a to internal/hash
Eliminates code duplication, uses shared inline FNV-1a implementation.
2026-06-04 10:45:11 +08:00
xfy
8e00e63972 chore: fix lint issues from performance optimization
- errcheck: check type assertions from sync.Pool.Get() in
  loadbalance/balancer.go and matcher/radix.go
- errcheck: check type assertion from list.Element.Value in
  resolver/resolver.go evictLRULocked
- revive: add doc comment for exported ReleaseMatchResult function
2026-06-04 00:22:45 +08:00
xfy
7fe1ca6bec perf(loadbalance): eliminate double-lock in ConsistentHash with atomic.Bool rebuild guard
SelectByKey and SelectExcludingByKey previously had a RLock→RUnlock→
rebuildCircle(Lock)→RLock pattern when the hash ring was empty. Under
cold-start concurrency, multiple goroutines could trigger simultaneous
rebuild attempts.

Add atomic.Bool 'rebuilt' flag with ensureRebuilt() check before any
RLock acquisition:
- Fast path: atomic load returns true → skip rebuild, proceed to RLock
- Cold start: first caller rebuilds and sets flag, subsequent callers
  see the flag and skip rebuild
- Rebuild() explicitly resets the flag for explicit ring invalidation

Eliminates the RLock→Unlock→Lock→RLock transition entirely. The ring
is guaranteed ready before RLock is acquired.
2026-06-04 00:20:43 +08:00
xfy
c6e7091089 perf(loadbalance): eliminate per-request allocations in filterHealthy with sync.Pool
filterHealthy() allocated 2 slices (available + backups) per call.
filterHealthyAndExclude() allocated 3 (adds excludeSet map).
IPHash allocated fnv.New64a() object per call.
All triggered on every request's LB selection.

Changes:
- Add filterContext struct holding reusable buffers, managed by sync.Pool
- Replace filterHealthy → filterInto (writes into pooled buffers)
- Replace filterHealthyAndExclude → filterIntoExcluding (pooled buffers)
- Add inline fnvHash64a() to avoid fnv.New64a() heap allocation
- Update all 6 balancer algorithms (RoundRobin, WeightedRoundRobin,
  LeastConnections, IPHash, Random, ConsistentHash) to use pooled
  filterContext via acquire/release pattern
- ConsistentHash.SelectExcludingByKey also uses pool for targetSet
- Remove buildExcludeSet (merged into filterIntoExcluding)

Result: allocs/op reduced from 2-3 to 0-1 on all LB Select paths.
2026-06-04 00:19:04 +08:00
xfy
d03c180f62 perf(loadbalance): replace failMu mutex with atomic operations in Target
Remove sync.Mutex from Target's failure tracking hot path:
- failCount int64 → atomic.Int64
- failedUntil int64 → atomic.Int64
- IsAvailable(): Load() instead of Lock/Unlock, eliminating mutex
  from every per-target check in every LB Select() call
- RecordFailure(): Add(1) + Store() instead of Lock/Unlock
- RecordSuccess(): Store(0) instead of Lock/Unlock

This removes the only mutex acquisition in the load balancing
selection path. Every Select() call iterates targets and calls
IsAvailable() on each — previously acquiring failMu per target
when MaxFails > 0. Now fully lock-free.
2026-06-04 00:12:29 +08:00
xfy
6612819f3a chore: remove stale AGENTS.md files, rewrite root AGENTS.md 2026-06-03 23:47:29 +08:00
xfy
6f17bbad7e chore: remove trailing blank lines and clean up whitespace 2026-06-03 18:08:34 +08:00
xfy
1a6b5f9166 Merge origin/master into master 2026-06-03 16:36:23 +08:00
xfy
2734b04d8f refactor: remove 16.8k lines of dead code across all internal packages
- Delete unused files: tempfile subsystem, matcher variants, server/internal
- Remove 200+ unused functions across proxy, ssl, lua, http2/3, stream, variable
- Fix proxy test type errors (backgroundRefresh ctx→Request)
- Move bench/tools mock backend into internal/testutil
- Remove corresponding test functions for all deleted code
2026-06-03 16:15:43 +08:00
xfy911
0b5b0eb747 docs(loadbalance): add package comments for loadbalance module
- Add package documentation for random load balancing file
- Include author attribution (xfy)
2026-06-03 15:28:53 +08:00
xfy
ba0b3c55bb fix(loadbalance): prevent double-close panic in SlowStartManager.Stop()
Use atomic Swap instead of Load+close to prevent concurrent Stop()
calls from both passing the check and closing the channel twice.
2026-06-03 01:07:22 +08:00
xfy
b6018ff9c9 test: add t.Parallel() to enable parallel test execution
Add t.Parallel() to 110 test functions across 3 test files:
- internal/loadbalance/balancer_test.go (42 tests)
- internal/config/validate_test.go (21 tests)
- internal/server/status_test.go (47 tests)

This reduces total test time from ~3 minutes to ~34 seconds (5.4x faster).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 17:57:23 +08:00
xfy
25d93c25fa refactor: remove unused code and fix formatting
- Remove unused benchmark/tools package
- Make ValidAlgorithms private (validAlgorithms) in loadbalance
- Remove dead code (_ = result) in lua/api_socket_tcp.go
- Fix code formatting with goimports

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 16:58:45 +08:00
xfy
f145a8770e refactor: modernize code with Go 1.22+ features
Apply modern Go patterns across the codebase:
- Replace `interface{}` with `any` (Go 1.18+)
- Use `for range n` instead of `for i := 0; i < n; i++` (Go 1.22+)
- Replace `sort.Slice` with `slices.Sort` from slices package
- Simplify sync.WaitGroup patterns with errgroup where appropriate
- Add Makefile targets for modernize analyzer

Total: 84 files updated, net reduction of 79 lines

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 10:37:45 +08:00
xfy
e7306a0c72 perf: optimize ConsistentHash and RateLimiter for better concurrency
- ConsistentHash: reuse main hash ring in SelectExcludingByKey instead of
  rebuilding per call, reducing memory allocation from 369KB to 1.8KB (99.5%)
- RateLimiter: replace single RWMutex with 16-segment sharded locks to
  reduce lock contention in high-concurrency scenarios
- TLS SessionTickets: add warning log when KeyFile is empty to alert
  users about session invalidation after restart

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 10:23:19 +08:00
xfy
91e04222b3 refactor: 统一 IP 白名单解析和 excludeSet 构建
Batch 1 重构:
- 新增 utils.ParseIPAllowList 统一 IP/CIDR 解析(含 localhost 特殊处理)
- pprof.go/status.go/purge.go 改用统一函数,减少 ~66 行重复代码
- 新增 loadbalance.buildExcludeSet 统一排除集合构建
- 更新 pprof_test.go 适配统一字段 allowed []net.IPNet

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 16:18:52 +08:00
xfy
8f79fb6797 test(config,handler,loadbalance,proxy): 扩展单元测试覆盖率
添加以下测试:
- validate_test.go: Rewrite、NextUpstream、DefaultServer、Mode、
  ListenConflicts、HTTP2、RedirectRewrite 验证测试
- sendfile_test.go: 无效文件描述符、零长度传输、部分传输、
  带偏移量传输测试
- balancer_test.go: ConsistentHash Select/SelectExcluding、
  RandomBalancer 边界条件和 Power of Two Choices 测试
- health_test.go: MarkHealthy/MarkUnhealthy 与 SlowStartManager
  集成测试

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 18:28:28 +08:00
xfy
26a7a58265 feat(loadbalance): 实现慢启动功能
- 添加 SlowStartManager 统一管理目标慢启动状态
- Target 新增 EffectiveWeight 和 SlowStart 字段
- 实现 GetEffectiveWeight 方法支持动态权重
- 权重从 1 线性增加到配置权重,防止恢复服务器被压垮

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 13:15:09 +08:00
xfy
b6e9772191 refactor(loadbalance): 用互斥锁替代原子操作保护 Target 失败状态
原子 CAS 操作在 IsAvailable/RecordFailure/RecordSuccess 之间存在协调问题,
改用 sync.Mutex 保护 failCount 和 failedUntil 的联合更新,简化逻辑并保证一致性。
同时预分配 backups 切片容量避免多次内存分配。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:40:50 +08:00
xfy
ce2d11197a test(loadbalance,proxy): 添加上游参数和 random 算法测试
新增 IsAvailable/RecordFailure/RecordSuccess/filterHealthy 备份优先级、
Random 负载均衡算法测试,适配 createHostClient 签名变更。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:28:59 +08:00
xfy
c0b7e30bf0 feat(config,loadbalance): 添加上游服务器参数和 random 负载均衡算法
Target 新增 MaxConns/MaxFails/FailTimeout/Backup/Down/ProxyURI 字段,
实现 IsAvailable/RecordFailure/RecordSuccess 软失败机制,
filterHealthy 支持备份服务器优先级选择,
新增 random(Power of Two Choices)负载均衡算法。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 11:28:02 +08:00
xfy
3d80b12f7d docs: 为其余模块添加标准化 godoc 注释
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 11:23:04 +08:00
xfy
2458ac1ed1 docs: 为其余模块添加标准化 godoc 注释
为剩余模块添加完整文档注释:
- app: 应用生命周期管理
- cache: 文件缓存
- config: 配置加载器
- handler: 静态文件处理和错误页面
- http2/http3: HTTP/2 和 HTTP/3 适配器
- loadbalance: 负载均衡算法和均衡器
- middleware: bodylimit、compression、rewrite、security
- mimeutil: MIME 类型检测
- netutil: URL 处理工具
- resolver: DNS 解析器
- server: 服务器升级处理
- ssl: SSL/TLS 和 OCSP
- stream: 流处理
- testutil: 测试工具
- variable: 变量池和 SSL 变量

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-20 10:59:53 +08:00
xfy
8f3f1527bc test(loadbalance): 添加负载均衡器完整测试覆盖
- 测试 RoundRobin/WRR/LeastConn/IPHash 算法
- 测试后端选择和权重分布
- 测试边界条件和并发安全

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 18:12:28 +08:00
xfy
8ed800271d test: 迁移基准测试循环到 Go 1.24 b.Loop() API
- 所有 *_bench_test.go 文件从 for i := 0; i < b.N; i++ 改为 for b.Loop()
- 部分测试文件从 for i := 0; i < N; ... 改为 for range N 或 for i := range N
- 涵盖模块: cache, handler, http2, http3, loadbalance, logging, lua,
  middleware/accesslog, middleware/bodylimit, middleware/rewrite,
  middleware/security, netutil, resolver, server, ssl, stream

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 13:50:15 +08:00
xfy
73ef7f4916 fix(lint): 修复剩余 lint 错误
- 统一八进制权限格式为 Go 1.13+ 风格 (0o644/0o755)
- 调整 Target 结构体字段顺序优化内存对齐
- 合并相邻的全局变量声明
- 删除多余空行
- 更新 Makefile 使用 gofumpt 替代 goimports

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:50:14 +08:00
xfy
0c8a15cfee fix(lint): 调整 Target 结构体字段顺序优化内存对齐 2026-04-13 16:41:20 +08:00
xfy
bdf615c585 fix(loadbalance): 添加 hostnameOnce 确保并发安全
Target 结构体添加 sync.Once 字段,确保 hostname 初始化的并发安全

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:21:21 +08:00
xfy
d21e27fbac fix(lint): 修复 golangci-lint 错误 (119 -> 0 issues)
主要修复:
- errcheck: defer Close 使用 //nolint:errcheck,类型断言改为 ok 检查
- govet fieldalignment: 调整结构体字段顺序优化内存布局
- revive unused-parameter: 将未使用参数改为 _
- exhaustive: 添加缺失的 switch case 或 default
- goconst: 提取重复字符串为常量 (accessAllow, accessDeny 等)
- staticcheck SA9003: 修复空分支逻辑
- gofmt: 运行 gofmt -w 格式化
- nolintlint: 修复 nolint 注释格式

其他改进:
- 更新 .golangci.yml 配置,启用更严格的检查
- 移除未使用的代码和导入
- 简化测试辅助函数调用

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:15:31 +08:00
xfy
f31e8afeff feat(lua): 添加 balancer_by_lua 动态负载均衡功能
- 新增 BalancerByLuaConfig 配置,支持 Lua 脚本控制后端选择
- 实现 api_balancer.go Lua API,暴露 set_current_peer 等函数
- Proxy 集成 Lua 引擎,fallback 到标准算法确保可靠性
- 添加负载均衡算法常量提取,消除魔法字符串
- 支持超时控制和备用算法配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:14:59 +08:00
xfy
95b6119e34 refactor: 使用标准库 slices/maps 替代自定义函数
- 使用 slices.Contains 替代 contains/containsInt 函数
- 使用 maps.Copy 替代手动遍历复制
- 删除 internal/cache 中不再需要的辅助函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 13:15:13 +08:00
xfy
8b382606df Merge branch 'lint-fix' - resolve sendfile.go conflict
Conflict: sendfile.go (!linux build tag) was incorrectly modified to
include linuxSendfile and getSocketFd functions which already exist
in sendfile_linux.go.

Resolution: Keep HEAD version (simple fallback returning ENOTSUP) as
Linux implementation is properly separated in sendfile_linux.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 09:26:48 +08:00
xfy
4c12703334 style(loadbalance): 使用自增运算符替代显式加法
totalWeight += 1 → totalWeight++

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 09:40:47 +08:00
xfy
4de32812f2 perf(loadbalance): 预计算一致性哈希虚拟节点哈希值
- Target 结构新增 VirtualHashes 字段存储预计算哈希
- 新增 PrecomputeHashes 方法在初始化时计算虚拟节点哈希
- SelectExcludingByKey 使用预计算哈希避免运行时重复计算
- 新增 SelectExcludingByKey 测试和基准测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 15:03:10 +08:00
xfy
5a6f4d351f feat(resolver): 新增 DNS 解析器支持
新增 internal/resolver 包,提供动态 DNS 解析功能:
- Resolver 类型支持配置多个 DNS 服务器
- 内置缓存支持 TTL 和最大条目数限制
- 统计信息追踪查询次数和缓存命中率

配置层面:
- ResolverConfig 支持 addresses/valid/timeout/ipv4/ipv6/cache_size
- 添加配置验证逻辑

Target 增强:
- 新增 hostname/resolvedIPs 字段支持 IP 缓存
- NeedsResolve 方法判断是否需要重新解析
- NewTargetFromConfig 工厂函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 11:36:49 +08:00
xfy
f6245c19e0 feat(proxy,loadbalance,config): 新增故障转移 (next_upstream) 支持
- 配置新增 NextUpstreamConfig 支持 tries 和 http_codes 参数
- 负载均衡器新增 SelectExcluding 方法用于故障转移排除选择
- 代理请求失败时自动尝试下一个健康后端
- 健康检查新增 MarkHealthy 方法用于故障转移成功后恢复状态

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 17:49:10 +08:00
xfy
0979b60ff2 test(cache,loadbalance,proxy,benchmark): 新增核心模块基准测试
- cache: LRU Get/Set 性能测试
- loadbalance: 负载均衡算法性能测试
- proxy: 代理处理性能测试
- benchmark/tools: 负载生成器和模拟后端工具

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 17:06:08 +08:00