Skip to content

Commit

Permalink
*fix an issue where inputstream is sometimes lost.
Browse files Browse the repository at this point in the history
Signed-off-by: provenceee <[email protected]>
  • Loading branch information
provenceee committed Nov 14, 2024
1 parent 40caf06 commit 5ad463d
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 528 deletions.
6 changes: 0 additions & 6 deletions sermant-plugins/sermant-router/spring-router-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package com.huaweicloud.sermant.router.spring.handler;

import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.router.spring.service.SpringConfigService;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* AbstractHandlerMapping处理器
Expand All @@ -29,27 +27,47 @@
* @since 2023-02-21
*/
public abstract class AbstractMappingHandler extends AbstractHandler {
/**
* 配置服务
*/
protected final SpringConfigService configService;

/**
* 构造方法
*/
public AbstractMappingHandler() {
configService = PluginServiceManager.getPluginService(SpringConfigService.class);
}

/**
* 获取透传的标记
*
* @param path 请求路径
* @param methodName http方法
* @param headers http请求头
* @param parameters url参数
* @param keys 透传标记key
* @return 透传的标记
*/
public abstract Map<String, List<String>> getRequestTag(String path, String methodName,
Map<String, List<String>> headers, Map<String, List<String>> parameters);
Map<String, List<String>> headers, Map<String, List<String>> parameters, Keys keys);

/**
* 透传标记key实体
*
* @author provenceee
* @since 2023-02-21
*/
public static class Keys {
private final Set<String> matchKeys;

private final Set<String> injectTags;

/**
* 构造方法
*
* @param matchKeys 标签路由透传标记
* @param injectTags 泳道透传标记
*/
public Keys(Set<String> matchKeys, Set<String> injectTags) {
this.matchKeys = matchKeys;
this.injectTags = injectTags;
}

public Set<String> getMatchKeys() {
return matchKeys;
}

public Set<String> getInjectTags() {
return injectTags;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public LaneMappingHandler() {
*/
@Override
public Map<String, List<String>> getRequestTag(String path, String methodName, Map<String, List<String>> headers,
Map<String, List<String>> parameters) {
Set<String> injectTags = configService.getInjectTags();
Map<String, List<String>> parameters, Keys keys) {
Set<String> injectTags = keys.getInjectTags();
if (CollectionUtils.isEmpty(injectTags)) {
// 染色标记为空,代表没有染色规则,直接return
LOGGER.fine("Lane tags are empty.");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class RouteMappingHandler extends AbstractMappingHandler {
*/
@Override
public Map<String, List<String>> getRequestTag(String path, String methodName, Map<String, List<String>> headers,
Map<String, List<String>> parameters) {
Set<String> matchKeys = configService.getMatchKeys();
Map<String, List<String>> parameters, Keys keys) {
Set<String> matchKeys = keys.getMatchKeys();
return getRequestTag(headers, matchKeys);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.router.common.handler.Handler;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler;
import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler.Keys;
import com.huaweicloud.sermant.router.spring.handler.LaneMappingHandler;
import com.huaweicloud.sermant.router.spring.handler.RouteMappingHandler;
import com.huaweicloud.sermant.router.spring.service.SpringConfigService;
import com.huaweicloud.sermant.router.spring.utils.SpringRouterUtils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
Expand All @@ -32,6 +37,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* webflux获取header拦截点
Expand All @@ -43,12 +49,15 @@ public class AbstractHandlerMappingInterceptor extends AbstractInterceptor {
private static final String EXCEPT_CLASS_NAME
= "org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping";

private final SpringConfigService configService;

private final List<AbstractMappingHandler> handlers;

/**
* 构造方法
*/
public AbstractHandlerMappingInterceptor() {
configService = PluginServiceManager.getPluginService(SpringConfigService.class);
handlers = new ArrayList<>();
handlers.add(new LaneMappingHandler());
handlers.add(new RouteMappingHandler());
Expand All @@ -59,14 +68,21 @@ public AbstractHandlerMappingInterceptor() {
public ExecuteContext before(ExecuteContext context) {
if (shouldHandle(context)) {
ThreadLocalUtils.removeRequestTag();
Set<String> matchKeys = configService.getMatchKeys();
Set<String> injectTags = configService.getInjectTags();
if (CollectionUtils.isEmpty(matchKeys) && CollectionUtils.isEmpty(injectTags)) {
// 染色标记为空,代表没有染色规则,直接return
return context;
}
ServerWebExchange exchange = (ServerWebExchange) context.getArguments()[0];
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
String path = request.getURI().getPath();
String methodName = request.getMethod().name();
Map<String, List<String>> queryParams = request.getQueryParams();
handlers.forEach(handler -> ThreadLocalUtils
.addRequestTag(handler.getRequestTag(path, methodName, headers, queryParams)));
String query = request.getURI().getQuery();
Map<String, List<String>> queryParams = SpringRouterUtils.getParametersByQuery(query);
handlers.forEach(handler -> ThreadLocalUtils.addRequestTag(
handler.getRequestTag(path, methodName, headers, queryParams, new Keys(matchKeys, injectTags))));
}
return context;
}
Expand Down
Loading

0 comments on commit 5ad463d

Please sign in to comment.