Sonic Wu

while(!(succeed = try()));

不使用任何数据结构的速率限制算法

没有使用任何数据结构和定时器,最简洁的速率限制算法:Token Bucket(令牌桶)

RateLimiter.py
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
import time

class RateLimiter:

    def __init__(self, max_rate=20.0, per_seconds=10.0):
        self.allowance = max_rate
        self.max_rate = max_rate
        self.per_seconds = per_seconds
        self.last_checked = int(time.time())

    def acquire(self):
        current = int(time.time())
        elapsed = current - self.last_checked
        self.last_checked = current

        self.allowance += elapsed * (self.max_rate / self.per_seconds)

        # throttle
        if self.allowance > self.max_rate:
            self.allowance = self.max_rate

        if self.allowance < 1.0:
            return False

        self.allowance -= 1.0
        return True

初始给予max_rate个令牌,每次请求消耗 1 个,每秒钟恢复max_rate / per_seconds个,最多恢复到max_rate个令牌。

效果:每per_seconds秒最多允许max_rate次请求。

Comments