mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-07-11 10:03:32 +00:00
Fix oom draft flood
This commit is contained in:
parent
236430100c
commit
ebe74a5648
2 changed files with 13 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue