Statistics
| Branch: | Revision:

ml / util / rateControl.c @ 08a4230a

History | View | Annotate | Download (1.29 KB)

1
/*
2
 *          Policy Management
3
 *
4
 *
5
 * This software was created by arpad.bakay@netvisor.hu
6
 *
7
 *     THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
8
 */
9

    
10
#include <sys/time.h>
11

    
12
#include "udpSocket.h"
13

    
14
static long bucket_size = 0;
15
static int drain_rate = 0;
16

    
17
static long bytes_in_bucket = 0;
18
struct timeval bib_when = { 0, 0};
19

    
20
int outputRateControl(int len) {
21
   struct timeval now;
22
   gettimeofday(&now, NULL);
23
   if(drain_rate <= 0) {
24
      bytes_in_bucket = 0;
25
      bib_when = now;
26
      return OK;
27
   }
28
   else {
29
       int total_drain_secs = bytes_in_bucket / drain_rate + 1; 
30
       if(now.tv_sec - bib_when.tv_sec - 1 < total_drain_secs) {
31
           bytes_in_bucket = 0;
32
       }
33
       else {
34
          long leaked = drain_rate * 1024 * (now.tv_sec - bib_when.tv_sec);
35
          leaked += drain_rate * (now.tv_usec - bib_when.tv_usec) / (1000000 / 1024); 
36
          if(leaked > bytes_in_bucket) bytes_in_bucket = 0;
37
          else bytes_in_bucket -= leaked;
38
       }
39
       bib_when = now;
40
       if(bytes_in_bucket + len <= bucket_size) {
41
              bytes_in_bucket += len;
42
              return OK;
43
       }
44
       else  return THROTTLE;
45
   }
46
}
47

    
48
void setOutputRateParams(int bucketsize, int drainrate) {
49
     bucket_size = bucketsize * 1024;
50
     outputRateControl(0);
51
     drain_rate = drainrate; 
52
}
53

    
54