Search in sources :

Example 1 with ChannelPromise

use of com.couchbase.client.core.deps.io.netty.channel.ChannelPromise in project couchbase-jvm-clients by couchbase.

the class FeatureNegotiatingHandler method connect.

/**
 * Intercepts the connect process inside the pipeline to only propagate either
 * success or failure if the hello process is completed either way.
 *
 * <p>Note that if no feature is to negotiate we can bail out right away.</p>
 *
 * @param ctx           the {@link ChannelHandlerContext} for which the connect operation is made.
 * @param remoteAddress the {@link SocketAddress} to which it should connect.
 * @param localAddress  the {@link SocketAddress} which is used as source on connect.
 * @param promise       the {@link ChannelPromise} to notify once the operation completes.
 */
@Override
public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
    if (features.isEmpty()) {
        ConnectTimings.record(ctx.channel(), this.getClass());
        ctx.pipeline().remove(this);
        ctx.connect(remoteAddress, localAddress, promise);
    } else {
        interceptedConnectPromise = promise;
        ChannelPromise downstream = ctx.newPromise();
        downstream.addListener(f -> {
            if (!f.isSuccess() && !interceptedConnectPromise.isDone()) {
                ConnectTimings.record(ctx.channel(), this.getClass());
                interceptedConnectPromise.tryFailure(f.cause());
            }
        });
        ctx.connect(remoteAddress, localAddress, downstream);
    }
}
Also used : ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)

Example 2 with ChannelPromise

use of com.couchbase.client.core.deps.io.netty.channel.ChannelPromise in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method propagateConnectFailureFromDownstream.

/**
 * This test verifies that if a downstream promise fails that the error
 * is propagated through the captured promise.
 */
@Test
void propagateConnectFailureFromDownstream() {
    final Exception connectException = new Exception("I failed");
    ChannelDuplexHandler failingHandler = new ChannelDuplexHandler() {

        @Override
        public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
            promise.setFailure(connectException);
        }
    };
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(failingHandler).addLast(handler);
    ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertEquals(connectException, connect.awaitUninterruptibly().cause());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ChannelDuplexHandler(com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 3 with ChannelPromise

use of com.couchbase.client.core.deps.io.netty.channel.ChannelPromise in project couchbase-jvm-clients by couchbase.

the class SelectBucketHandler method connect.

@Override
public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
    interceptedConnectPromise = promise;
    ChannelPromise downstream = ctx.newPromise();
    downstream.addListener(f -> {
        if (!f.isSuccess() && !interceptedConnectPromise.isDone()) {
            ConnectTimings.record(ctx.channel(), this.getClass());
            interceptedConnectPromise.tryFailure(f.cause());
        }
    });
    ctx.connect(remoteAddress, localAddress, downstream);
}
Also used : ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)

Example 4 with ChannelPromise

use of com.couchbase.client.core.deps.io.netty.channel.ChannelPromise in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandlerTest method propagateConnectFailureFromDownstream.

/**
 * This test verifies that if a downstream promise fails that the error
 * is propagated through the captured promise.
 */
@Test
void propagateConnectFailureFromDownstream() {
    final Exception connectException = new Exception("I failed");
    ChannelDuplexHandler failingHandler = new ChannelDuplexHandler() {

        @Override
        public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
            promise.setFailure(connectException);
        }
    };
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(failingHandler).addLast(handler);
    ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertEquals(connectException, connect.awaitUninterruptibly().cause());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ChannelDuplexHandler(com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 5 with ChannelPromise

use of com.couchbase.client.core.deps.io.netty.channel.ChannelPromise in project couchbase-jvm-clients by couchbase.

the class SaslAuthenticationHandler method connect.

/**
 * Intercepts the connect process inside the pipeline to only propagate either
 * success or failure if the hello process is completed either way.
 *
 * @param ctx           the {@link ChannelHandlerContext} for which the connect operation is made.
 * @param remoteAddress the {@link SocketAddress} to which it should connect.
 * @param localAddress  the {@link SocketAddress} which is used as source on connect.
 * @param promise       the {@link ChannelPromise} to notify once the operation completes.
 */
@Override
public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
    interceptedConnectPromise = promise;
    ChannelPromise downstream = ctx.newPromise();
    downstream.addListener(f -> {
        if (!f.isSuccess() && !interceptedConnectPromise.isDone()) {
            ConnectTimings.record(ctx.channel(), this.getClass());
            interceptedConnectPromise.tryFailure(f.cause());
        }
    });
    ctx.connect(remoteAddress, localAddress, downstream);
}
Also used : ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)

Aggregations

ChannelPromise (com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)10 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 Test (org.junit.jupiter.api.Test)5 ChannelDuplexHandler (com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler)4 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketAddress (java.net.SocketAddress)4 TimeoutException (java.util.concurrent.TimeoutException)4 EndpointConnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent)1 EndpointDisconnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent)1 ChannelException (com.couchbase.client.core.deps.io.netty.channel.ChannelException)1 ChannelOutboundHandlerAdapter (com.couchbase.client.core.deps.io.netty.channel.ChannelOutboundHandlerAdapter)1 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)1 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1