feat: 添加重连逻辑和CI workflow
Some checks failed
Build / build (push) Failing after 1m30s

This commit is contained in:
lafay
2026-03-19 10:52:23 +08:00
parent 74438c1f7f
commit d0ff0a28df
2 changed files with 58 additions and 5 deletions

40
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,40 @@
name: Build
on:
push:
branches: [main, master]
create:
tag:
- 'v*'
pull_request:
branches: [main, master]
env:
GO_VERSION: '1.25'
APP_NAME: schedule_converter
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: |
cd schedule_converter
go mod download
- name: Build Linux AMD64
run: |
cd schedule_converter
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ${{ env.APP_NAME }}-linux-amd64 .
- name: Build Windows AMD64
run: |
cd schedule_converter
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o ${{ env.APP_NAME }}-windows-amd64.exe .
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ env.APP_NAME }}
path: |
schedule_converter/${{ env.APP_NAME }}-linux-amd64
schedule_converter/${{ env.APP_NAME }}-windows-amd64.exe

23
main.go
View File

@@ -68,25 +68,38 @@ func runGRPCClient(serverAddr string) {
}()
reconnectInterval := 5 * time.Second
const maxReconnectInterval = 60 * time.Second
retryCount := 0
for {
select {
case <-ctx.Done():
slog.Info("客户端已停止")
slog.Info("客户端已停止", "total_retries", retryCount)
return
default:
}
retryCount++
currentInterval := reconnectInterval
if retryCount > 1 {
currentInterval = min(reconnectInterval*time.Duration(retryCount-1), maxReconnectInterval)
}
slog.Info("正在连接服务器", "attempt", retryCount, "server", serverAddr)
c := grpcClient.NewClient(serverAddr, runnerID, version)
handler := grpcClient.NewTaskHandler()
c.SetHandler(handler)
if err := c.Connect(ctx); err != nil {
slog.Warn("连接失败,稍后重试", "error", err, "retry_after", reconnectInterval)
time.Sleep(reconnectInterval)
slog.Warn("连接失败", "error", err, "attempt", retryCount, "retry_after", currentInterval)
time.Sleep(currentInterval)
continue
}
slog.Info("连接成功", "attempt", retryCount)
retryCount = 0
heartbeatCtx, heartbeatCancel := context.WithCancel(ctx)
go c.HeartbeatLoop(heartbeatCtx, 30*time.Second)
@@ -99,9 +112,9 @@ func runGRPCClient(serverAddr string) {
}
if err != nil {
slog.Warn("连接断开,稍后重连", "error", err, "retry_after", reconnectInterval)
slog.Warn("连接断开", "error", err, "retry_after", reconnectInterval)
} else {
slog.Info("连接已关闭,稍后重连", "retry_after", reconnectInterval)
slog.Info("连接已关闭")
}
time.Sleep(reconnectInterval)
}