Search in sources :

Example 86 with ChannelHandler

use of io.netty.channel.ChannelHandler in project ambry by linkedin.

the class FrontendNettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    // If channel handler implementations are not annotated with @Sharable, Netty creates a new instance of every class
    // in the pipeline for every connection.
    // i.e. if there are a 1000 active connections there will be a 1000 NettyMessageProcessor instances.
    ChannelPipeline pipeline = ch.pipeline();
    // connection stats handler to track connection related metrics
    pipeline.addLast("connectionStatsHandler", connectionStatsHandler);
    // if SSL is enabled, add an SslHandler before the HTTP codec
    if (sslFactory != null) {
        InetSocketAddress peerAddress = ch.remoteAddress();
        String peerHost = peerAddress.getHostName();
        int peerPort = peerAddress.getPort();
        SslHandler sslHandler = new SslHandler(sslFactory.createSSLEngine(peerHost, peerPort, SSLFactory.Mode.SERVER));
        pipeline.addLast("sslHandler", sslHandler);
    }
    pipeline.addLast("codec", new HttpServerCodec(nettyConfig.nettyServerMaxInitialLineLength, nettyConfig.nettyServerMaxHeaderSize, nettyConfig.nettyServerMaxChunkSize)).addLast("healthCheckHandler", new HealthCheckHandler(restServerState, nettyMetrics)).addLast("publicAccessLogHandler", new PublicAccessLogHandler(publicAccessLogger, nettyMetrics)).addLast("idleStateHandler", new IdleStateHandler(0, 0, nettyConfig.nettyServerIdleTimeSeconds)).addLast("chunker", new ChunkedWriteHandler());
    if (addedChannelHandlers != null) {
        pipeline.addLast(addedChannelHandlers.toArray(new ChannelHandler[0]));
    }
    // custom processing class that interfaces with a RestRequestService.
    pipeline.addLast("processor", new NettyMessageProcessor(nettyMetrics, nettyConfig, performanceConfig, requestHandler));
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) InetSocketAddress(java.net.InetSocketAddress) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 87 with ChannelHandler

use of io.netty.channel.ChannelHandler in project pravega by pravega.

the class AdminConnectionListener method createEncodingStack.

@Override
public List<ChannelHandler> createEncodingStack(String connectionName) {
    List<ChannelHandler> stack = new ArrayList<>();
    stack.add(new ExceptionLoggingHandler(connectionName));
    stack.add(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER));
    stack.add(new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4));
    stack.add(new CommandDecoder());
    return stack;
}
Also used : CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) ArrayList(java.util.ArrayList) ChannelHandler(io.netty.channel.ChannelHandler) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 88 with ChannelHandler

use of io.netty.channel.ChannelHandler in project pravega by pravega.

the class PravegaConnectionListener method createEncodingStack.

@Override
public List<ChannelHandler> createEncodingStack(String connectionName) {
    List<ChannelHandler> stack = new ArrayList<>();
    stack.add(new ExceptionLoggingHandler(connectionName));
    stack.add(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER));
    stack.add(new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4));
    stack.add(new CommandDecoder());
    stack.add(new AppendDecoder());
    return stack;
}
Also used : AppendDecoder(io.pravega.shared.protocol.netty.AppendDecoder) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) ArrayList(java.util.ArrayList) ChannelHandler(io.netty.channel.ChannelHandler) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 89 with ChannelHandler

use of io.netty.channel.ChannelHandler in project janusgraph by JanusGraph.

the class SaslAndHMACAuthenticationHandlerTest method testHttpChannelReadWhenAuthenticatorHasBeenAdded.

