package client
|
|
import (
|
"context"
|
"fmt"
|
"math/rand"
|
"time"
|
|
"gat1400Exchange/config"
|
"gat1400Exchange/pkg/logger"
|
"gat1400Exchange/vo"
|
)
|
|
func Init1400Client(ctx context.Context) {
|
if !config.ClientConf.Enable {
|
logger.Debug("GAT/1400 Client disabled")
|
return
|
}
|
|
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()
|
}
|
}
|
}
|