Search in sources :

Example 16 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project neo4j by neo4j.

the class CatchupServer method start.

@Override
public synchronized void start() throws Throwable {
    if (channel != null) {
        return;
    }
    workerGroup = new NioEventLoopGroup(0, threadFactory);
    ServerBootstrap bootstrap = new ServerBootstrap().group(workerGroup).channel(NioServerSocketChannel.class).localAddress(listenAddress.socketAddress()).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) {
            CatchupServerProtocol protocol = new CatchupServerProtocol();
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new VersionDecoder(logProvider));
            pipeline.addLast(new VersionPrepender());
            pipeline.addLast(new ResponseMessageTypeEncoder());
            pipeline.addLast(new RequestMessageTypeEncoder());
            pipeline.addLast(new TxPullResponseEncoder());
            pipeline.addLast(new CoreSnapshotEncoder());
            pipeline.addLast(new GetStoreIdResponseEncoder());
            pipeline.addLast(new StoreCopyFinishedResponseEncoder());
            pipeline.addLast(new TxStreamFinishedResponseEncoder());
            pipeline.addLast(new FileChunkEncoder());
            pipeline.addLast(new FileHeaderEncoder());
            pipeline.addLast(new ServerMessageTypeHandler(protocol, logProvider));
            pipeline.addLast(decoders(protocol));
            pipeline.addLast(new TxPullRequestHandler(protocol, storeIdSupplier, dataSourceAvailabilitySupplier, transactionIdStoreSupplier, logicalTransactionStoreSupplier, txPullBatchSize, monitors, logProvider));
            pipeline.addLast(new ChunkedWriteHandler());
            pipeline.addLast(new GetStoreRequestHandler(protocol, dataSourceSupplier, checkPointerSupplier, fs, pageCache, logProvider, storeCopyCheckPointMutex));
            pipeline.addLast(new GetStoreIdRequestHandler(protocol, storeIdSupplier));
            if (coreState != null) {
                pipeline.addLast(new CoreSnapshotRequestHandler(protocol, coreState));
            }
            pipeline.addLast(new ExceptionLoggingHandler(log));
            pipeline.addLast(new ExceptionMonitoringHandler(monitors.newMonitor(ExceptionMonitoringHandler.Monitor.class, CatchupServer.class)));
            pipeline.addLast(new ExceptionSwallowingHandler());
        }
    });
    try {
        channel = bootstrap.bind().syncUninterruptibly().channel();
    } catch (Exception e) {
        //noinspection ConstantConditions
        if (e instanceof BindException) {
            userLog.error("Address is already bound for setting: " + CausalClusteringSettings.transaction_listen_address + " with value: " + listenAddress);
            log.error("Address is already bound for setting: " + CausalClusteringSettings.transaction_listen_address + " with value: " + listenAddress, e);
            throw e;
        }
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ExceptionSwallowingHandler(org.neo4j.causalclustering.handlers.ExceptionSwallowingHandler) VersionPrepender(org.neo4j.causalclustering.VersionPrepender) TxStreamFinishedResponseEncoder(org.neo4j.causalclustering.catchup.tx.TxStreamFinishedResponseEncoder) GetStoreIdResponseEncoder(org.neo4j.causalclustering.catchup.storecopy.GetStoreIdResponseEncoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) CoreSnapshotRequestHandler(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshotRequestHandler) ExceptionLoggingHandler(org.neo4j.causalclustering.handlers.ExceptionLoggingHandler) FileHeaderEncoder(org.neo4j.causalclustering.catchup.storecopy.FileHeaderEncoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) TxPullRequestHandler(org.neo4j.causalclustering.catchup.tx.TxPullRequestHandler) GetStoreRequestHandler(org.neo4j.causalclustering.catchup.storecopy.GetStoreRequestHandler) FileChunkEncoder(org.neo4j.causalclustering.catchup.storecopy.FileChunkEncoder) BindException(java.net.BindException) VersionDecoder(org.neo4j.causalclustering.VersionDecoder) TxPullResponseEncoder(org.neo4j.causalclustering.catchup.tx.TxPullResponseEncoder) StoreCopyFinishedResponseEncoder(org.neo4j.causalclustering.catchup.storecopy.StoreCopyFinishedResponseEncoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) BindException(java.net.BindException) ExceptionMonitoringHandler(org.neo4j.causalclustering.handlers.ExceptionMonitoringHandler) CoreSnapshotEncoder(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshotEncoder) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) GetStoreIdRequestHandler(org.neo4j.causalclustering.catchup.storecopy.GetStoreIdRequestHandler)

Example 17 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project intellij-community by JetBrains.

the class NettyUtil method addHttpServerCodec.