@Test
public void testHttpChannelReadWhenAuthenticatorHasBeenAdded() throws Exception {
    final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
    final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
    final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
    final ChannelHandler mockHandler = createMock(ChannelHandler.class);
    final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
    final HttpMessage msg = createMock(HttpMessage.class);
    final HttpHeaders headers = createMock(HttpHeaders.class);
    expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
    expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
    expect(ctx.pipeline()).andReturn(pipeline);
    expect(pipeline.get("hmac_authenticator")).andReturn(mockHandler);
    expect(msg.headers()).andReturn(headers).times(2);
    expect(headers.get(isA(String.class))).andReturn(null).times(2);
    expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
    replayAll();
    final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
    handler.channelRead(ctx, msg);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) JanusGraphSimpleAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator) SaslAndHMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator) HMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator) SaslAndHMACAuthenticator(org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) HttpMessage(io.netty.handler.codec.http.HttpMessage) ChannelPipeline(io.netty.channel.ChannelPipeline) Test(org.junit.jupiter.api.Test)

Example 90 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class RequestStateCleanerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        // New request incoming - setup/clear *all* state objects for new requests
        for (ProcessingStateClassAndKeyPair<? extends ProcessingState> stateClassAndKeyPair : PROCESSING_STATE_ATTRIBUTE_KEYS) {
            // See if we have an existing state object for this channel for the given state type.
            @SuppressWarnings("unchecked") AttributeKey<ProcessingState> attrKey = (AttributeKey<ProcessingState>) stateClassAndKeyPair.getRight();
            Attribute<ProcessingState> processingStateAttr = ctx.channel().attr(attrKey);
            ProcessingState processingState = processingStateAttr.get();
            if (processingState == null) {
                // We don't already have one for this channel, so create one and register it.
                processingState = stateClassAndKeyPair.getLeft().newInstance();
                processingStateAttr.set(processingState);
            }
            // Clean the state for the new request.
            processingState.cleanStateForNewRequest();
        }
        HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        // Set the DistributedTracingConfig on the HttpProcessingState.
        // noinspection deprecation - This is the only place that should actually be calling this method.
        httpProcessingState.setDistributedTracingConfig(distributedTracingConfig);
        // Send a request received event to the metricsListener.
        if (metricsListener != null) {
            metricsListener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, httpProcessingState);
        }
        // Remove the idle channel timeout handler (if there is one) so that it doesn't kill this new request if the
        // endpoint takes longer to complete than the idle timeout value - the idle channel timeout is only for
        // timing out channels that are idle *in-between* requests.
        ChannelPipeline pipeline = ctx.pipeline();
        ChannelHandler idleChannelTimeoutHandler = pipeline.get(HttpChannelInitializer.IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
        if (idleChannelTimeoutHandler != null)
            pipeline.remove(idleChannelTimeoutHandler);
        // last chunk when the timeout hits.
        if (incompleteHttpCallTimeoutMillis > 0 && !(msg instanceof LastHttpContent)) {
            IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler(incompleteHttpCallTimeoutMillis);
            ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
            if (existingHandler == null) {
                pipeline.addFirst(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
            } else {
                logger.error("Handling HttpRequest for new request and found an IncompleteHttpCallTimeoutHandler " + "already in the pipeline. This should not be possible. A new " + "IncompleteHttpCallTimeoutHandler will replace the old one. worker_channel_id={}", ctx.channel().toString());
                pipeline.replace(existingHandler, INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
            }
        }
        ProxyRouterProcessingState proxyRouterProcessingState = ChannelAttributes.getProxyRouterProcessingStateForChannel(ctx).get();
        // Set the DistributedTracingConfig on the ProxyRouterProcessingState.
        // noinspection deprecation - This is the only place that should actually be calling this method.
        proxyRouterProcessingState.setDistributedTracingConfig(distributedTracingConfig);
    } else if (msg instanceof LastHttpContent) {
        // The HTTP call is complete, so we can remove the IncompleteHttpCallTimeoutHandler.
        ChannelPipeline pipeline = ctx.pipeline();
        ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
        if (existingHandler != null)
            pipeline.remove(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
    }
    // Continue on the pipeline processing.
    super.channelRead(ctx, msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) ChannelHandler(io.netty.channel.ChannelHandler) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelPipeline(io.netty.channel.ChannelPipeline) AttributeKey(io.netty.util.AttributeKey) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ProcessingState(com.nike.riposte.server.http.ProcessingState) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11