Browse code

sbc: introduced dynamic rate limiting

also changed the time-base parameter to milliseconds.

Raphael Coeffic authored on 26/07/2013 11:05:28 • Václav Kubart committed on 02/08/2013 12:59:10
Showing 3 changed files
... ...
@@ -3,25 +3,22 @@
3 3
 
4 4
 #define min(a,b) ((a) < (b) ? (a) : (b))
5 5
 
6
-RateLimit::RateLimit(unsigned int rate, unsigned int peak, 
7
-		     unsigned int time_base_seconds)
8
-  : rate(rate),
9
-    peak(peak),
10
-    counter(peak)
6
+DynRateLimit::DynRateLimit(unsigned int time_base_ms)
7
+  : last_update(0), counter(0)
11 8
 {
12 9
   // wall_clock has a resolution of 20ms
13
-  time_base = (1000 * time_base_seconds) / 20;
14
-  last_update = AmAppTimer::instance()->wall_clock;
10
+  time_base = time_base_ms / 20;
15 11
 }
16 12
 
17
-bool RateLimit::limit(unsigned int size)
13
+bool DynRateLimit::limit(unsigned int rate, unsigned int peak, 
14
+			 unsigned int size)
18 15
 {
19 16
   lock();
20 17
 
21 18
   if(AmAppTimer::instance()->wall_clock - last_update 
22 19
      > time_base) {
23 20
 
24
-    update_limit();
21
+    update_limit(rate,peak);
25 22
   }
26 23
 
27 24
   if(counter <= 0) {
... ...
@@ -35,7 +32,7 @@ bool RateLimit::limit(unsigned int size)
35 32
   return false; // do not limit
36 33
 }
37 34
 
38
-void RateLimit::update_limit()
35
+void DynRateLimit::update_limit(int rate, int peak)
39 36
 {
40 37
   counter = min(peak, counter+rate);
41 38
   last_update = AmAppTimer::instance()->wall_clock;
... ...
@@ -5,38 +5,57 @@
5 5
 #include "atomic_types.h"
6 6
 #include <sys/types.h>
7 7
 
8
-class RateLimit
8
+class DynRateLimit
9 9
   : protected AmMutex
10 10
 {
11 11
   u_int32_t last_update;
12
-  int rate;
13
-  int peak;
14 12
   int counter;
15 13
 
16 14
   unsigned int time_base;
17 15
 
18
-  void update_limit();
16
+  void update_limit(int rate, int peak);
19 17
 
20 18
 public:
21
-  // rate: units/time_base
22
-  // peak: units/time_base
23
-  // time_base: seconds
24
-  RateLimit(unsigned int rate, unsigned int peak, 
25
-	    unsigned int time_base);
19
+  // time_base_ms: milliseconds
20
+  DynRateLimit(unsigned int time_base_ms);
26 21
 
27
-  virtual ~RateLimit() {}
22
+  virtual ~DynRateLimit() {}
28 23
 
29
-  int getRate() const { return rate; }
30
-  int getPeak() const { return peak; }
31 24
   unsigned int getTimeBase() const { return time_base; }
32 25
 
33 26
   /**
27
+   * rate: units/time_base
28
+   * peak: units/time_base
34 29
    * returns true if 'size' should be dropped
35 30
    */
36
-  bool limit(unsigned int size);
31
+  bool limit(unsigned int rate, unsigned int peak, unsigned int size);
37 32
 
38 33
   /** Get last update timestamp (wheeltimer::wallclock ticks) */
39 34
   u_int32_t getLastUpdate() { return last_update; }
40 35
 };
41 36
 
37
+class RateLimit
38
+  : protected DynRateLimit
39
+{
40
+  int rate;
41
+  int peak;
42
+
43
+public:
44
+  // time_base_ms: milliseconds
45
+  RateLimit(unsigned int rate, unsigned int peak, unsigned int time_base_ms)
46
+    : DynRateLimit(time_base_ms), rate(rate), peak(peak)
47
+  {}
48
+
49
+  int getRate() const { return rate; }
50
+  int getPeak() const { return peak; }
51
+  unsigned int getTimeBase() const { return DynRateLimit::getTimeBase(); }
52
+
53
+  /**
54
+   * returns true if 'size' should be dropped
55
+   */
56
+  bool limit(unsigned int size) {
57
+    return DynRateLimit::limit(rate,peak,size);
58
+  }
59
+};
60
+
42 61
 #endif
... ...
@@ -178,7 +178,8 @@ SBCCallLeg::SBCCallLeg(const SBCCallProfile& call_profile, AmSipDialog* p_dlg,
178 178
      call_profile.rtprelay_bw_limit_peak > 0) {
179 179
 
180 180
     RateLimit* limit = new RateLimit(call_profile.rtprelay_bw_limit_rate,
181
-				     call_profile.rtprelay_bw_limit_peak, 1);
181
+				     call_profile.rtprelay_bw_limit_peak,
182
+				     1000);
182 183
     rtp_relay_rate_limit.reset(limit);
183 184
   }
184 185
 }