mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 03:15:40 +08: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
|
|
}
|