public static void addHttpServerCodec(@NotNull ChannelPipeline pipeline) {
    pipeline.addLast("httpRequestEncoder", new HttpResponseEncoder());
    // https://jetbrains.zendesk.com/agent/tickets/68315
    pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder(16 * 1024, 16 * 1024, 8192));
    pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH));
    // could be added earlier if HTTPS
    if (pipeline.get(ChunkedWriteHandler.class) == null) {
        pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
    }
    pipeline.addLast("corsHandler", new CorsHandlerDoNotUseOwnLogger(CorsConfigBuilder.forAnyOrigin().shortCircuit().allowCredentials().allowNullOrigin().allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.PATCH).allowedRequestHeaders("origin", "accept", "authorization", "content-type", "x-ijt").build()));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder)

Example 18 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project intellij-community by JetBrains.

the class PortUnificationServerHandler method decode.

protected void decode(@NotNull ChannelHandlerContext context, @NotNull ByteBuf buffer) throws Exception {
    ChannelPipeline pipeline = context.pipeline();
    if (detectSsl && SslHandler.isEncrypted(buffer)) {
        SSLEngine engine = SSL_SERVER_CONTEXT.getValue().createSSLEngine();
        engine.setUseClientMode(false);
        pipeline.addLast(new SslHandler(engine), new ChunkedWriteHandler(), new PortUnificationServerHandler(delegatingHttpRequestHandler, false, detectGzip));
    } else {
        int magic1 = buffer.getUnsignedByte(buffer.readerIndex());
        int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
        if (detectGzip && magic1 == 31 && magic2 == 139) {
            pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP), new PortUnificationServerHandler(delegatingHttpRequestHandler, detectSsl, false));
        } else if (isHttp(magic1, magic2)) {
            NettyUtil.addHttpServerCodec(pipeline);
            pipeline.addLast("delegatingHttpHandler", delegatingHttpRequestHandler);
            final Logger logger = Logger.getInstance(BuiltInServer.class);
            if (logger.isDebugEnabled()) {
                pipeline.addLast(new ChannelOutboundHandlerAdapter() {

                    @Override
                    public void write(ChannelHandlerContext context, Object message, ChannelPromise promise) throws Exception {
                        if (message instanceof HttpResponse) {
                            HttpResponse response = (HttpResponse) message;
                            logger.debug("OUT HTTP: " + response.toString());
                        }
                        super.write(context, message, promise);
                    }
                });
            }
        } else if (magic1 == 'C' && magic2 == 'H') {
            buffer.skipBytes(2);
            pipeline.addLast(new CustomHandlerDelegator());
        } else {
            Logger.getInstance(BuiltInServer.class).warn("unknown request, first two bytes " + magic1 + " " + magic2);
            context.close();
        }
    }
    // must be after new channels handlers addition (netty bug?)
    pipeline.remove(this);
    // Buffer will be automatically released after messageReceived, but we pass it to next handler, and next handler will also release, so, we must retain.
    // We can introduce Decoder.isAutoRelease, but in this case, if error will be thrown while we are executing, buffer will not be released.
    // So, it is robust solution just always release (Decoder does) and just retain (we - client) if autorelease behavior is not suitable.
    buffer.retain();
    // we must fire channel read - new added handler must read buffer
    context.fireChannelRead(buffer);
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) HttpResponse(io.netty.handler.codec.http.HttpResponse) Logger(com.intellij.openapi.diagnostic.Logger) SslHandler(io.netty.handler.ssl.SslHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler)

Example 19 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project async-http-client by AsyncHttpClient.

the class HttpStaticFileServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpStaticFileServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 20 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project motan by weibocom.

the class Netty4HttpServer method open.

@Override
public boolean open() {
    if (isAvailable()) {
        return true;
    }
    if (channel != null) {
        channel.close();
    }
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
    }
    boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    // TODO max connection protect
    int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
    int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), 500);
    int minWorkerThread = 0, maxWorkerThread = 0;
    if (shareChannel) {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
    } else {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
    }
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    final NettyHttpRequestHandler handler = new NettyHttpRequestHandler(this, messageHandler, new ThreadPoolExecutor(minWorkerThread, maxWorkerThread, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workerQueueSize)));
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
            ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(maxContentLength));
            ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
            ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
            ch.pipeline().addLast("serverHandler", handler);
        }
    }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false);
    ChannelFuture f;
    try {
        f = b.bind(url.getPort()).sync();
        channel = f.channel();
    } catch (InterruptedException e) {
        LoggerUtil.error("init http server fail.", e);
        return false;
    }
    state = ChannelState.ALIVE;
    StatsUtil.registryStatisticCallback(this);
    LoggerUtil.info("Netty4HttpServer ServerChannel finish Open: url=" + url);
    return true;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)21 ChannelPipeline (io.netty.channel.ChannelPipeline)12 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)6 SslHandler (io.netty.handler.ssl.SslHandler)6 SocketChannel (io.netty.channel.socket.SocketChannel)5 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)5 LoggingHandler (io.netty.handler.logging.LoggingHandler)5 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 Channel (io.netty.channel.Channel)4 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)4 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)4 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelFuture (io.netty.channel.ChannelFuture)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)3 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)3 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)2