Search in sources :

Example 21 with HttpRequestDecoder

use of io.netty.handler.codec.http.HttpRequestDecoder 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)

Example 22 with HttpRequestDecoder

use of io.netty.handler.codec.http.HttpRequestDecoder 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 23 with HttpRequestDecoder

use of io.netty.handler.codec.http.HttpRequestDecoder in project cloudstack by apache.

the class NfsSecondaryStorageResource method startPostUploadServer.

private void startPostUploadServer() {
    final int PORT = 8210;
    final int NO_OF_WORKERS = 15;
    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup(NO_OF_WORKERS);
    final ServerBootstrap b = new ServerBootstrap();
    final NfsSecondaryStorageResource storageResource = this;
    b.group(bossGroup, workerGroup);
    b.channel(NioServerSocketChannel.class);
    b.handler(new LoggingHandler(LogLevel.INFO));
    b.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new HttpRequestDecoder());
            pipeline.addLast(new HttpResponseEncoder());
            pipeline.addLast(new HttpContentCompressor());
            pipeline.addLast(new HttpUploadServerHandler(storageResource));
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                Channel ch = b.bind(PORT).sync().channel();
                s_logger.info(String.format("Started post upload server on port %d with %d workers", PORT, NO_OF_WORKERS));
                ch.closeFuture().sync();
            } catch (InterruptedException e) {
                s_logger.info("Failed to start post upload server");
                s_logger.debug("Exception while starting post upload server", e);
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
                s_logger.info("shutting down post upload server");
            }
        }
    }.start();
    s_logger.info("created a thread to start post upload server");
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)23 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)19 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)12 ChannelPipeline (io.netty.channel.ChannelPipeline)11 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)6 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)5 SocketChannel (io.netty.channel.socket.SocketChannel)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 LoggingHandler (io.netty.handler.logging.LoggingHandler)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HttpResponseDecoder (io.netty.handler.codec.http.HttpResponseDecoder)3 RequestStateCleanerHandler (com.nike.riposte.server.handler.RequestStateCleanerHandler)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ChannelHandler (io.netty.channel.ChannelHandler)2