mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 14:26:36 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package sync periodically fetches and applies the active tunnel configuration.
|
|
package sync
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/apps/flared/config"
|
|
"github.com/Rain-kl/Wavelet/internal/apps/flared/frpc"
|
|
"github.com/Rain-kl/Wavelet/internal/apps/flared/httpclient"
|
|
service "github.com/Rain-kl/Wavelet/pkg/protocol"
|
|
)
|
|
|
|
// Service synchronizes tunnel configuration from the control plane to the local frpc manager.
|
|
type Service struct {
|
|
client *httpclient.Client
|
|
frpcManager *frpc.Manager
|
|
config *config.Config
|
|
triggerCh chan struct{}
|
|
}
|
|
|
|
// New creates a sync service with the given client, frpc manager, and config.
|
|
func New(client *httpclient.Client, manager *frpc.Manager, cfg *config.Config) *Service {
|
|
return &Service{
|
|
client: client,
|
|
frpcManager: manager,
|
|
config: cfg,
|
|
triggerCh: make(chan struct{}, 1),
|
|
}
|
|
}
|
|
|
|
// Trigger requests an immediate configuration sync without waiting for the next interval.
|
|
func (s *Service) Trigger() {
|
|
select {
|
|
case s.triggerCh <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// Run starts the sync loop until ctx is canceled.
|
|
func (s *Service) Run(ctx context.Context) {
|
|
ticker := time.NewTicker(s.config.SyncInterval.Duration())
|
|
defer ticker.Stop()
|
|
|
|
// initial sync
|
|
s.doSync(ctx)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
s.doSync(ctx)
|
|
case <-s.triggerCh:
|
|
s.doSync(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) doSync(ctx context.Context) {
|
|
slog.Debug("fetching active tunnel config")
|
|
configResp, err := s.client.GetActiveConfig(ctx)
|
|
if err != nil {
|
|
slog.Error("failed to fetch active tunnel config", "error", err)
|
|
return
|
|
}
|
|
|
|
// 不在 sync 层做版本早退,由 frpcManager.UpdateConfig 负责判断。
|
|
// 原因:重启后进程全部消失,即使版本/checksum 未变,仍需重新拉起 frpc 进程。
|
|
configChanged, err := s.frpcManager.UpdateConfig(ctx, configResp)
|
|
if err != nil {
|
|
slog.Error("failed to apply tunnel config", "error", err)
|
|
s.reportApplyLog(ctx, configResp, "failed", err.Error())
|
|
return
|
|
}
|
|
if configChanged {
|
|
slog.Info("tunnel config applied successfully", "version", configResp.Version)
|
|
s.reportApplyLog(ctx, configResp, "success", "apply success")
|
|
return
|
|
}
|
|
slog.Debug("tunnel config unchanged, skipping apply log report", "version", configResp.Version)
|
|
}
|
|
|
|
func (s *Service) reportApplyLog(ctx context.Context, configResp *service.FlaredTunnelConfigResponse, result string, message string) {
|
|
if configResp == nil {
|
|
return
|
|
}
|
|
logPayload := service.ApplyLogPayload{
|
|
Version: configResp.Version,
|
|
Result: result,
|
|
Message: message,
|
|
Checksum: configResp.Checksum,
|
|
}
|
|
if reportErr := s.client.ReportApplyLog(ctx, logPayload); reportErr != nil {
|
|
slog.Error("failed to report apply log", "error", reportErr)
|
|
}
|
|
}
|