diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java index 26fc4d1dd..d4498023b 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java @@ -407,8 +407,9 @@ protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaService sofaServiceAnnotation, SofaServiceBinding sofaServiceBindingAnnotation, BindingConverterContext bindingConverterContext) { - bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout()); - + if (sofaServiceBindingAnnotation.timeout() != 0) { + bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout()); + } //TODO need a magic number if (sofaServiceBindingAnnotation.weight() != 0) { bindingParam.setWeight(sofaServiceBindingAnnotation.weight()); diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java index 1aa5e24ab..e392a296d 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java @@ -38,6 +38,7 @@ import com.alipay.sofa.rpc.boot.test.bean.threadpool.ThreadPoolService; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.alipay.sofa.rpc.core.exception.SofaTimeOutException; import com.alipay.sofa.runtime.api.annotation.SofaClientFactory; import com.alipay.sofa.runtime.api.annotation.SofaReference; import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding; @@ -136,12 +137,43 @@ public class SofaBootRpcAllTest { @SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", loadBalancer = "roundRobin"), uniqueId = "loadbalancer") private AnnotationService annotationLoadBalancerService; + @SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"), jvmFirst = false, uniqueId = "timeout") + private AnnotationService annotationProviderTimeoutService; + + @SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", timeout = 1000), jvmFirst = false, uniqueId = "timeout") + private AnnotationService annotationConsumerTimeoutService; + @SofaClientFactory private ClientFactory clientFactory; @Autowired private ConsumerConfigContainer consumerConfigContainer; + @Test + public void testTimeoutPriority() throws InterruptedException { + + //If all timeout configuration is not configured, the default timeout time 3000ms will take effect.The interface is ok. + Assert.assertEquals("sleep 2000 ms", annotationService.testTimeout(2000)); + + try { + //If all timeout configuration is not configured, the default timeout time 3000ms will take effect.The call will be time out. + annotationService.testTimeout(4000); + Assert.fail(); + } catch (SofaTimeOutException e) { + + } + //Only configure the provider side timeout 5000ms, and the default timeout time 3000ms will be invalid. + //Assert.assertEquals("sleep 4000 ms", annotationProviderTimeoutService.testTimeout(4000)); + try { + //Configured the consumer side timeout time of 1000ms, the provider side timeout time of 5000ms and the default timeout time of 3000ms are invalid. + annotationConsumerTimeoutService.testTimeout(2000); + Assert.fail(); + } catch (SofaTimeOutException e) { + + } + + } + @Test public void testInvoke() throws InterruptedException { Assert.assertEquals("sync", helloSyncService.saySync("sync")); diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationService.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationService.java index 4b75d1142..870a63e6a 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationService.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationService.java @@ -22,4 +22,6 @@ */ public interface AnnotationService { String hello(); + + String testTimeout(long millis); } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceImpl.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceImpl.java index 16bde1fe1..92d2d6d06 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceImpl.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceImpl.java @@ -32,4 +32,14 @@ public class AnnotationServiceImpl implements AnnotationService { public String hello() { return "Hello, Annotation"; } + + @Override + public String testTimeout(long millis) { + try { + Thread.sleep(millis); + } catch (Exception e) { + + } + return "sleep " + millis + " ms"; + } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServicePbImpl.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServicePbImpl.java index 2799f4dc7..1013bb9ab 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServicePbImpl.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServicePbImpl.java @@ -32,4 +32,9 @@ public class AnnotationServicePbImpl implements AnnotationService { public String hello() { return null; } + + @Override + public String testTimeout(long millis) { + return null; + } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceTimeoutImpl.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceTimeoutImpl.java new file mode 100644 index 000000000..0a14f8d37 --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceTimeoutImpl.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.test.bean.annotation; + +import com.alipay.sofa.runtime.api.annotation.SofaService; +import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding; +import org.springframework.stereotype.Component; + +/** + * @Author: BaoYi + * @Date: 2021/7/21 2:17 下午 + */ +@Component +@SofaService(bindings = { @SofaServiceBinding(bindingType = "bolt", timeout = 5000) }, uniqueId = "timeout") +public class AnnotationServiceTimeoutImpl implements AnnotationService { + + @Override + public String hello() { + return null; + } + + @Override + public String testTimeout(long millis) { + try { + Thread.sleep(millis); + } catch (Exception e) { + + } + return "sleep " + millis + " ms"; + } +} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java index ac608e07f..4e2068207 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java @@ -32,7 +32,7 @@ * * @return timeout */ - int timeout() default 3000; + int timeout() default 0; /** * retry times diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java index dab8a7775..d3721e633 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java @@ -74,7 +74,7 @@ * * @return timeout */ - int timeout() default 3000; + int timeout() default 0; /** * specify serialize type