Fix oom draft flood

This commit is contained in:
世界 2026-06-23 15:15:01 +08:00
parent 236430100c
commit ebe74a5648
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 13 additions and 0 deletions

View file

@ -32,6 +32,8 @@ type Service struct {
adaptiveTimer *adaptiveTimer
lastReportTime atomic.Int64
//nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft.
lastDraftTime atomic.Int64
//nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft.
draftCancelled atomic.Bool
}

View file

@ -34,6 +34,7 @@ import "C"
import (
"sync"
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/byteformats"
@ -41,6 +42,8 @@ import (
"github.com/sagernet/sing/service"
)
const oomDraftMinInterval = time.Hour
var (
globalAccess sync.Mutex
globalServices []*Service
@ -109,6 +112,14 @@ func (s *Service) writeOOMDraft(memoryUsage uint64) {
if s.draftCancelled.Load() {
return
}
now := time.Now().UnixNano()
lastDraft := s.lastDraftTime.Load()
if time.Duration(now-lastDraft) < oomDraftMinInterval {
return
}
if !s.lastDraftTime.CompareAndSwap(lastDraft, now) {
return
}
reporter := service.FromContext[OOMReporter](s.ctx)
if reporter == nil {
return