mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
platform: Add oom killer
This commit is contained in:
parent
2cb0e37f50
commit
cc9cb0b477
|
@ -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 {
|
||||||
|
|
38
common/dialer/conntrack/killer.go
Normal file
38
common/dialer/conntrack/killer.go
Normal 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
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue