From 6a46e852fc284b84198d53c713165710d6f08838 Mon Sep 17 00:00:00 2001 From: caojie09 Date: Mon, 3 Dec 2018 10:38:59 +0800 Subject: [PATCH] Handle clientTimeout attribute in GenericContext. (#350) * Handle clientTimeout attribute in GenericContext. --- .../sofa/rpc/filter/ConsumerGenericFilter.java | 18 ++++++++++++++++++ .../sofa/rpc/test/generic/GenericTest.java | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/filter/ConsumerGenericFilter.java b/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/filter/ConsumerGenericFilter.java index f434296d3..755ca68c7 100644 --- a/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/filter/ConsumerGenericFilter.java +++ b/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/filter/ConsumerGenericFilter.java @@ -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]; @@ -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; + } } diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/generic/GenericTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/generic/GenericTest.java index 909519349..c19113b70 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/generic/GenericTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/test/generic/GenericTest.java @@ -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);