瀏覽代碼

webmvc-adapter: improve to avoid ErrorEntryFreeException (#1533)

If entry already exists in request just skip creation.
master
cdfive GitHub 4 年之前
父節點
當前提交
a1ce97677c
沒有發現已知的金鑰在資料庫的簽署中 GPG 金鑰 ID: 4AEE18F83AFDEB23
共有 1 個檔案被更改,包括 12 行新增4 行删除
  1. +12
    -4
      sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java

+ 12
- 4
sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java 查看文件

@@ -61,9 +61,7 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor
String origin = parseOrigin(request);
String contextName = getContextName(request);
ContextUtil.enter(contextName, origin);
Entry entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.IN);

setEntryInRequest(request, baseWebMvcConfig.getRequestAttributeName(), entry);
setEntryInRequest(request, baseWebMvcConfig.getRequestAttributeName(), resourceName);
}
return true;
} catch (BlockException e) {
@@ -110,12 +108,22 @@ public abstract class AbstractSentinelInterceptor implements HandlerInterceptor
ModelAndView modelAndView) throws Exception {
}

protected void setEntryInRequest(HttpServletRequest request, String name, Entry entry) {
/**
* Note:
* If the attribute key already exists in request, don't create new {@link Entry},
* to guarantee the order of {@link Entry} in pair and avoid {@link com.alibaba.csp.sentinel.ErrorEntryFreeException}.
*
* Refer to:
* https://github.com/alibaba/Sentinel/issues/1531
* https://github.com/alibaba/Sentinel/issues/1482
*/
protected void setEntryInRequest(HttpServletRequest request, String name, String resourceName) throws BlockException {
Object attrVal = request.getAttribute(name);
if (attrVal != null) {
RecordLog.warn("[{}] The attribute key '{}' already exists in request, please set `requestAttributeName`",
getClass().getSimpleName(), name);
} else {
Entry entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.IN);
request.setAttribute(name, entry);
}
}


Loading…
取消
儲存