Skip to content

Commit

Permalink
adjust timeout priority (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvenLjj authored Jul 27, 2021
1 parent 11db148 commit 0708241
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
*/
public interface AnnotationService {
String hello();

String testTimeout(long millis);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public class AnnotationServicePbImpl implements AnnotationService {
public String hello() {
return null;
}

@Override
public String testTimeout(long millis) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @return timeout
*/
int timeout() default 3000;
int timeout() default 0;

/**
* retry times
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
*
* @return timeout
*/
int timeout() default 3000;
int timeout() default 0;

/**
* specify serialize type
Expand Down

0 comments on commit 0708241

Please sign in to comment.