Skip to content

Commit

Permalink
webmvc-adapter: improve to avoid ErrorEntryFreeException (alibaba#1533)
Browse files Browse the repository at this point in the history
If entry already exists in request just skip creation.
  • Loading branch information
cdfive authored and mastertiller committed Aug 20, 2020
1 parent 034f50e commit 3868aa3
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
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) {
Expand Down Expand Up @@ -110,12 +108,22 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
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);
}
}
Expand Down

0 comments on commit 3868aa3

Please sign in to comment.