Java Code Examples for io.netty.util.concurrent.Promise#isDone()

The following examples show how to use io.netty.util.concurrent.Promise#isDone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void applyHandshakeTimeout(Promise<Channel> p) {
    final Promise<Channel> promise = p == null ? handshakePromise : p;
    // Set timeout if necessary.
    final long handshakeTimeoutMillis = this.handshakeTimeoutMillis;
    if (handshakeTimeoutMillis <= 0 || promise.isDone()) {
        return;
    }

    final ScheduledFuture<?> timeoutFuture = ctx.executor().schedule(new Runnable() {
        @Override
        public void run() {
            if (promise.isDone()) {
                return;
            }
            try {
                if (handshakePromise.tryFailure(HANDSHAKE_TIMED_OUT)) {
                    SslUtils.handleHandshakeFailure(ctx, HANDSHAKE_TIMED_OUT, true);
                }
            } finally {
                releaseAndFailAll(HANDSHAKE_TIMED_OUT);
            }
        }
    }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);

    // Cancel the handshake timeout when handshake is finished.
    promise.addListener(new FutureListener<Channel>() {
        @Override
        public void operationComplete(Future<Channel> f) throws Exception {
            timeoutFuture.cancel(false);
        }
    });
}
 
Example 2
Source File: HealthCheckedChannelPool.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Try to acquire a channel from the underlying pool. This will keep retrying the acquisition until the provided result
 * future is completed.
 *
 * @param resultFuture The future that should be completed with the acquired channel. If this is completed external to this
 * function, this function will stop trying to acquire a channel.
 * @param timeoutFuture The future for the timeout task. This future will be cancelled when a channel is acquired.
 */
private void tryAcquire(Promise<Channel> resultFuture, ScheduledFuture<?> timeoutFuture) {
    // Something else completed the future (probably a timeout). Stop trying to get a channel.
    if (resultFuture.isDone()) {
        return;
    }

    Promise<Channel> delegateFuture = eventLoopGroup.next().newPromise();
    delegate.acquire(delegateFuture);
    delegateFuture.addListener(f -> ensureAcquiredChannelIsHealthy(delegateFuture, resultFuture, timeoutFuture));
}
 
Example 3
Source File: HttpChannelPool.java    From armeria with Apache License 2.0 5 votes vote down vote up
private void connect(SessionProtocol desiredProtocol, PoolKey key, ChannelAcquisitionFuture promise,
                     ClientConnectionTimingsBuilder timingsBuilder) {
    setPendingAcquisition(desiredProtocol, key, promise);
    timingsBuilder.socketConnectStart();

    final InetSocketAddress remoteAddress;
    try {
        remoteAddress = toRemoteAddress(key);
    } catch (UnknownHostException e) {
        notifyConnect(desiredProtocol, key, eventLoop.newFailedFuture(e), promise, timingsBuilder);
        return;
    }

    // Fail immediately if it is sure that the remote address doesn't support the desired protocol.
    if (SessionProtocolNegotiationCache.isUnsupported(remoteAddress, desiredProtocol)) {
        notifyConnect(desiredProtocol, key,
                      eventLoop.newFailedFuture(
                              new SessionProtocolNegotiationException(
                                      desiredProtocol, "previously failed negotiation")),
                      promise, timingsBuilder);
        return;
    }

    // Create a new connection.
    final Promise<Channel> sessionPromise = eventLoop.newPromise();
    connect(remoteAddress, desiredProtocol, sessionPromise);

    if (sessionPromise.isDone()) {
        notifyConnect(desiredProtocol, key, sessionPromise, promise, timingsBuilder);
    } else {
        sessionPromise.addListener((Future<Channel> future) -> {
            notifyConnect(desiredProtocol, key, future, promise, timingsBuilder);
        });
    }
}
 
Example 4
Source File: ProxyEndpoint.java    From zuul with Apache License 2.0 5 votes vote down vote up
private void proxyRequestToOrigin() {
    Promise<PooledConnection> promise = null;
    try {
        attemptNum += 1;

        IClientConfig requestConfig = origin.getExecutionContext(zuulRequest).getRequestConfig();
        setReadTimeoutOnContext(requestConfig, attemptNum);

        currentRequestStat = createRequestStat();
        origin.preRequestChecks(zuulRequest);
        concurrentReqCount++;

        // update RPS trackers
        updateOriginRpsTrackers(origin, attemptNum);

        // We pass this AtomicReference<Server> here and the origin impl will assign the chosen server to it.
        promise = origin.connectToOrigin(
                zuulRequest, channelCtx.channel().eventLoop(), attemptNum, passport, chosenServer, chosenHostAddr);

        storeAndLogOriginRequestInfo();
        currentRequestAttempt = origin.newRequestAttempt(chosenServer.get(), context, attemptNum);
        requestAttempts.add(currentRequestAttempt);
        passport.add(PassportState.ORIGIN_CONN_ACQUIRE_START);

        if (promise.isDone()) {
            operationComplete(promise);
        } else {
            promise.addListener(this);
        }
    }
    catch (Exception ex) {
        LOG.error("Error while connecting to origin, UUID {} " + context.getUUID(), ex);
        storeAndLogOriginRequestInfo();
        if (promise != null && ! promise.isDone()) {
            promise.setFailure(ex);
        } else {
            errorFromOrigin(ex);
        }
    }
}