Skip to content

Commit

Permalink
Handle clientTimeout attribute in GenericContext. (sofastack#350)
Browse files Browse the repository at this point in the history
* Handle clientTimeout attribute in GenericContext.
  • Loading branch information
caojie09 authored and leizhiyuan committed Dec 3, 2018
1 parent a45290e commit 6a46e85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws So
String type = getSerializeFactoryType(request.getMethodName(), request.getMethodArgs());
request.addRequestProp(RemotingConstants.HEAD_GENERIC_TYPE, type);

// 修正超时时间
Long clientTimeout = getClientTimeoutFromGenericContext(request.getMethodName(),
request.getMethodArgs());
if (clientTimeout != null) {
request.setTimeout(clientTimeout.intValue());
}

// 修正请求对象
Object[] genericArgs = request.getMethodArgs();
String methodName = (String) genericArgs[0];
Expand Down Expand Up @@ -109,4 +116,15 @@ private String getSerializeFactoryType(String method, Object[] args) throws Sofa
}
throw new SofaRpcException(RpcErrorType.CLIENT_FILTER, "Unsupported method of generic service");
}

private Long getClientTimeoutFromGenericContext(String method, Object[] args) throws SofaRpcException {
if (METHOD_GENERIC_INVOKE.equals(method)) {
if (args.length == 4 && args[3] instanceof GenericContext) {
return ((GenericContext) args[3]).getClientTimeout();
} else if (args.length == 5 && args[4] instanceof GenericContext) {
return ((GenericContext) args[4]).getClientTimeout();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ private void testTimeout(final GenericService proxy, GenericObject genericObject
Assert.assertFalse(isSuccess);

// 3. 指定超时,结果序列化为People 类
People peopleResult = proxy.$genericInvoke("hello",
People peopleResult = proxy.$genericInvoke("helloTimeout",
new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" },
new Object[] { genericObject }, People.class, genericContext);
assertEquals(peopleResult, people);

// 4. 指定超时,结果序列化为GenericObject
GenericObject result = (GenericObject) proxy.$genericInvoke("hello",
GenericObject result = (GenericObject) proxy.$genericInvoke("helloTimeout",
new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" },
new Object[] { genericObject }, genericContext);
isCorrect(result);
Expand Down

0 comments on commit 6a46e85

Please sign in to comment.