This is an automated email from the ASF dual-hosted git repository.
yui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new c874d65f1 [type: refactor] Optimize how to build rpcType (#3207)
c874d65f1 is described below
commit c874d65f1505005ff6d5156925dc8d980e0c0150
Author: xiaoyu <[email protected]>
AuthorDate: Wed Apr 6 18:38:23 2022 +0800
[type: refactor] Optimize how to build rpcType (#3207)
---
.../plugin/global/DefaultShenyuContextBuilder.java | 29 ++++++++++++----------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
index 3d77d306e..8bd6a603d 100644
---
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
+++
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.plugin.global;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -25,8 +26,6 @@ import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextBuilder;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
import org.apache.shenyu.plugin.global.cache.MetaDataCache;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
@@ -41,8 +40,6 @@ import java.util.Optional;
*/
public class DefaultShenyuContextBuilder implements ShenyuContextBuilder {
- private static final Logger LOG =
LoggerFactory.getLogger(GlobalPlugin.class);
-
private static final String RPC_TYPE = "rpc_type";
private static final String UPGRADE = "Upgrade";
@@ -60,22 +57,28 @@ public class DefaultShenyuContextBuilder implements
ShenyuContextBuilder {
@Override
public ShenyuContext build(final ServerWebExchange exchange) {
+ Pair<String, MetaData> buildData = buildData(exchange);
+ return
decoratorMap.get(buildData.getLeft()).decorator(buildDefaultContext(exchange.getRequest()),
buildData.getRight());
+ }
+
+ private Pair<String, MetaData> buildData(final ServerWebExchange exchange)
{
ServerHttpRequest request = exchange.getRequest();
- String path = request.getURI().getPath();
- MetaData metaData = MetaDataCache.getInstance().obtain(path);
HttpHeaders headers = request.getHeaders();
+ String rpcType = headers.getFirst(RPC_TYPE);
+ if (StringUtils.isNotEmpty(rpcType)) {
+ return Pair.of(rpcType, new MetaData());
+ }
String upgrade = headers.getFirst(UPGRADE);
- String rpcType;
+ if (StringUtils.isNotEmpty(upgrade) &&
RpcTypeEnum.WEB_SOCKET.getName().equals(upgrade)) {
+ return Pair.of(RpcTypeEnum.WEB_SOCKET.getName(), new MetaData());
+ }
+ MetaData metaData =
MetaDataCache.getInstance().obtain(request.getURI().getPath());
if (Objects.nonNull(metaData) &&
Boolean.TRUE.equals(metaData.getEnabled())) {
exchange.getAttributes().put(Constants.META_DATA, metaData);
- rpcType = metaData.getRpcType();
- } else if (StringUtils.isNotEmpty(upgrade) &&
RpcTypeEnum.WEB_SOCKET.getName().equals(upgrade)) {
- rpcType = RpcTypeEnum.WEB_SOCKET.getName();
+ return Pair.of(metaData.getRpcType(), metaData);
} else {
- String rpcTypeParam = request.getHeaders().getFirst(RPC_TYPE);
- rpcType = StringUtils.isEmpty(rpcTypeParam) ?
RpcTypeEnum.HTTP.getName() : rpcTypeParam;
+ return Pair.of(RpcTypeEnum.HTTP.getName(), new MetaData());
}
- return
decoratorMap.get(rpcType).decorator(buildDefaultContext(request), metaData);
}
private ShenyuContext buildDefaultContext(final ServerHttpRequest request)
{