Skip to content

Commit 53e5814

Browse files
authored
API - Add | Remove Routing Rules (#3189)
* add RuleTag to routing rules * add router pb commands * add and remove routing rules api * cleanup * fix * improve error msg * add append flag apply balancer config
1 parent 8a439bf commit 53e5814

13 files changed

Lines changed: 901 additions & 269 deletions

File tree

app/router/command/command.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ func (s *routingServer) OverrideBalancerTarget(ctx context.Context, request *Ove
5454
return nil, newError("unsupported router implementation")
5555
}
5656

57+
func (s *routingServer) AddRule(ctx context.Context, request *AddRuleRequest) (*AddRuleResponse, error) {
58+
if bo, ok := s.router.(routing.Router); ok {
59+
return &AddRuleResponse{}, bo.AddRule(request.Config, request.ShouldAppend)
60+
}
61+
return nil, newError("unsupported router implementation")
62+
63+
}
64+
func (s *routingServer) RemoveRule(ctx context.Context, request *RemoveRuleRequest) (*RemoveRuleResponse, error) {
65+
if bo, ok := s.router.(routing.Router); ok {
66+
return &RemoveRuleResponse{}, bo.RemoveRule(request.RuleTag)
67+
}
68+
return nil, newError("unsupported router implementation")
69+
}
70+
5771
// NewRoutingServer creates a statistics service with statistics manager.
5872
func NewRoutingServer(router routing.Router, routingStats stats.Channel) RoutingServiceServer {
5973
return &routingServer{

app/router/command/command.pb.go

Lines changed: 405 additions & 141 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/router/command/command.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ option java_package = "com.xray.app.router.command";
77
option java_multiple_files = true;
88

99
import "common/net/network.proto";
10+
import "common/serial/typed_message.proto";
1011

1112
// RoutingContext is the context with information relative to routing process.
1213
// It conforms to the structure of xray.features.routing.Context and
@@ -88,13 +89,28 @@ message OverrideBalancerTargetRequest {
8889

8990
message OverrideBalancerTargetResponse {}
9091

92+
message AddRuleRequest {
93+
xray.common.serial.TypedMessage config = 1;
94+
bool shouldAppend = 2;
95+
}
96+
message AddRuleResponse {}
97+
98+
message RemoveRuleRequest {
99+
string ruleTag = 1;
100+
}
101+
102+
message RemoveRuleResponse {}
103+
91104
service RoutingService {
92105
rpc SubscribeRoutingStats(SubscribeRoutingStatsRequest)
93106
returns (stream RoutingContext) {}
94107
rpc TestRoute(TestRouteRequest) returns (RoutingContext) {}
95108

96109
rpc GetBalancerInfo(GetBalancerInfoRequest) returns (GetBalancerInfoResponse){}
97110
rpc OverrideBalancerTarget(OverrideBalancerTargetRequest) returns (OverrideBalancerTargetResponse) {}
111+
112+
rpc AddRule(AddRuleRequest) returns (AddRuleResponse) {}
113+
rpc RemoveRule(RemoveRuleRequest) returns (RemoveRuleResponse) {}
98114
}
99115

100116
message Config {}

app/router/command/command_grpc.pb.go

Lines changed: 81 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/router/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
type Rule struct {
1313
Tag string
14+
RuleTag string
1415
Balancer *Balancer
1516
Condition Condition
1617
}
@@ -150,7 +151,7 @@ func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatch
150151
leastLoadStrategy := NewLeastLoadStrategy(s)
151152
return &Balancer{
152153
selectors: br.OutboundSelector,
153-
ohm: ohm,
154+
ohm: ohm,
154155
fallbackTag: br.FallbackTag,
155156
strategy: leastLoadStrategy,
156157
}, nil
@@ -159,7 +160,7 @@ func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatch
159160
case "":
160161
return &Balancer{
161162
selectors: br.OutboundSelector,
162-
ohm: ohm,
163+
ohm: ohm,
163164
fallbackTag: br.FallbackTag,
164165
strategy: &RandomStrategy{},
165166
}, nil

0 commit comments

Comments
 (0)