All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.handler.traffic.ChannelTrafficShapingHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2011 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.handler.traffic;

import java.util.concurrent.Executor;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipelineFactory;
import io.netty.channel.ChannelStateEvent;
import io.netty.handler.execution.ExecutionHandler;
import io.netty.handler.execution.MemoryAwareThreadPoolExecutor;
import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;

/**
 * This implementation of the {@link AbstractTrafficShapingHandler} is for channel
 * traffic shaping, that is to say a per channel limitation of the bandwidth.

* * The general use should be as follow:
*
    *
  • Add in your pipeline a new ChannelTrafficShapingHandler, before a recommended {@link ExecutionHandler} (like * {@link OrderedMemoryAwareThreadPoolExecutor} or {@link MemoryAwareThreadPoolExecutor}).
    * ChannelTrafficShapingHandler myHandler = new ChannelTrafficShapingHandler(executor);
    * executor could be created using Executors.newCachedThreadPool();
    * pipeline.addLast("CHANNEL_TRAFFIC_SHAPING", myHandler);

    * * Note that this handler has a Pipeline Coverage of "one" which means a new handler must be created * for each new channel as the counter cannot be shared among all channels. For instance, if you have a * {@link ChannelPipelineFactory}, you should create a new ChannelTrafficShapingHandler in this * {@link ChannelPipelineFactory} each time getPipeline() method is called.

    * * Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation) * or the check interval (in millisecond) that represents the delay between two computations of the * bandwidth and so the call back of the doAccounting method (0 means no accounting at all).

    * * A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting, * it is recommended to set a positive value, even if it is high since the precision of the * Traffic Shaping depends on the period where the traffic is computed. The highest the interval, * the less precise the traffic shaping will be. It is suggested as higher value something close * to 5 or 10 minutes.
    *
  • *
  • When you shutdown your application, release all the external resources like the executor * by calling:
    * myHandler.releaseExternalResources();
    *
  • *

*/ public class ChannelTrafficShapingHandler extends AbstractTrafficShapingHandler { /** * @param executor * @param writeLimit * @param readLimit * @param checkInterval */ public ChannelTrafficShapingHandler(Executor executor, long writeLimit, long readLimit, long checkInterval) { super(executor, writeLimit, readLimit, checkInterval); } /** * @param executor * @param writeLimit * @param readLimit */ public ChannelTrafficShapingHandler(Executor executor, long writeLimit, long readLimit) { super(executor, writeLimit, readLimit); } @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { if (trafficCounter != null) { trafficCounter.stop(); trafficCounter = null; } super.channelClosed(ctx, e); } @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // readSuspended = true; ctx.setAttachment(Boolean.TRUE); ctx.getChannel().setReadable(false); if (trafficCounter == null) { // create a new counter now trafficCounter = new TrafficCounter(this, executor, "ChannelTC" + ctx.getChannel().getId(), checkInterval); } if (trafficCounter != null) { trafficCounter.start(); } super.channelConnected(ctx, e); // readSuspended = false; ctx.setAttachment(null); ctx.getChannel().setReadable(true); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy