View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.ChannelHandler.Sharable;
19  import org.jboss.netty.handler.execution.ExecutionHandler;
20  import org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor;
21  import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
22  import org.jboss.netty.util.ObjectSizeEstimator;
23  import org.jboss.netty.util.Timer;
24  
25  /**
26   * This implementation of the {@link AbstractTrafficShapingHandler} is for global
27   * traffic shaping, that is to say a global limitation of the bandwidth, whatever
28   * the number of opened channels.<br><br>
29   *
30   * The general use should be as follow:<br>
31   * <ul>
32   * <li>Create your unique GlobalTrafficShapingHandler like:<br><br>
33   * <tt>GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(timer);</tt><br><br>
34   * timer could be created using <tt>HashedWheelTimer<tt><br>
35   * <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><br>
36   *
37   * <b>Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created
38   * and shared among all channels as the counter must be shared among all channels.</b><br><br>
39   *
40   * Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation)
41   * or the check interval (in millisecond) that represents the delay between two computations of the
42   * bandwidth and so the call back of the doAccounting method (0 means no accounting at all).<br><br>
43   *
44   * A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting,
45   * it is recommended to set a positive value, even if it is high since the precision of the
46   * Traffic Shaping depends on the period where the traffic is computed. The highest the interval,
47   * the less precise the traffic shaping will be. It is suggested as higher value something close
48   * to 5 or 10 minutes.<br>
49   * </li>
50   * <li>Add it in your pipeline, before a recommended {@link ExecutionHandler} (like
51   * {@link OrderedMemoryAwareThreadPoolExecutor} or {@link MemoryAwareThreadPoolExecutor}).<br>
52   * <tt>pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);</tt><br><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   * @author The Netty Project (netty-dev@lists.jboss.org)
61   * @author Frederic Bregier
62   * @version $Rev: 1225 $, $Date: 2012-05-20 10:48:53 +0200 (dim., 20 mai 2012) $
63   */
64  @Sharable
65  public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler {
66      /**
67       * Create the global TrafficCounter
68       */
69      void createGlobalTrafficCounter() {
70          TrafficCounter tc;
71          if (timer != null) {
72              tc = new TrafficCounter(this, timer, "GlobalTC",
73                      checkInterval);
74              setTrafficCounter(tc);
75              tc.start();
76          }
77      }
78  
79      /**
80       * @param timer
81       * @param writeLimit
82       * @param readLimit
83       * @param checkInterval
84       */
85      public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
86              long readLimit, long checkInterval) {
87          super(timer, writeLimit, readLimit, checkInterval);
88          createGlobalTrafficCounter();
89      }
90  
91      /**
92       * @param timer
93       * @param writeLimit
94       * @param readLimit
95       */
96      public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
97              long readLimit) {
98          super(timer, writeLimit, readLimit);
99          createGlobalTrafficCounter();
100     }
101 
102     /**
103      * @param timer
104      * @param checkInterval
105      */
106     public GlobalTrafficShapingHandler(Timer timer, long checkInterval) {
107         super(timer, checkInterval);
108         createGlobalTrafficCounter();
109     }
110 
111     /**
112      * @param timer
113      */
114     public GlobalTrafficShapingHandler(Timer timer) {
115         super(timer);
116         createGlobalTrafficCounter();
117     }
118 
119     /**
120      * @param objectSizeEstimator
121      * @param timer
122      * @param writeLimit
123      * @param readLimit
124      * @param checkInterval
125      */
126     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
127             Timer timer, long writeLimit, long readLimit,
128             long checkInterval) {
129         super(objectSizeEstimator, timer, writeLimit, readLimit,
130                 checkInterval);
131         createGlobalTrafficCounter();
132     }
133 
134     /**
135      * @param objectSizeEstimator
136      * @param timer
137      * @param writeLimit
138      * @param readLimit
139      */
140     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
141             Timer timer, long writeLimit, long readLimit) {
142         super(objectSizeEstimator, timer, writeLimit, readLimit);
143         createGlobalTrafficCounter();
144     }
145 
146     /**
147      * @param objectSizeEstimator
148      * @param timer
149      * @param checkInterval
150      */
151     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
152             Timer timer, long checkInterval) {
153         super(objectSizeEstimator, timer, checkInterval);
154         createGlobalTrafficCounter();
155     }
156 
157     /**
158      * @param objectSizeEstimator
159      * @param timer
160      */
161     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
162             Timer timer) {
163         super(objectSizeEstimator, timer);
164         createGlobalTrafficCounter();
165     }
166 
167 }