From d0ff0a28dfb6e68cfc17660d8b268e62c78f3578 Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Thu, 19 Mar 2026 10:52:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=92=8CCI=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 40 +++++++++++++++++++++++++++++++++++++ main.go | 23 ++++++++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..7960a69 --- /dev/null +++ b/.github/workflows/build.yml @@ -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 \ No newline at end of file diff --git a/main.go b/main.go index 7ab0bc2..49617a0 100644 --- a/main.go +++ b/main.go @@ -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) }