mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 23:06:36 +08:00
refactor(repo): consolidate openflare-server to root and move subprojects to internal/apps
- Merge all files inside openflare-server to the repository root directory. - Relocate agent, relay, and flared subprojects from internal/ to internal/apps/. - Combine docker-compose files and update build context paths to root. - Update GitHub workflows and Dockerfiles to refer to new directories and package names. - Rewrite Go package imports across all files. - Resolve database renew test race condition and clean up docs.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
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"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
client *httpclient.Client
|
||||
frpcManager *frpc.Manager
|
||||
config *config.Config
|
||||
triggerCh chan struct{}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Trigger() {
|
||||
select {
|
||||
case s.triggerCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
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 进程。
|
||||
err = s.frpcManager.UpdateConfig(ctx, configResp)
|
||||
|
||||
result := "success"
|
||||
message := "apply success"
|
||||
if err != nil {
|
||||
result = "failed"
|
||||
message = err.Error()
|
||||
slog.Error("failed to apply tunnel config", "error", err)
|
||||
} else {
|
||||
slog.Info("tunnel config applied successfully", "version", configResp.Version)
|
||||
}
|
||||
|
||||
// Report apply log
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user