Skip to content

Commit 0708241

Browse files
authored
adjust timeout priority (#849)
1 parent 11db148 commit 0708241

File tree

8 files changed

+99
-4
lines changed

8 files changed

+99
-4
lines changed

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,9 @@ protected void convertServiceAnnotation(RpcBindingParam bindingParam,
407407
SofaService sofaServiceAnnotation,
408408
SofaServiceBinding sofaServiceBindingAnnotation,
409409
BindingConverterContext bindingConverterContext) {
410-
bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
411-
410+
if (sofaServiceBindingAnnotation.timeout() != 0) {
411+
bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
412+
}
412413
//TODO need a magic number
413414
if (sofaServiceBindingAnnotation.weight() != 0) {
414415
bindingParam.setWeight(sofaServiceBindingAnnotation.weight());

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.alipay.sofa.rpc.boot.test.bean.threadpool.ThreadPoolService;
3939
import com.alipay.sofa.rpc.config.ConsumerConfig;
4040
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
41+
import com.alipay.sofa.rpc.core.exception.SofaTimeOutException;
4142
import com.alipay.sofa.runtime.api.annotation.SofaClientFactory;
4243
import com.alipay.sofa.runtime.api.annotation.SofaReference;
4344
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
@@ -136,12 +137,43 @@ public class SofaBootRpcAllTest {
136137
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", loadBalancer = "roundRobin"), uniqueId = "loadbalancer")
137138
private AnnotationService annotationLoadBalancerService;
138139

140+
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"), jvmFirst = false, uniqueId = "timeout")
141+
private AnnotationService annotationProviderTimeoutService;
142+
143+
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", timeout = 1000), jvmFirst = false, uniqueId = "timeout")
144+
private AnnotationService annotationConsumerTimeoutService;
145+
139146
@SofaClientFactory
140147
private ClientFactory clientFactory;
141148

142149
@Autowired
143150
private ConsumerConfigContainer consumerConfigContainer;
144151

152+
@Test
153+
public void testTimeoutPriority() throws InterruptedException {
154+
155+
//If all timeout configuration is not configured, the default timeout time 3000ms will take effect.The interface is ok.
156+
Assert.assertEquals("sleep 2000 ms", annotationService.testTimeout(2000));
157+
158+
try {
159+
//If all timeout configuration is not configured, the default timeout time 3000ms will take effect.The call will be time out.
160+
annotationService.testTimeout(4000);
161+
Assert.fail();
162+
} catch (SofaTimeOutException e) {
163+
164+
}
165+
//Only configure the provider side timeout 5000ms, and the default timeout time 3000ms will be invalid.
166+
//Assert.assertEquals("sleep 4000 ms", annotationProviderTimeoutService.testTimeout(4000));
167+
try {
168+
//Configured the consumer side timeout time of 1000ms, the provider side timeout time of 5000ms and the default timeout time of 3000ms are invalid.
169+
annotationConsumerTimeoutService.testTimeout(2000);
170+
Assert.fail();
171+
} catch (SofaTimeOutException e) {
172+
173+
}
174+
175+
}
176+
145177
@Test
146178
public void testInvoke() throws InterruptedException {
147179
Assert.assertEquals("sync", helloSyncService.saySync("sync"));

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
*/
2323
public interface AnnotationService {
2424
String hello();
25+
26+
String testTimeout(long millis);
2527
}

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServiceImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ public class AnnotationServiceImpl implements AnnotationService {
3232
public String hello() {
3333
return "Hello, Annotation";
3434
}
35+
36+
@Override
37+
public String testTimeout(long millis) {
38+
try {
39+
Thread.sleep(millis);
40+
} catch (Exception e) {
41+
42+
}
43+
return "sleep " + millis + " ms";
44+
}
3545
}

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/bean/annotation/AnnotationServicePbImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public class AnnotationServicePbImpl implements AnnotationService {
3232
public String hello() {
3333
return null;
3434
}
35+
36+
@Override
37+
public String testTimeout(long millis) {
38+
return null;
39+
}
3540
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.boot.test.bean.annotation;
18+
19+
import com.alipay.sofa.runtime.api.annotation.SofaService;
20+
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* @Author: BaoYi
25+
* @Date: 2021/7/21 2:17 下午
26+
*/
27+
@Component
28+
@SofaService(bindings = { @SofaServiceBinding(bindingType = "bolt", timeout = 5000) }, uniqueId = "timeout")
29+
public class AnnotationServiceTimeoutImpl implements AnnotationService {
30+
31+
@Override
32+
public String hello() {
33+
return null;
34+
}
35+
36+
@Override
37+
public String testTimeout(long millis) {
38+
try {
39+
Thread.sleep(millis);
40+
} catch (Exception e) {
41+
42+
}
43+
return "sleep " + millis + " ms";
44+
}
45+
}

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @return timeout
3434
*/
35-
int timeout() default 3000;
35+
int timeout() default 0;
3636

3737
/**
3838
* retry times

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaServiceBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
*
7575
* @return timeout
7676
*/
77-
int timeout() default 3000;
77+
int timeout() default 0;
7878

7979
/**
8080
* specify serialize type

0 commit comments

Comments
 (0)