修复服务退出超时问题

This commit is contained in:
2025-03-05 14:50:50 +08:00
parent 0144d1d46f
commit 6e2309df2d
3 changed files with 12 additions and 25 deletions

View File

@@ -1,20 +1,18 @@
## todo ## todo
ProxyConn 直接实现 Conn 相同的接口,不再取出 Conn 使用 压力测试
读取 conn 时加上超时机制 读取 conn 时加上超时机制
代理节点超时控制 代理节点超时控制
在控制通道直接传输目标地址,客户端可以同时开始数据通道和目标地址的连接建立
网关根据代理节点对目标服务连接的反馈,决定向用户返回的 socks 响应 网关根据代理节点对目标服务连接的反馈,决定向用户返回的 socks 响应
数据通道池化 数据通道池化
在控制通道直接传输目标地址,客户端可以同时开始数据通道和目标地址的连接建立 协程池化
检查退出超时的问题
补全测试
### 长期 ### 长期

View File

@@ -27,7 +27,6 @@ type Service struct {
cancel context.CancelFunc cancel context.CancelFunc
userConnMap sync.Map userConnMap sync.Map
ctrlConnMap sync.Map
fwdLesWg utils.CountWaitGroup fwdLesWg utils.CountWaitGroup
ctrlConnWg utils.CountWaitGroup ctrlConnWg utils.CountWaitGroup
@@ -45,8 +44,8 @@ func New(config *Config) *Service {
Config: config, Config: config,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
userConnMap: sync.Map{}, userConnMap: sync.Map{},
ctrlConnMap: sync.Map{},
fwdLesWg: utils.CountWaitGroup{}, fwdLesWg: utils.CountWaitGroup{},
ctrlConnWg: utils.CountWaitGroup{}, ctrlConnWg: utils.CountWaitGroup{},
@@ -101,7 +100,6 @@ func (s *Service) Run() {
} }
wg.Wait() wg.Wait()
// 协程建立有先后顺序,不能乱,否则会泄露
s.dataConnWg.Wait() s.dataConnWg.Wait()
s.ctrlConnWg.Wait() s.ctrlConnWg.Wait()
s.fwdLesWg.Wait() s.fwdLesWg.Wait()
@@ -115,13 +113,6 @@ func (s *Service) Run() {
}) })
s.userConnMap.Clear() s.userConnMap.Clear()
s.ctrlConnMap.Range(func(key, value any) bool {
conn := value.(net.Conn)
utils.Close(conn)
return true
})
s.ctrlConnMap.Clear()
s.ctrlConnWg.Wait() s.ctrlConnWg.Wait()
slog.Debug("控制通道连接已关闭") slog.Debug("控制通道连接已关闭")
s.dataConnWg.Wait() s.dataConnWg.Wait()

View File

@@ -38,17 +38,15 @@ func Start() {
osQuit := make(chan os.Signal) osQuit := make(chan os.Signal)
signal.Notify(osQuit, os.Interrupt, syscall.SIGTERM) signal.Notify(osQuit, os.Interrupt, syscall.SIGTERM)
errQuit := make(chan struct{})
defer close(errQuit)
// 启动服务 // 启动服务
slog.Info("启动服务") slog.Info("启动服务")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
errQuit := make(chan struct{}, 1)
defer close(errQuit)
go func() { go func() {
defer wg.Done() defer wg.Done()
err := startFwdServer(ctx) err := startFwdServer(ctx)
@@ -72,14 +70,14 @@ func Start() {
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
wg.Wait()
select { select {
case <-utils.ChanWgWait(timeout, &wg): case <-utils.ChanWgWait(timeout, &wg):
slog.Info("服务已退出") slog.Info("服务已退出")
case <-timeout.Done(): case <-timeout.Done():
slog.Warn("退出超时,强制退出") slog.Warn("退出超时,强制退出")
} }
time.Sleep(3 * time.Second)
} }
func startFwdServer(ctx context.Context) error { func startFwdServer(ctx context.Context) error {