实现节点下线功能,优化控制通道和数据通道的连接处理

This commit is contained in:
2025-05-16 16:59:33 +08:00
parent 8a6a4833d4
commit 22f3c37478
7 changed files with 136 additions and 120 deletions

View File

@@ -43,27 +43,43 @@ func Start() error {
return fmt.Errorf("注册节点失败: %w", err)
}
// 建立控制通道
// 连接到网关
var ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
var errCh = make(chan error)
go func() {
for {
err = ctrl(ctx, id, host)
if err == nil {
errCh <- nil
return
}
select {
case <-ctx.Done():
return
default:
slog.Error("建立控制通道失败", "err", err)
slog.Info(fmt.Sprintf("%d 秒后重试", core.RetryInterval))
}
select {
case <-ctx.Done():
return
case <-time.After(time.Duration(core.RetryInterval) * time.Second):
}
slog.Error("建立控制通道失败", "err", err)
slog.Info(fmt.Sprintf("%d 秒后重试", core.RetryInterval))
time.Sleep(time.Duration(core.RetryInterval) * time.Second)
}
}()
// 等待退出
select {
case err := <-errCh:
if err != nil {
slog.Error("控制通道发生错误", "err", err)
}
}
// 下线节点
slog.Debug("下线节点...")
err = report.Offline()