From 670f2ec94fffc60e4435b63aee6d3ad74df59be0 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Wed, 26 Mar 2025 20:45:53 +0400 Subject: [PATCH] prepend rules when no cleanup --- app/router/command/command.go | 2 +- app/router/restriction.go | 4 ++-- app/router/router.go | 10 +++++++--- features/routing/router.go | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/router/command/command.go b/app/router/command/command.go index fd9caa22..55e9f8f7 100644 --- a/app/router/command/command.go +++ b/app/router/command/command.go @@ -55,7 +55,7 @@ func (s *routingServer) OverrideBalancerTarget(ctx context.Context, request *Ove func (s *routingServer) AddRule(ctx context.Context, request *AddRuleRequest) (*AddRuleResponse, error) { if bo, ok := s.router.(routing.Router); ok { - return &AddRuleResponse{}, bo.AddRule(request.Config, request.ShouldAppend) + return &AddRuleResponse{}, bo.AddRule(request.Config, request.ShouldAppend, false) } return nil, errors.New("unsupported router implementation") diff --git a/app/router/restriction.go b/app/router/restriction.go index 88cf382c..93dfed1f 100644 --- a/app/router/restriction.go +++ b/app/router/restriction.go @@ -39,7 +39,7 @@ func (r *Router) RestrictionRule(restriction *route.Restriction, ip net.IP) erro rule.RoutingRule.SourceGeoip = append(rule.RoutingRule.SourceGeoip, sourceIP) r.RemoveRule(restriction.Tag) - return r.ReloadRules(&Config{Rule: []*RoutingRule{rule.RoutingRule}}, true) + return r.ReloadRules(&Config{Rule: []*RoutingRule{rule.RoutingRule}}, true, restriction.CleanInterval == 0) } } } @@ -52,7 +52,7 @@ func (r *Router) RestrictionRule(restriction *route.Restriction, ip net.IP) erro } errors.LogWarning(r.ctx, "restrict IP -> ", ip.String(), " for route violation.") - return r.ReloadRules(&Config{Rule: []*RoutingRule{newRule}}, true) + return r.ReloadRules(&Config{Rule: []*RoutingRule{newRule}}, true, restriction.CleanInterval == 0) } func shouldCleanup(restriction *route.Restriction) bool { diff --git a/app/router/router.go b/app/router/router.go index 7e79c15e..270bbc2d 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -101,12 +101,12 @@ func (r *Router) AddRule(config *serial.TypedMessage, shouldAppend bool) error { return err } if c, ok := inst.(*Config); ok { - return r.ReloadRules(c, shouldAppend) + return r.ReloadRules(c, shouldAppend, false) } return errors.New("AddRule: config type error") } -func (r *Router) ReloadRules(config *Config, shouldAppend bool) error { +func (r *Router) ReloadRules(config *Config, shouldAppend bool, prependRules bool) error { r.mu.Lock() defer r.mu.Unlock() @@ -147,7 +147,11 @@ func (r *Router) ReloadRules(config *Config, shouldAppend bool) error { } rr.Balancer = brule } - r.rules = append(r.rules, rr) + if prependRules { + r.rules = append([]*Rule{rr}, r.rules...) + } else { + r.rules = append(r.rules, rr) + } } return nil diff --git a/features/routing/router.go b/features/routing/router.go index 53761434..b955a133 100644 --- a/features/routing/router.go +++ b/features/routing/router.go @@ -16,7 +16,7 @@ type Router interface { // PickRoute returns a route decision based on the given routing context. PickRoute(ctx Context) (Route, error) - AddRule(config *serial.TypedMessage, shouldAppend bool) error + AddRule(config *serial.TypedMessage, shouldAppend bool, prependRules bool) error RemoveRule(tag string) error RestrictionRule(restriction *route.Restriction, ip net.IP) error } @@ -62,7 +62,7 @@ func (DefaultRouter) PickRoute(ctx Context) (Route, error) { } // AddRule implements Router. -func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool) error { +func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool, prependRules bool) error { return common.ErrNoClue }