mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 12:40:11 +00:00
test: Singbox内核定期释放TCP会话列表内存
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# Build go
|
# Build go
|
||||||
FROM golang:1.24.0-alpine AS builder
|
FROM golang:1.24.1-alpine AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
ENV CGO_ENABLED=0
|
ENV CGO_ENABLED=0
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/InazumaV/V2bX/common/format"
|
"github.com/InazumaV/V2bX/common/format"
|
||||||
"github.com/InazumaV/V2bX/common/rate"
|
"github.com/InazumaV/V2bX/common/rate"
|
||||||
|
"github.com/InazumaV/V2bX/common/task"
|
||||||
|
|
||||||
"github.com/InazumaV/V2bX/limiter"
|
"github.com/InazumaV/V2bX/limiter"
|
||||||
|
|
||||||
@@ -19,9 +21,15 @@ import (
|
|||||||
|
|
||||||
var _ adapter.ConnectionTracker = (*HookServer)(nil)
|
var _ adapter.ConnectionTracker = (*HookServer)(nil)
|
||||||
|
|
||||||
|
type ConnEntry struct {
|
||||||
|
Conn net.Conn
|
||||||
|
Timestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type HookServer struct {
|
type HookServer struct {
|
||||||
counter sync.Map //map[string]*counter.TrafficCounter
|
counter sync.Map //map[string]*counter.TrafficCounter
|
||||||
userconn sync.Map //map[string][]net.Conn
|
userconn sync.Map //map[string][]*ConnEntry
|
||||||
|
Cleanup *task.Task
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HookServer) ModeList() []string {
|
func (h *HookServer) ModeList() []string {
|
||||||
@@ -33,6 +41,10 @@ func NewHookServer() *HookServer {
|
|||||||
counter: sync.Map{},
|
counter: sync.Map{},
|
||||||
userconn: sync.Map{},
|
userconn: sync.Map{},
|
||||||
}
|
}
|
||||||
|
server.Cleanup = &task.Task{
|
||||||
|
Interval: 5 * time.Minute,
|
||||||
|
Execute: server.CleanupOldConnections,
|
||||||
|
}
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,14 +94,19 @@ func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapte
|
|||||||
}
|
}
|
||||||
|
|
||||||
conn = counter.NewConnCounter(conn, t.GetCounter(m.User))
|
conn = counter.NewConnCounter(conn, t.GetCounter(m.User))
|
||||||
|
entry := &ConnEntry{
|
||||||
|
Conn: conn,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
if conns, exist := h.userconn.Load(taguuid); exist {
|
if conns, exist := h.userconn.Load(taguuid); exist {
|
||||||
if connList, ok := conns.([]net.Conn); ok {
|
if connList, ok := conns.([]*ConnEntry); ok {
|
||||||
h.userconn.Store(taguuid, append(connList, conn))
|
h.userconn.Store(taguuid, append(connList, entry))
|
||||||
} else {
|
} else {
|
||||||
h.userconn.Store(taguuid, []net.Conn{conn})
|
h.userconn.Delete(taguuid)
|
||||||
|
h.userconn.Store(taguuid, []*ConnEntry{entry})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
h.userconn.Store(taguuid, []net.Conn{conn})
|
h.userconn.Store(taguuid, []*ConnEntry{entry})
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn
|
return conn
|
||||||
@@ -150,14 +167,14 @@ func (h *HookServer) CloseConnections(tag string, uuids []string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
connList, ok := v.([]net.Conn)
|
connList, ok := v.([]*ConnEntry)
|
||||||
if !ok {
|
if !ok {
|
||||||
h.userconn.Delete(taguuid)
|
h.userconn.Delete(taguuid)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, conn := range connList {
|
for _, entry := range connList {
|
||||||
err := conn.Close()
|
err := entry.Conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("close conn error: ", err)
|
log.Error("close conn error: ", err)
|
||||||
}
|
}
|
||||||
@@ -166,3 +183,29 @@ func (h *HookServer) CloseConnections(tag string, uuids []string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *HookServer) CleanupOldConnections() error {
|
||||||
|
expiredTime := time.Now().Add(-time.Minute * 30)
|
||||||
|
h.userconn.Range(func(key, value interface{}) bool {
|
||||||
|
connList, ok := value.([]*ConnEntry)
|
||||||
|
if !ok {
|
||||||
|
h.userconn.Delete(key)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var activeConns []*ConnEntry
|
||||||
|
for _, entry := range connList {
|
||||||
|
if entry.Timestamp.After(expiredTime) {
|
||||||
|
activeConns = append(activeConns, entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(activeConns) == 0 {
|
||||||
|
h.userconn.Delete(key)
|
||||||
|
} else {
|
||||||
|
h.userconn.Store(key, activeConns)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Sing) Start() error {
|
func (b *Sing) Start() error {
|
||||||
|
b.hookServer.Cleanup.Start(false)
|
||||||
return b.box.Start()
|
return b.box.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user