mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-13 12:46:36 +08:00
fix: protect shared rules across delivery and recovery (#560)
This commit is contained in:
@@ -370,43 +370,43 @@ type getConfigResponse struct {
|
||||
|
||||
// getConfigData 获取配置数据(避免循环依赖)
|
||||
func getConfigData() ([]byte, error) {
|
||||
config.OnUpdate(func(c *config.Config) error {
|
||||
for _, svc := range c.Services {
|
||||
if svc == nil {
|
||||
continue
|
||||
// Reporting is read-only: enriching a detached snapshot must not persist
|
||||
// stale state over a config currently being reloaded or acknowledged.
|
||||
cfg := config.Global()
|
||||
for _, svc := range cfg.Services {
|
||||
if svc == nil {
|
||||
continue
|
||||
}
|
||||
s := registry.ServiceRegistry().Get(svc.Name)
|
||||
ss, ok := s.(serviceStatus)
|
||||
if ok && ss != nil {
|
||||
status := ss.Status()
|
||||
svc.Status = &config.ServiceStatus{
|
||||
CreateTime: status.CreateTime().Unix(),
|
||||
State: string(status.State()),
|
||||
}
|
||||
s := registry.ServiceRegistry().Get(svc.Name)
|
||||
ss, ok := s.(serviceStatus)
|
||||
if ok && ss != nil {
|
||||
status := ss.Status()
|
||||
svc.Status = &config.ServiceStatus{
|
||||
CreateTime: status.CreateTime().Unix(),
|
||||
State: string(status.State()),
|
||||
if st := status.Stats(); st != nil {
|
||||
svc.Status.Stats = &config.ServiceStats{
|
||||
TotalConns: st.Get(stats.KindTotalConns),
|
||||
CurrentConns: st.Get(stats.KindCurrentConns),
|
||||
TotalErrs: st.Get(stats.KindTotalErrs),
|
||||
InputBytes: st.Get(stats.KindInputBytes),
|
||||
OutputBytes: st.Get(stats.KindOutputBytes),
|
||||
}
|
||||
if st := status.Stats(); st != nil {
|
||||
svc.Status.Stats = &config.ServiceStats{
|
||||
TotalConns: st.Get(stats.KindTotalConns),
|
||||
CurrentConns: st.Get(stats.KindCurrentConns),
|
||||
TotalErrs: st.Get(stats.KindTotalErrs),
|
||||
InputBytes: st.Get(stats.KindInputBytes),
|
||||
OutputBytes: st.Get(stats.KindOutputBytes),
|
||||
}
|
||||
}
|
||||
for _, ev := range status.Events() {
|
||||
if !ev.Time.IsZero() {
|
||||
svc.Status.Events = append(svc.Status.Events, config.ServiceEvent{
|
||||
Time: ev.Time.Unix(),
|
||||
Msg: ev.Message,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, ev := range status.Events() {
|
||||
if !ev.Time.IsZero() {
|
||||
svc.Status.Events = append(svc.Status.Events, config.ServiceEvent{
|
||||
Time: ev.Time.Unix(),
|
||||
Msg: ev.Message,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var resp getConfigResponse
|
||||
resp.Config = config.Global()
|
||||
resp.Config = cfg
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
resp.Config.Write(buf, "json")
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/go-gost/x/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -148,3 +152,31 @@ func TestPostJSONWithFallbackRemembersDetectedURL(t *testing.T) {
|
||||
t.Fatalf("expected remembered http url first, got %s", calls[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigReportDoesNotPersistStaleRuntime(t *testing.T) {
|
||||
previous, path := config.Global(), config.PersistPath()
|
||||
defer config.Set(previous)
|
||||
defer config.SetPersistPath(path)
|
||||
config.Set(&config.Config{Services: []*config.ServiceConfig{{Name: "old-runtime"}}})
|
||||
filename := filepath.Join(t.TempDir(), "gost.json")
|
||||
candidate := []byte(`{"services":[{"name":"new-on-disk"}]}`)
|
||||
if err := os.WriteFile(filename, candidate, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.SetPersistPath(filename)
|
||||
config.EnablePersist()
|
||||
report, err := getConfigData()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(report, []byte("old-runtime")) {
|
||||
t.Fatalf("unexpected report: %s", report)
|
||||
}
|
||||
saved, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(saved, candidate) {
|
||||
t.Fatalf("report overwrote candidate config: %s", saved)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user