platform: Add oom killer

This commit is contained in:
世界 2023-03-16 10:55:06 +08:00
parent 2cb0e37f50
commit cc9cb0b477
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 80 additions and 33 deletions

View file

@ -1,29 +1,32 @@
package conntrack package conntrack
import ( import (
"io"
"net" "net"
"runtime/debug"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
type Conn struct { type Conn struct {
net.Conn net.Conn
element *list.Element[*ConnEntry] element *list.Element[io.Closer]
} }
func NewConn(conn net.Conn) *Conn { func NewConn(conn net.Conn) (*Conn, error) {
entry := &ConnEntry{
Conn: conn,
Stack: debug.Stack(),
}
connAccess.Lock() connAccess.Lock()
element := openConnection.PushBack(entry) element := openConnection.PushBack(conn)
connAccess.Unlock() connAccess.Unlock()
if KillerEnabled {
err := killerCheck()
if err != nil {
conn.Close()
return nil, err
}
}
return &Conn{ return &Conn{
Conn: conn, Conn: conn,
element: element, element: element,
} }, nil
} }
func (c *Conn) Close() error { func (c *Conn) Close() error {

View file

@ -0,0 +1,38 @@
package conntrack
import (
"runtime"
runtimeDebug "runtime/debug"
"time"
E "github.com/sagernet/sing/common/exceptions"
)
var (
KillerEnabled bool
MemoryLimit int64
killerLastCheck time.Time
)
func killerCheck() error {
if !KillerEnabled {
return nil
}
nowTime := time.Now()
if nowTime.Sub(killerLastCheck) < 3*time.Second {
return nil
}
killerLastCheck = nowTime
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
inuseMemory := int64(memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased)
if inuseMemory > MemoryLimit {
Close()
go func() {
time.Sleep(time.Second)
runtimeDebug.FreeOSMemory()
}()
return E.New("out of memory")
}
return nil
}

View file

@ -1,29 +1,32 @@
package conntrack package conntrack
import ( import (
"io"
"net" "net"
"runtime/debug"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
type PacketConn struct { type PacketConn struct {
net.PacketConn net.PacketConn
element *list.Element[*ConnEntry] element *list.Element[io.Closer]
} }
func NewPacketConn(conn net.PacketConn) *PacketConn { func NewPacketConn(conn net.PacketConn) (*PacketConn, error) {
entry := &ConnEntry{
Conn: conn,
Stack: debug.Stack(),
}
connAccess.Lock() connAccess.Lock()
element := openConnection.PushBack(entry) element := openConnection.PushBack(conn)
connAccess.Unlock() connAccess.Unlock()
if KillerEnabled {
err := killerCheck()
if err != nil {
conn.Close()
return nil, err
}
}
return &PacketConn{ return &PacketConn{
PacketConn: conn, PacketConn: conn,
element: element, element: element,
} }, nil
} }
func (c *PacketConn) Close() error { func (c *PacketConn) Close() error {

View file

@ -10,22 +10,17 @@ import (
var ( var (
connAccess sync.RWMutex connAccess sync.RWMutex
openConnection list.List[*ConnEntry] openConnection list.List[io.Closer]
) )
type ConnEntry struct {
Conn io.Closer
Stack []byte
}
func Count() int { func Count() int {
return openConnection.Len() return openConnection.Len()
} }
func List() []*ConnEntry { func List() []io.Closer {
connAccess.RLock() connAccess.RLock()
defer connAccess.RUnlock() defer connAccess.RUnlock()
connList := make([]*ConnEntry, 0, openConnection.Len()) connList := make([]io.Closer, 0, openConnection.Len())
for element := openConnection.Front(); element != nil; element = element.Next() { for element := openConnection.Front(); element != nil; element = element.Next() {
connList = append(connList, element.Value) connList = append(connList, element.Value)
} }
@ -36,8 +31,8 @@ func Close() {
connAccess.Lock() connAccess.Lock()
defer connAccess.Unlock() defer connAccess.Unlock()
for element := openConnection.Front(); element != nil; element = element.Next() { for element := openConnection.Front(); element != nil; element = element.Next() {
common.Close(element.Value.Conn) common.Close(element.Value)
element.Value = nil element.Value = nil
} }
openConnection = list.List[*ConnEntry]{} openConnection.Init()
} }

View file

@ -178,12 +178,12 @@ func trackConn(conn net.Conn, err error) (net.Conn, error) {
if !conntrack.Enabled || err != nil { if !conntrack.Enabled || err != nil {
return conn, err return conn, err
} }
return conntrack.NewConn(conn), nil return conntrack.NewConn(conn)
} }
func trackPacketConn(conn net.PacketConn, err error) (net.PacketConn, error) { func trackPacketConn(conn net.PacketConn, err error) (net.PacketConn, error) {
if !conntrack.Enabled || err != nil { if !conntrack.Enabled || err != nil {
return conn, err return conn, err
} }
return conntrack.NewPacketConn(conn), nil return conntrack.NewPacketConn(conn)
} }

View file

@ -2,9 +2,17 @@
package libbox package libbox
import "runtime/debug" import (
runtimeDebug "runtime/debug"
"github.com/sagernet/sing-box/common/dialer/conntrack"
)
const memoryLimit = 30 * 1024 * 1024
func SetMemoryLimit() { func SetMemoryLimit() {
debug.SetGCPercent(10) runtimeDebug.SetGCPercent(10)
debug.SetMemoryLimit(30 * 1024 * 1024) runtimeDebug.SetMemoryLimit(memoryLimit)
conntrack.KillerEnabled = true
conntrack.MemoryLimit = memoryLimit
} }