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.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
55   * by calling:<br>
56   * <tt>myHandler.releaseExternalResources();</tt><br>
57   * </li>
58   * </ul><br>
59   */
60  @Sharable
61  public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler {
62      /**
63       * Create the global TrafficCounter
64       */
65      void createGlobalTrafficCounter() {
66          TrafficCounter tc;
67          if (timer != null) {
68              tc = new TrafficCounter(this, timer, "GlobalTC",
69                      checkInterval);
70              setTrafficCounter(tc);
71              tc.start();
72          }
73      }
74  
75      public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
76              long readLimit, long checkInterval) {
77          super(timer, writeLimit, readLimit, checkInterval);
78          createGlobalTrafficCounter();
79      }
80  
81      public GlobalTrafficShapingHandler(Timer timer, long writeLimit,
82              long readLimit) {
83          super(timer, writeLimit, readLimit);
84          createGlobalTrafficCounter();
85      }
86  
87      public GlobalTrafficShapingHandler(Timer timer, long checkInterval) {
88          super(timer, checkInterval);
89          createGlobalTrafficCounter();
90      }
91  
92      public GlobalTrafficShapingHandler(Timer timer) {
93          super(timer);
94          createGlobalTrafficCounter();
95      }
96  
97      public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
98              Timer timer, long writeLimit, long readLimit,
99              long checkInterval) {
100         super(objectSizeEstimator, timer, writeLimit, readLimit,
101                 checkInterval);
102         createGlobalTrafficCounter();
103     }
104 
105     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
106             Timer timer, long writeLimit, long readLimit) {
107         super(objectSizeEstimator, timer, writeLimit, readLimit);
108         createGlobalTrafficCounter();
109     }
110 
111     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
112             Timer timer, long checkInterval) {
113         super(objectSizeEstimator, timer, checkInterval);
114         createGlobalTrafficCounter();
115     }
116 
117     public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
118             Timer timer) {
119         super(objectSizeEstimator, timer);
120         createGlobalTrafficCounter();
121     }
122 
123 }