Search in sources :

Example 1 with TimeoutCountDown

use of org.apache.dubbo.rpc.TimeoutCountDown in project dubbo by alibaba.

the class DubboInvoker method calculateTimeout.

private int calculateTimeout(Invocation invocation, String methodName) {
    Object countdown = RpcContext.getContext().get(TIME_COUNTDOWN_KEY);
    int timeout = DEFAULT_TIMEOUT;
    if (countdown == null) {
        timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getContext(), DEFAULT_TIMEOUT);
        if (getUrl().getParameter(ENABLE_TIMEOUT_COUNTDOWN_KEY, false)) {
            // pass timeout to remote server
            invocation.setObjectAttachment(TIMEOUT_ATTACHMENT_KEY, timeout);
        }
    } else {
        TimeoutCountDown timeoutCountDown = (TimeoutCountDown) countdown;
        timeout = (int) timeoutCountDown.timeRemaining(TimeUnit.MILLISECONDS);
        // pass timeout to remote server
        invocation.setObjectAttachment(TIMEOUT_ATTACHMENT_KEY, timeout);
    }
    return timeout;
}
Also used : TimeoutCountDown(org.apache.dubbo.rpc.TimeoutCountDown)

Example 2 with TimeoutCountDown

use of org.apache.dubbo.rpc.TimeoutCountDown in project dubbo by alibaba.

the class ConsumerContextFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext context = RpcContext.getContext();
    context.setInvoker(invoker).setInvocation(invocation).setLocalAddress(NetUtils.getLocalHost(), 0).setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()).setRemoteApplicationName(invoker.getUrl().getParameter(REMOTE_APPLICATION_KEY)).setAttachment(REMOTE_APPLICATION_KEY, invoker.getUrl().getParameter(APPLICATION_KEY));
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation) invocation).setInvoker(invoker);
    }
    // pass default timeout set by end user (ReferenceConfig)
    Object countDown = context.get(TIME_COUNTDOWN_KEY);
    if (countDown != null) {
        TimeoutCountDown timeoutCountDown = (TimeoutCountDown) countDown;
        if (timeoutCountDown.isExpired()) {
            return AsyncRpcResult.newDefaultAsyncResult(new RpcException(RpcException.TIMEOUT_TERMINATE, "No time left for making the following call: " + invocation.getServiceName() + "." + invocation.getMethodName() + ", terminate directly."), invocation);
        }
    }
    return invoker.invoke(invocation);
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcException(org.apache.dubbo.rpc.RpcException) TimeoutCountDown(org.apache.dubbo.rpc.TimeoutCountDown)

Aggregations

TimeoutCountDown (org.apache.dubbo.rpc.TimeoutCountDown)2 RpcContext (org.apache.dubbo.rpc.RpcContext)1 RpcException (org.apache.dubbo.rpc.RpcException)1 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)1