Example usage for io.netty.handler.traffic ChannelTrafficShapingHandler ChannelTrafficShapingHandler

List of usage examples for io.netty.handler.traffic ChannelTrafficShapingHandler ChannelTrafficShapingHandler

Introduction

In this page you can find the example usage for io.netty.handler.traffic ChannelTrafficShapingHandler ChannelTrafficShapingHandler.

Prototype

public ChannelTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval) 

Source Link

Document

Create a new instance using default max time as delay allowed value of 15000 ms.

Usage

From source file:io.hekate.network.netty.NettyServerClient.java

License:Apache License

private void init(Channel channel, HandshakeRequest request, NettyServerHandler handlerReg) {
    NettyServerHandlerConfig<Object> cfg = handlerReg.config();

    if (cfg.getLoggerCategory() != null) {
        log = LoggerFactory.getLogger(cfg.getLoggerCategory());

        debug = log.isDebugEnabled();//from  w w  w  .  ja  va 2 s.c om
        trace = log.isTraceEnabled();
    }

    this.eventLoop = channel.eventLoop();
    this.serverHandler = cfg.getHandler();
    this.handlerReg = handlerReg;
    this.metrics = handlerReg.metrics();
    this.codec = request.codec();

    // Register this client.
    handlerReg.add(this);

    // Configure metrics.
    if (metrics != null) {
        channel.pipeline()
                .addFirst(new ChannelTrafficShapingHandler(0, 0, NettyInternalConst.TRAFFIC_SHAPING_INTERVAL) {
                    @Override
                    protected void doAccounting(TrafficCounter counter) {
                        metrics.onBytesReceived(counter.lastReadBytes());
                        metrics.onBytesSent(counter.lastWrittenBytes());
                    }
                });
    }

    if (debug) {
        log.debug("Accepted connection [from={}, protocol={}]", address(), cfg.getProtocol());
    }

    // Accept handshake.
    HandshakeAccept accept = new HandshakeAccept(hbInterval, hbLossThreshold, hbDisabled);

    channel.writeAndFlush(accept).addListener(future -> {
        if (channel.isOpen()) {
            connectNotified = true;

            // Notify on connect.
            serverHandler.onConnect(request.payload(), this);
        }
    });
}