Adjust the logic of timeout, fix triple about timeout countdown function problem (#11030)

Signed-off-by: crazyhzm <crazyhzm@gmail.com>

Signed-off-by: crazyhzm <crazyhzm@gmail.com>
This commit is contained in:
huazhongming 2022-11-28 16:56:31 +08:00 committed by GitHub
parent 7aaba188e1
commit 4568958ef5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 20 deletions

View File

@ -261,26 +261,21 @@ public class RpcUtils {
return timeout;
}
public static long getTimeout(URL url, String methodName, RpcContext context, long defaultTimeout) {
public static long getTimeout(URL url, String methodName, RpcContext context, Invocation invocation, long defaultTimeout) {
long timeout = defaultTimeout;
Object genericTimeout = context.getObjectAttachment(TIMEOUT_KEY);
if (genericTimeout != null) {
timeout = convertToNumber(genericTimeout, defaultTimeout);
Object timeoutFromContext = context.getObjectAttachment(TIMEOUT_KEY);
Object timeoutFromInvocation = invocation.getObjectAttachment(TIMEOUT_KEY);
if (timeoutFromContext != null) {
timeout = convertToNumber(timeoutFromContext, defaultTimeout);
} else if (timeoutFromInvocation != null) {
timeout = convertToNumber(timeoutFromInvocation, defaultTimeout);
} else if (url != null) {
timeout = url.getMethodPositiveParameter(methodName, TIMEOUT_KEY, defaultTimeout);
}
return timeout;
}
public static long getTimeoutFromInvocation(Invocation invocation, long defaultTimeout) {
long timeout = defaultTimeout;
Object genericTimeout = invocation.getObjectAttachment(TIMEOUT_KEY);
if (genericTimeout != null) {
timeout = convertToNumber(genericTimeout, defaultTimeout);
}
return timeout;
}
private static long convertToNumber(Object obj, long defaultTimeout) {
long timeout = defaultTimeout;
try {

View File

@ -178,7 +178,7 @@ public class DubboInvoker<T> extends AbstractInvoker<T> {
Object countdown = RpcContext.getClientAttachment().getObjectAttachment(TIME_COUNTDOWN_KEY);
int timeout;
if (countdown == null) {
timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getClientAttachment(), DEFAULT_TIMEOUT);
timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getClientAttachment(), invocation, DEFAULT_TIMEOUT);
if (getUrl().getParameter(ENABLE_TIMEOUT_COUNTDOWN_KEY, false)) {
invocation.setObjectAttachment(TIMEOUT_ATTACHMENT_KEY, timeout); // pass timeout to remote server
}

View File

@ -283,7 +283,7 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
Object countdown = RpcContext.getClientAttachment().getObjectAttachment(TIME_COUNTDOWN_KEY);
int timeout;
if (countdown == null) {
timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getClientAttachment(), DEFAULT_TIMEOUT);
timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getClientAttachment(), invocation, DEFAULT_TIMEOUT);
if (getUrl().getParameter(ENABLE_TIMEOUT_COUNTDOWN_KEY, false)) {
invocation.setObjectAttachment(TIMEOUT_ATTACHMENT_KEY, timeout); // pass timeout to remote server
}

View File

@ -106,7 +106,7 @@ public class TripleInvoker<T> extends AbstractInvoker<T> {
if (!connection.isAvailable()) {
CompletableFuture<AppResponse> future = new CompletableFuture<>();
RpcException exception = TriRpcStatus.UNAVAILABLE.withDescription(
String.format("upstream %s is unavailable", getUrl().getAddress()))
String.format("upstream %s is unavailable", getUrl().getAddress()))
.asException();
future.completeExceptionally(exception);
return new AsyncRpcResult(future, invocation);
@ -301,14 +301,11 @@ public class TripleInvoker<T> extends AbstractInvoker<T> {
}
private int calculateTimeout(Invocation invocation, String methodName) {
if (invocation.getObjectAttachment(TIMEOUT_KEY) != null) {
return (int) RpcUtils.getTimeoutFromInvocation(invocation, 3000);
}
Object countdown = RpcContext.getClientAttachment().getObjectAttachment(TIME_COUNTDOWN_KEY);
int timeout;
if (countdown == null) {
timeout = (int) RpcUtils.getTimeout(getUrl(), methodName,
RpcContext.getClientAttachment(), 3000);
RpcContext.getClientAttachment(), invocation, 3000);
if (getUrl().getParameter(ENABLE_TIMEOUT_COUNTDOWN_KEY, false)) {
invocation.setObjectAttachment(TIMEOUT_ATTACHMENT_KEY,
timeout); // pass timeout to remote server