当前位置:   article > 正文

限速算法基于令牌桶算法

限速算法

限速算法基于令牌桶算法
1)记最大速度单位为S,第一次发送开始时,记当前时间为T,记令牌桶初始化N=0, N最大值为2S,最小发送单位M=S/4,发送间隔I = 200ms
2)发送前记录当前时间为t,如果 S(t-T) + N >= 2S, 令N = 2S, T = t ,发送M数据,休眠I时间
3)发送前记录当前时间为t, 如果 M < S(t-T) + N < 2S,则发送M数据,休眠I时间
4)发送前记录当前时间为t,如果 S(t-T) + N < M, 则使N = N + S(t-T), 休眠I时间
5)如果数据全部发送完毕则结束

#define RATELIMIT_BURST_TIMES_DEFAULT       (2) //2s
#define RATELIMIT_USLEEP_INTERVAL           (200000) //200ms
#define RATELIMIT_MINTOKER_DIV_PARAM        (4) //division parameter

typedef struct _RateLimitInfo 
{
    struct timeval  StartTime;

    uint8_t         RateLimitBurstTimes;
    uint64_t        MaxSendRateBps;

    uint64_t        RateLimitToken;
    uint64_t        MinRateLimitToken;      /* when Token is larger than minToken, then send data of  MinRateLimitToken */
    uint64_t        SleepInterval;
    uint64_t        DataBuffSize;
    uint64_t        DataBuffCount;
    char            DataBuff[0];
}__attribute__((packed)) RateLimitInfo;

uint64_t 
GetRateLimitValuebybps(
    uint64_t MaxSendRatebps,
    uint64_t UnitSize
    )
{
    uint64_t ret = 0;
    uint64_t sendRateHostOrder = be64toh(MaxSendRatebps); //net order convert if needed
    ret = sendRateHostOrder / 8;

    if (ret % UnitSize > UnitSize / 2)
    {
        ret = ret - ret % UnitSize;
    }
    else
    {
        ret = ret - ret % UnitSize + UnitSize;
    }

    if (ret == 0)
    {
        ret = UnitSize;
    }

    return ret;
}

uint64_t 
RateLimitGetMinLimitToken(
    uint64_t MaxSendRateBps, 
    uint64_t UnitSize
	)
{
    uint64_t minRateLimitToken = MaxSendRateBps / RATELIMIT_MINTOKER_DIV_PARAM ;

    minRateLimitToken = minRateLimitToken + UnitSize - minRateLimitToken % UnitSize;

    return minRateLimitToken;
}

int 
RateLimitInit(
    uint64_t MaxSendRateBps, 
    uint64_t UnitSize, 
    RateLimitInfo** RateLimit
	)
{
    *RateLimit = malloc(sizeof(RateLimitInfo) + 1024 * 1024);
    if (*RateLimit == NULL)
    {
        printf("Malloc memory for Ratelimit error!\n");
        return -ENOMEM;
    }

    (*RateLimit)->RateLimitBurstTimes = RATELIMIT_BURST_TIMES_DEFAULT;
    (*RateLimit)->MaxSendRateBps = MaxSendRateBps;
    (*RateLimit)->DataBuffSize = 1024 * 1024; //1M
    (*RateLimit)->DataBuffCount = 0;
    (*RateLimit)->RateLimitToken = 0;
    (*RateLimit)->SleepInterval = RATELIMIT_USLEEP_INTERVAL;
    /* MinRateLimitToken as usual , shoule be smaller or equal than DataBuffSize*/
    (*RateLimit)->MinRateLimitToken = RateLimitGetMinLimitToken(MaxSendRateBps, UnitSize);

    gettimeofday(&((*RateLimit)->StartTime), NULL);
    return 0;
}

void
UpdateRateLimitToken(
    RateLimitInfo* RateLimit
    )
{
    struct timeval currentTime;
    uint64_t diff = 0;
    uint64_t tokenAddSize = 0; 

    if (RateLimit->SleepInterval)
    {   
        usleep(RateLimit->SleepInterval);
    }

    gettimeofday(&currentTime, NULL);
    diff = (currentTime.tv_sec - (RateLimit->StartTime).tv_sec) * 1000 + (currentTime.tv_usec - (RateLimit->StartTime).tv_usec) / 1000;
    tokenAddSize = diff * RateLimit->MaxSendRateBps / 1000;

    RateLimit->RateLimitToken = RateLimit->RateLimitToken + tokenAddSize;
    if (RateLimit->RateLimitToken < RateLimit->MinRateLimitToken)
    {
        RateLimit->SleepInterval = RATELIMIT_USLEEP_INTERVAL;
        goto CommonReturn;
    }
    else
    {
        memcpy(&RateLimit->StartTime, &currentTime, sizeof(currentTime));
    }
CommonReturn:
    return;
}

void RateLimitFree(RateLimitInfo* RateLimit)
{
    free(RateLimit);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122

使用:

int 
SendUnitDataWithRateLimit(
    int Sockfd,
    RateLimitInfo* RateLimit
    )
{
    int ret = 0;
    int64_t sendSize = 0;
    for(;;)
    {
        UpdateRateLimitToken(RateLimit);
        while (RateLimit->RateLimitToken >= RateLimit->MinRateLimitToken && RateLimit->RateLimitToken >= Conn->UnitSize)
        {
            /*Do your send function*/
            sendSize = send_func(SockFd, (char *)RateLimit->DataBuff, RateLimit->RateLimitToken);
			RateLimit->RateLimitToken -= sendSize;
            /*Do your exit logic*/
        }
    }
    
CommonReturn:
    return ret;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/230034
推荐阅读
相关标签
  

闽ICP备14008679号