mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-14 04:43:17 +00:00
36 lines
595 B
Go
36 lines
595 B
Go
package conntrack
|
|
|
|
import (
|
|
runtimeDebug "runtime/debug"
|
|
"time"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
"github.com/sagernet/sing/common/memory"
|
|
)
|
|
|
|
var (
|
|
KillerEnabled bool
|
|
MemoryLimit uint64
|
|
killerLastCheck time.Time
|
|
)
|
|
|
|
func KillerCheck() error {
|
|
if !KillerEnabled {
|
|
return nil
|
|
}
|
|
nowTime := time.Now()
|
|
if nowTime.Sub(killerLastCheck) < 3*time.Second {
|
|
return nil
|
|
}
|
|
killerLastCheck = nowTime
|
|
if memory.Total() > MemoryLimit {
|
|
Close()
|
|
go func() {
|
|
time.Sleep(time.Second)
|
|
runtimeDebug.FreeOSMemory()
|
|
}()
|
|
return E.New("out of memory")
|
|
}
|
|
return nil
|
|
}
|