mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-12 04:06:37 +08:00
fix: make chain and limiter updates idempotent
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -43,8 +43,8 @@ func updateChain(req updateChainRequest) error {
|
|||||||
|
|
||||||
name := strings.TrimSpace(req.Chain)
|
name := strings.TrimSpace(req.Chain)
|
||||||
|
|
||||||
if !registry.ChainRegistry().IsRegistered(name) {
|
if registry.ChainRegistry().IsRegistered(name) {
|
||||||
return errors.New("chain " + name + " not found")
|
registry.ChainRegistry().Unregister(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Data.Name = name
|
req.Data.Name = name
|
||||||
@@ -54,19 +54,22 @@ func updateChain(req updateChainRequest) error {
|
|||||||
return errors.New("create chain " + name + " failed: " + err.Error())
|
return errors.New("create chain " + name + " failed: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
registry.ChainRegistry().Unregister(name)
|
|
||||||
|
|
||||||
if err := registry.ChainRegistry().Register(name, v); err != nil {
|
if err := registry.ChainRegistry().Register(name, v); err != nil {
|
||||||
return errors.New("chain " + name + " already exists")
|
return errors.New("chain " + name + " already exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.OnUpdate(func(c *config.Config) error {
|
config.OnUpdate(func(c *config.Config) error {
|
||||||
|
found := false
|
||||||
for i := range c.Chains {
|
for i := range c.Chains {
|
||||||
if c.Chains[i].Name == name {
|
if c.Chains[i].Name == name {
|
||||||
c.Chains[i] = &req.Data
|
c.Chains[i] = &req.Data
|
||||||
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
c.Chains = append(c.Chains, &req.Data)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -77,10 +80,9 @@ func deleteChain(req deleteChainRequest) error {
|
|||||||
|
|
||||||
name := strings.TrimSpace(req.Chain)
|
name := strings.TrimSpace(req.Chain)
|
||||||
|
|
||||||
if !registry.ChainRegistry().IsRegistered(name) {
|
if registry.ChainRegistry().IsRegistered(name) {
|
||||||
return errors.New("chain " + name + " not found")
|
registry.ChainRegistry().Unregister(name)
|
||||||
}
|
}
|
||||||
registry.ChainRegistry().Unregister(name)
|
|
||||||
|
|
||||||
config.OnUpdate(func(c *config.Config) error {
|
config.OnUpdate(func(c *config.Config) error {
|
||||||
chains := c.Chains
|
chains := c.Chains
|
||||||
|
|||||||
@@ -37,27 +37,30 @@ func updateLimiter(req updateLimiterRequest) error {
|
|||||||
|
|
||||||
name := strings.TrimSpace(req.Limiter)
|
name := strings.TrimSpace(req.Limiter)
|
||||||
|
|
||||||
if !registry.TrafficLimiterRegistry().IsRegistered(name) {
|
if registry.TrafficLimiterRegistry().IsRegistered(name) {
|
||||||
return errors.New("limiter " + name + " not found")
|
registry.TrafficLimiterRegistry().Unregister(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Data.Name = name
|
req.Data.Name = name
|
||||||
|
|
||||||
v := parser.ParseTrafficLimiter(&req.Data)
|
v := parser.ParseTrafficLimiter(&req.Data)
|
||||||
|
|
||||||
registry.TrafficLimiterRegistry().Unregister(name)
|
|
||||||
|
|
||||||
if err := registry.TrafficLimiterRegistry().Register(name, v); err != nil {
|
if err := registry.TrafficLimiterRegistry().Register(name, v); err != nil {
|
||||||
return errors.New("limiter " + name + " already exists")
|
return errors.New("limiter " + name + " already exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.OnUpdate(func(c *config.Config) error {
|
config.OnUpdate(func(c *config.Config) error {
|
||||||
|
found := false
|
||||||
for i := range c.Limiters {
|
for i := range c.Limiters {
|
||||||
if c.Limiters[i].Name == name {
|
if c.Limiters[i].Name == name {
|
||||||
c.Limiters[i] = &req.Data
|
c.Limiters[i] = &req.Data
|
||||||
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
c.Limiters = append(c.Limiters, &req.Data)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -68,10 +71,9 @@ func deleteLimiter(req deleteLimiterRequest) error {
|
|||||||
|
|
||||||
name := strings.TrimSpace(req.Limiter)
|
name := strings.TrimSpace(req.Limiter)
|
||||||
|
|
||||||
if !registry.TrafficLimiterRegistry().IsRegistered(name) {
|
if registry.TrafficLimiterRegistry().IsRegistered(name) {
|
||||||
return errors.New("limiter " + name + " not found")
|
registry.TrafficLimiterRegistry().Unregister(name)
|
||||||
}
|
}
|
||||||
registry.TrafficLimiterRegistry().Unregister(name)
|
|
||||||
|
|
||||||
config.OnUpdate(func(c *config.Config) error {
|
config.OnUpdate(func(c *config.Config) error {
|
||||||
limiteres := c.Limiters
|
limiteres := c.Limiters
|
||||||
|
|||||||
Reference in New Issue
Block a user