forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor control plugin (alibaba#11169)
* Refactor control plugin. * For checkstyle.
- Loading branch information
1 parent
a843736
commit 0c44dce
Showing
26 changed files
with
315 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 0 additions & 90 deletions
90
core/src/main/java/com/alibaba/nacos/core/control/http/NacosHttpTpsControlInterceptor.java
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
core/src/main/java/com/alibaba/nacos/core/control/http/NacosHttpTpsControlRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.core.control.http; | ||
|
||
import com.alibaba.nacos.core.code.ControllerMethodsCache; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Nacos http tps control cut point filter registration. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
@Configuration | ||
public class NacosHttpTpsControlRegistration { | ||
|
||
@Bean | ||
public FilterRegistrationBean<NacosHttpTpsFilter> tpsFilterRegistration(NacosHttpTpsFilter tpsFilter) { | ||
FilterRegistrationBean<NacosHttpTpsFilter> registration = new FilterRegistrationBean<>(); | ||
registration.setFilter(tpsFilter); | ||
//nacos naming | ||
registration.addUrlPatterns("/v1/ns/*", "/v2/ns/*"); | ||
//nacos config | ||
registration.addUrlPatterns("/v1/cs/*", "/v2/cs/*"); | ||
registration.setName("tpsFilter"); | ||
registration.setOrder(6); | ||
return registration; | ||
} | ||
|
||
@Bean | ||
public NacosHttpTpsFilter tpsFilter(ControllerMethodsCache methodsCache) { | ||
return new NacosHttpTpsFilter(methodsCache); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
...rc/main/java/com/alibaba/nacos/core/control/http/NacosHttpTpsControlWebMvcConfigurer.java
This file was deleted.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
core/src/main/java/com/alibaba/nacos/core/control/http/NacosHttpTpsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.core.control.http; | ||
|
||
import com.alibaba.nacos.api.remote.RpcScheduledExecutor; | ||
import com.alibaba.nacos.common.utils.StringUtils; | ||
import com.alibaba.nacos.core.code.ControllerMethodsCache; | ||
import com.alibaba.nacos.core.control.TpsControl; | ||
import com.alibaba.nacos.core.control.TpsControlConfig; | ||
import com.alibaba.nacos.plugin.control.ControlManagerCenter; | ||
import com.alibaba.nacos.plugin.control.Loggers; | ||
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest; | ||
import com.alibaba.nacos.plugin.control.tps.response.TpsCheckResponse; | ||
|
||
import javax.servlet.AsyncContext; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Nacos http tps control cut point filter. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class NacosHttpTpsFilter implements Filter { | ||
|
||
private ControllerMethodsCache controllerMethodsCache; | ||
|
||
public NacosHttpTpsFilter(ControllerMethodsCache controllerMethodsCache) { | ||
this.controllerMethodsCache = controllerMethodsCache; | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
Filter.super.init(filterConfig); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; | ||
final HttpServletResponse response = (HttpServletResponse) servletResponse; | ||
|
||
Method method = controllerMethodsCache.getMethod(httpServletRequest); | ||
try { | ||
if (method != null && method.isAnnotationPresent(TpsControl.class) | ||
&& TpsControlConfig.isTpsControlEnabled()) { | ||
TpsControl tpsControl = method.getAnnotation(TpsControl.class); | ||
String pointName = tpsControl.pointName(); | ||
String parserName = StringUtils.isBlank(tpsControl.name()) ? pointName : tpsControl.name(); | ||
HttpTpsCheckRequestParser parser = HttpTpsCheckRequestParserRegistry.getParser(parserName); | ||
TpsCheckRequest httpTpsCheckRequest = null; | ||
if (parser != null) { | ||
httpTpsCheckRequest = parser.parse(httpServletRequest); | ||
} | ||
if (httpTpsCheckRequest == null) { | ||
httpTpsCheckRequest = new TpsCheckRequest(); | ||
} | ||
if (StringUtils.isBlank(httpTpsCheckRequest.getPointName())) { | ||
httpTpsCheckRequest.setPointName(pointName); | ||
} | ||
TpsCheckResponse checkResponse = ControlManagerCenter.getInstance().getTpsControlManager() | ||
.check(httpTpsCheckRequest); | ||
if (!checkResponse.isSuccess()) { | ||
AsyncContext asyncContext = httpServletRequest.startAsync(); | ||
asyncContext.setTimeout(0); | ||
RpcScheduledExecutor.CONTROL_SCHEDULER.schedule( | ||
() -> generate503Response(httpServletRequest, response, checkResponse.getMessage(), | ||
asyncContext), 1000L, TimeUnit.MILLISECONDS); | ||
return; | ||
} | ||
|
||
} | ||
} catch (Throwable throwable) { | ||
Loggers.TPS.warn("Fail to http tps check", throwable); | ||
} | ||
|
||
filterChain.doFilter(httpServletRequest, servletResponse); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
Filter.super.destroy(); | ||
} | ||
|
||
void generate503Response(HttpServletRequest request, HttpServletResponse response, String message, | ||
AsyncContext asyncContext) { | ||
|
||
try { | ||
// Disable cache. | ||
response.setHeader("Pragma", "no-cache"); | ||
response.setDateHeader("Expires", 0); | ||
response.setHeader("Cache-Control", "no-cache,no-store"); | ||
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); | ||
response.getOutputStream().println(message); | ||
asyncContext.complete(); | ||
} catch (Exception ex) { | ||
Loggers.TPS.error("Error to generate tps 503 response", ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.