View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.traffic;
17  
18  import org.jboss.netty.channel.ChannelHandlerContext;
19  import org.jboss.netty.channel.ChannelPipelineFactory;
20  import org.jboss.netty.channel.ChannelStateEvent;
21  import org.jboss.netty.handler.execution.ExecutionHandler;
22  import org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor;
23  import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
24  import org.jboss.netty.util.ObjectSizeEstimator;
25  import org.jboss.netty.util.Timer;
26  
27  /**
28   * This implementation of the {@link AbstractTrafficShapingHandler} is for channel
29   * traffic shaping, that is to say a per channel limitation of the bandwidth.<br><br>
30   *
31   * The general use should be as follow:<br>
32   * <ul>
33   * <li>Add in your pipeline a new ChannelTrafficShapingHandler, before a recommended {@link ExecutionHandler} (like
34   * {@link OrderedMemoryAwareThreadPoolExecutor} or {@link MemoryAwareThreadPoolExecutor}).<br>
35   * <tt>ChannelTrafficShapingHandler myHandler = new ChannelTrafficShapingHandler(timer);</tt><br>
36   * timer could be created using <tt>HashedWheelTimer</tt><br>
37   * <tt>pipeline.addLast("CHANNEL_TRAFFIC_SHAPING", myHandler);</tt><br><br>
38   *
39   * <b>Note that this handler has a Pipeline Coverage of "one" which means a new handler must be created
40   * for each new channel as the counter cannot be shared among all channels.</b> For instance, if you have a
41   * {@link ChannelPipelineFactory}, you should create a new ChannelTrafficShapingHandler in this
42   * {@link ChannelPipelineFactory} each time getPipeline() method is called.<br><br>
43   *
44   * Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation)
45   * or the check interval (in millisecond) that represents the delay between two computations of the
46   * bandwidth and so the call back of the doAccounting method (0 means no accounting at all).<br><br>
47   *
48   * A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting,
49   * it is recommended to set a positive value, even if it is high since the precision of the
50   * Traffic Shaping depends on the period where the traffic is computed. The highest the interval,
51   * the less precise the traffic shaping will be. It is suggested as higher value something close
52   * to 5 or 10 minutes.<br>
53   * </li>
54   * <li>When you shutdown your application, release all the external resources (except the timer internal itself)
55   * by calling:<br>
56   * <tt>myHandler.releaseExternalResources();</tt><br>
57   * </li>
58   * </ul><br>
59   */
60  public class ChannelTrafficShapingHandler extends AbstractTrafficShapingHandler {
61  
62      public ChannelTrafficShapingHandler(Timer timer, long writeLimit,
63              long readLimit, long checkInterval) {
64          super(timer, writeLimit, readLimit, checkInterval);
65      }
66  
67      public ChannelTrafficShapingHandler(Timer timer, long writeLimit,
68              long readLimit) {
69          super(timer, writeLimit, readLimit);
70      }
71  
72      public ChannelTrafficShapingHandler(Timer timer, long checkInterval) {
73          super(timer, checkInterval);
74      }
75  
76      public ChannelTrafficShapingHandler(Timer timer) {
77          super(timer);
78      }
79  
80      public ChannelTrafficShapingHandler(
81              ObjectSizeEstimator objectSizeEstimator, Timer timer,
82              long writeLimit, long readLimit, long checkInterval) {
83          super(objectSizeEstimator, timer, writeLimit, readLimit,
84                  checkInterval);
85      }
86  
87      public ChannelTrafficShapingHandler(
88              ObjectSizeEstimator objectSizeEstimator, Timer timer,
89              long writeLimit, long readLimit) {
90          super(objectSizeEstimator, timer, writeLimit, readLimit);
91      }
92  
93      public ChannelTrafficShapingHandler(
94              ObjectSizeEstimator objectSizeEstimator, Timer timer,
95              long checkInterval) {
96          super(objectSizeEstimator, timer, checkInterval);
97      }
98  
99      public ChannelTrafficShapingHandler(
100             ObjectSizeEstimator objectSizeEstimator, Timer timer) {
101         super(objectSizeEstimator, timer);
102     }
103 
104     @Override
105     public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
106             throws Exception {
107         if (trafficCounter != null) {
108             trafficCounter.stop();
109         }
110         super.channelClosed(ctx, e);
111     }
112 
113     @Override
114     public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
115             throws Exception {
116         // readSuspended = true;
117         ctx.setAttachment(Boolean.TRUE);
118         ctx.getChannel().setReadable(false);
119         if (trafficCounter == null) {
120             // create a new counter now
121             if (timer != null) {
122                 trafficCounter = new TrafficCounter(this, timer, "ChannelTC" +
123                         ctx.getChannel().getId(), checkInterval);
124             }
125         }
126         if (trafficCounter != null) {
127             trafficCounter.start();
128         }
129         super.channelConnected(ctx, e);
130         // readSuspended = false;
131         ctx.setAttachment(null);
132         ctx.getChannel().setReadable(true);
133     }
134 
135 }