mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-26 05:01:53 +08:00
性能优化: 热路径零分配, 自适应池CAS无锁化, 锁外计算
- port_scan: fmt.Sprintf→JoinHostPort+fmtPort零分配地址格式化 (2.5x) - port_scan: strings.ToLower→containsFold零分配大小写不敏感匹配 (2.5x) - port_scan: slidingWindowSchedule修复semaphore泄漏bug - service_probe: readFromConn预分配4KB缓冲区消除扩容 - adaptive_pool: maybeAdjust用atomic CAS代替持锁检查, 99%免锁 - adaptive_timeout: Timeout锁外计算均值/标准差, 只锁缓存更新 - 新增perf_bench_test.go基准测试验证所有优化
This commit is contained in:
@@ -23,7 +23,7 @@ type AdaptivePool struct {
|
||||
|
||||
// 监控参数
|
||||
checkInterval time.Duration
|
||||
lastCheck time.Time
|
||||
lastCheckNano int64 // 原子, UnixNano
|
||||
lastExhaustedCount int64
|
||||
lastPacketCount int64
|
||||
|
||||
@@ -67,20 +67,22 @@ func (ap *AdaptivePool) Invoke(task interface{}) error {
|
||||
}
|
||||
|
||||
// maybeAdjust 检查并可能调整线程池大小
|
||||
// 使用原子 CAS 进行时间检查,99%+ 的调用零锁开销
|
||||
func (ap *AdaptivePool) maybeAdjust() {
|
||||
now := time.Now()
|
||||
|
||||
ap.mu.Lock()
|
||||
if now.Sub(ap.lastCheck) < ap.checkInterval {
|
||||
ap.mu.Unlock()
|
||||
lastCheck := atomic.LoadInt64(&ap.lastCheckNano)
|
||||
now := time.Now().UnixNano()
|
||||
if now-lastCheck < int64(ap.checkInterval) {
|
||||
return
|
||||
}
|
||||
ap.lastCheck = now
|
||||
if !atomic.CompareAndSwapInt64(&ap.lastCheckNano, lastCheck, now) {
|
||||
return // 其他 goroutine 已在检查
|
||||
}
|
||||
|
||||
// 获取当前计数
|
||||
currentExhausted := ap.state.GetResourceExhaustedCount()
|
||||
currentPackets := ap.state.GetPacketCount()
|
||||
|
||||
ap.mu.Lock()
|
||||
// 计算增量(本周期内的耗尽率)
|
||||
deltaExhausted := currentExhausted - ap.lastExhaustedCount
|
||||
deltaPackets := currentPackets - ap.lastPacketCount
|
||||
|
||||
Reference in New Issue
Block a user