diff --git a/service/oomkiller/service.go b/service/oomkiller/service.go index bbf032d0f..01e078b8a 100644 --- a/service/oomkiller/service.go +++ b/service/oomkiller/service.go @@ -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 } diff --git a/service/oomkiller/service_darwin.go b/service/oomkiller/service_darwin.go index f1d228369..64504ac46 100644 --- a/service/oomkiller/service_darwin.go +++ b/service/oomkiller/service_darwin.go @@ -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