mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-28 05:46:36 +08:00
[优化] 修复重启 frpc 挂掉问题
This commit is contained in:
@@ -45,7 +45,7 @@ func NewManager(frpsPath string, dataDir string, agentToken string) *Manager {
|
||||
frpsPath: frpsPath,
|
||||
dataDir: dataDir,
|
||||
configPath: filepath.Join(dataDir, "frps.toml"),
|
||||
status: "unhealthy",
|
||||
status: "unknown", // 启动阶段尚未获取配置,状态未知;避免首次 heartbeat 误报 frps_unhealthy
|
||||
agentToken: agentToken,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,11 +85,13 @@ func (m *Manager) UpdateConfig(ctx context.Context, newConfig *service.FlaredTun
|
||||
if newConfig == nil {
|
||||
return nil
|
||||
}
|
||||
if newConfig.Version == m.currentVersion && newConfig.Checksum == m.currentChecksum {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Info("applying new tunnel config", "version", newConfig.Version)
|
||||
versionChanged := newConfig.Version != m.currentVersion || newConfig.Checksum != m.currentChecksum
|
||||
if versionChanged {
|
||||
slog.Info("applying new tunnel config", "version", newConfig.Version)
|
||||
} else {
|
||||
slog.Debug("tunnel config version unchanged, ensuring processes are running", "version", newConfig.Version)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(m.cfg.DataDir, 0o755); err != nil {
|
||||
return fmt.Errorf("create data dir failed: %w", err)
|
||||
@@ -102,10 +104,11 @@ func (m *Manager) UpdateConfig(ctx context.Context, newConfig *service.FlaredTun
|
||||
tomlContent := buildFrpcToml(relay, newConfig.Proxies)
|
||||
configPath := filepath.Join(m.cfg.DataDir, fmt.Sprintf("frpc_%s.toml", relay.RelayNodeID))
|
||||
|
||||
needsRestart := true
|
||||
needsRestart := false
|
||||
existingData, err := os.ReadFile(configPath)
|
||||
if err == nil && string(existingData) == tomlContent {
|
||||
needsRestart = false
|
||||
if err != nil || string(existingData) != tomlContent {
|
||||
// 配置文件不存在或内容有变化,需要写入并重启
|
||||
needsRestart = true
|
||||
}
|
||||
|
||||
if needsRestart {
|
||||
@@ -115,6 +118,8 @@ func (m *Manager) UpdateConfig(ctx context.Context, newConfig *service.FlaredTun
|
||||
}
|
||||
m.restartProcess(ctx, relay.RelayNodeID, configPath)
|
||||
} else if _, ok := m.processes[relay.RelayNodeID]; !ok {
|
||||
// 配置未变但进程不存在(如重启后),直接启动进程
|
||||
slog.Info("frpc process missing, starting", "relay_id", relay.RelayNodeID)
|
||||
m.restartProcess(ctx, relay.RelayNodeID, configPath)
|
||||
}
|
||||
}
|
||||
@@ -128,10 +133,12 @@ func (m *Manager) UpdateConfig(ctx context.Context, newConfig *service.FlaredTun
|
||||
}
|
||||
}
|
||||
|
||||
m.currentVersion = newConfig.Version
|
||||
m.currentChecksum = newConfig.Checksum
|
||||
|
||||
return m.saveState()
|
||||
if versionChanged {
|
||||
m.currentVersion = newConfig.Version
|
||||
m.currentChecksum = newConfig.Checksum
|
||||
return m.saveState()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) restartProcess(ctx context.Context, relayID string, configPath string) {
|
||||
|
||||
@@ -61,12 +61,8 @@ func (s *Service) doSync(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if s.frpcManager.GetCurrentConfigVersion() == configResp.Version &&
|
||||
s.frpcManager.GetCurrentConfigChecksum() == configResp.Checksum {
|
||||
slog.Debug("tunnel config is up to date", "version", configResp.Version)
|
||||
return
|
||||
}
|
||||
|
||||
// 不在 sync 层做版本早退,由 frpcManager.UpdateConfig 负责判断。
|
||||
// 原因:重启后进程全部消失,即使版本/checksum 未变,仍需重新拉起 frpc 进程。
|
||||
err = s.frpcManager.UpdateConfig(ctx, configResp)
|
||||
|
||||
result := "success"
|
||||
|
||||
Reference in New Issue
Block a user