From 62beabfd1466fd3a6b6c0736acc8c5a3b3ec4b3b Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期日, 28 四月 2024 10:10:35 +0800 Subject: [PATCH] 完善1400转发流程 --- client/client.go | 68 +++++++++++++++++++++++++++++++++- 1 files changed, 66 insertions(+), 2 deletions(-) diff --git a/client/client.go b/client/client.go index d1004d8..f415a31 100644 --- a/client/client.go +++ b/client/client.go @@ -1,15 +1,79 @@ package client import ( + "context" + "fmt" + "math/rand" + "time" + "gat1400Exchange/config" "gat1400Exchange/pkg/logger" + "gat1400Exchange/vo" ) -func Init1400Client() { +func Init1400Client(ctx context.Context) { if !config.ClientConf.Enable { logger.Debug("GAT/1400 Client disabled") return } - register() + go registerLoop(ctx) + go keepaliveLoop(ctx) + go syncTimeLoop(ctx) + +} + +func registerLoop(ctx context.Context) { + ticker := time.NewTicker(1 * time.Second) + for { + select { + case <-ctx.Done(): + unRegister() + return + case <-ticker.C: + if clientStatus != vo.StatusSuccess { + fmt.Println("register") + if !register() { + randNum := rand.Intn(300) + 1 + // 闅忔満绛夊緟300s, 閲嶆柊娉ㄥ唽 + ticker.Reset(time.Duration(randNum) * time.Second) + } else { + ticker.Reset(1 * time.Second) + } + } + } + } +} + +func keepaliveLoop(ctx context.Context) { + var failCount int + ticker := time.NewTicker(time.Duration(config.ClientConf.HeartbeatInterval) * time.Second) + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + if status := keepalive(); status != vo.StatusSuccess { + failCount++ + + if failCount > config.ClientConf.HeartbeatFailCount { + clientStatus = status + } + } else { + failCount = 0 + } + } + } +} + +func syncTimeLoop(ctx context.Context) { + ticker := time.NewTicker(time.Duration(config.ClientConf.HeartbeatInterval*10) * time.Second) + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + syncTime() + } + } } -- Gitblit v1.8.0