-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Fix timeout filter not work in async way. #3174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import org.apache.dubbo.rpc.Invoker; | ||
| import org.apache.dubbo.rpc.Result; | ||
| import org.apache.dubbo.rpc.RpcException; | ||
| import org.apache.dubbo.rpc.RpcInvocation; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
|
|
@@ -36,21 +37,40 @@ public class TimeoutFilter implements Filter { | |
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(TimeoutFilter.class); | ||
|
|
||
| private static final String TIMEOUT_FILTER_START_TIME = "timeout_filter_start_time"; | ||
|
|
||
| @Override | ||
| public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { | ||
| long start = System.currentTimeMillis(); | ||
| Result result = invoker.invoke(invocation); | ||
| long elapsed = System.currentTimeMillis() - start; | ||
| if (invoker.getUrl() != null | ||
| && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(), | ||
| "timeout", Integer.MAX_VALUE)) { | ||
| if (logger.isWarnEnabled()) { | ||
| logger.warn("invoke time out. method: " + invocation.getMethodName() | ||
| + " arguments: " + Arrays.toString(invocation.getArguments()) + " , url is " | ||
| + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms."); | ||
| if (invocation.getAttachments() != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no attachement for invocation, to handle fix timeout should we care to create a attachment and set TIMEOUT_FILTER_START_TIME ? What do you say?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like you are right...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have fix this place.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @carryxyh Just thinking loud here (please bear with me 😄 ), should we create a FilterWrapper and use it to store framework code related internal properties here, it may give us better way of separating object information. e.g So even in future we need to keep add internal properties we may not be landed up overriding user propvided values.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khanimteyaz That's interesting, but if we add an extra |
||
| long start = System.currentTimeMillis(); | ||
| invocation.getAttachments().put(TIMEOUT_FILTER_START_TIME, String.valueOf(start)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be careful that this attachment
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is indeed a problem. I personally think that my revision is rather stubborn. But haven't found a better way to pass the start time. . . Maybe using a DataStore or sth else?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I think the previous way you give is better, please see my comments below.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have checked the code. |
||
| } else { | ||
| if (invocation instanceof RpcInvocation) { | ||
| RpcInvocation invc = (RpcInvocation) invocation; | ||
| long start = System.currentTimeMillis(); | ||
| invc.setAttachment(TIMEOUT_FILTER_START_TIME, String.valueOf(start)); | ||
| } | ||
| } | ||
| return result; | ||
| return invoker.invoke(invocation); | ||
| } | ||
|
|
||
| @Override | ||
| public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) { | ||
| String startAttach = invocation.getAttachment(TIMEOUT_FILTER_START_TIME); | ||
| if (startAttach != null) { | ||
| long elapsed = System.currentTimeMillis() - Long.valueOf(startAttach); | ||
| if (invoker.getUrl() != null | ||
| && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(), | ||
| "timeout", Integer.MAX_VALUE)) { | ||
| if (logger.isWarnEnabled()) { | ||
| logger.warn("invoke time out. method: " + invocation.getMethodName() | ||
| + " arguments: " + Arrays.toString(invocation.getArguments()) + " , url is " | ||
| + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms."); | ||
| } | ||
| } | ||
| // we should not change the attachments of the invocation. | ||
| invocation.getAttachments().remove(TIMEOUT_FILTER_START_TIME); | ||
|
chickenlj marked this conversation as resolved.
Outdated
|
||
| } | ||
| return result; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.