Skip to content

Commit

Permalink
Merge pull requests #187
Browse files Browse the repository at this point in the history
Routing by port
  • Loading branch information
chenzhiguo authored Dec 31, 2024
2 parents a5475f3 + 3330e78 commit 7a37415
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@ default boolean isAccessible() {
return state == null || state.isAccessible();
}

/**
* Checks if the specified port matches the current port.
*
* @param port The port to check against the current port.
* @return {@code true} if the specified port matches the current port, {@code false} otherwise.
*/
default boolean isPort(int port) {
return getPort() == port;
}

/**
* Evaluates the predicate associated with this endpoint, if any, to determine
* if this endpoint satisfies the conditions defined by the predicate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public interface RouteFilter {

int ORDER_LOCALHOST = ORDER_STICKY + 100;

int ORDER_HEALTH = ORDER_LOCALHOST + 100;
int ORDER_PORT = ORDER_LOCALHOST + 100;

int ORDER_HEALTH = ORDER_PORT + 100;

int ORDER_VIRTUAL = ORDER_HEALTH + 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.governance.invoke.filter.route;

import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.annotation.ConditionalOnFlowControlEnabled;
import com.jd.live.agent.governance.invoke.OutboundInvocation;
import com.jd.live.agent.governance.invoke.filter.RouteFilter;
import com.jd.live.agent.governance.invoke.filter.RouteFilterChain;
import com.jd.live.agent.governance.request.Portable;
import com.jd.live.agent.governance.request.ServiceRequest.OutboundRequest;

/**
* A class that implements the {@link RouteFilter} interface.
* It filters outbound requests based on the port number.
*
* @since 1.6.0
*/
@Extension(value = "PortFilter", order = RouteFilter.ORDER_PORT)
@ConditionalOnFlowControlEnabled
public class PortFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
T request = invocation.getRequest();
if (request instanceof Portable) {
Integer port = ((Portable) request).getPort();
if (port != null) {
invocation.getRouteTarget().filter(e -> e.isPort(port));
}
}
chain.filter(invocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* inbound and outbound requests.
* </p>
*/
public interface HttpRequest extends ServiceRequest {
public interface HttpRequest extends ServiceRequest, Portable {

/**
* Returns the URI of the request.
Expand All @@ -45,13 +45,6 @@ public interface HttpRequest extends ServiceRequest {
*/
String getSchema();

/**
* Returns the port number of the request.
*
* @return The port number.
*/
Integer getPort();

/**
* Returns the host name of the request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.governance.request;

/**
* An interface representing an object that has a port.
* Implementing classes should provide a method to get the port number.
*/
public interface Portable {

/**
* Returns the port number of the request.
*
* @return The port number.
*/
Integer getPort();
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ com.jd.live.agent.governance.invoke.filter.route.StickyFilter
com.jd.live.agent.governance.invoke.filter.route.VirtualFilter
com.jd.live.agent.governance.invoke.filter.route.CircuitBreakerFilter
com.jd.live.agent.governance.invoke.filter.route.GroupFilter
com.jd.live.agent.governance.invoke.filter.route.PortFilter

0 comments on commit 7a37415

Please sign in to comment.