令牌桶算法实现API限流

对着背影说爱祢 提交于 2019-12-17 04:15:20

https://www.cnblogs.com/wangxiayun/p/9300556.html

令牌桶算法( Token Bucket )和 Leaky Bucket 效果一样但方向相反的算法,更加容易理解.随着时间流逝,系统会按恒定 1/QPS 时间间隔(如果 QPS=100 ,则间隔是 10ms )往桶里加入 Token(想象和漏洞漏水相反,有个水龙头在不断的加水),如果桶已经满了就不再加了.新请求来临时,会各自拿走一个 Token ,如果没有 Token 可拿了就阻塞或者拒绝服务.

 

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

@Autowired

   private JedisClientService jedisClient;

 

   public boolean acquire(String key, Integer permits, long currMillSecond) {

       try  {

 

           //针对新用户创建令牌桶

           if (!jedisClient.exists(key)) {

               jedisClient.hset(key, "last_mill_second", String.valueOf(currMillSecond));

               jedisClient.hset(key, "curr_permits""0");

               jedisClient.hset(key, "max_permits""50");

               jedisClient.hset(key, "rate""400");

               return true;

           }

           //获取令牌桶信息,上一个令牌时间,当前可用令牌数,最大令牌数,令牌消耗速率

           List<String> limitInfo = jedisClient.hmget(key, "last_mill_second""curr_permits""max_permits""rate");

           long lastMillSecond = Long.parseLong(limitInfo.get(0));

           Integer currPermits = Integer.valueOf(limitInfo.get(1));

           Integer maxPermits = Integer.valueOf(limitInfo.get(2));

           Double rate = Double.valueOf(limitInfo.get(3));

           //向桶里面添加令牌

           Double reversePermitsDouble = ((currMillSecond - lastMillSecond) / 1000) * rate;

 

           Integer reversePermits = reversePermitsDouble.intValue();

           Integer expectCurrPermits = reversePermits + currPermits;

           Integer localCurrPermits = Math.min(expectCurrPermits, maxPermits);

           //添加令牌之后更新时间

           if (reversePermits > 0) {

               jedisClient.hset(key, "last_mill_second", String.valueOf(currMillSecond));

           }

           //判断桶里面剩余的令牌数目

           if (localCurrPermits - permits >= 0) {

               jedisClient.hset(key, "curr_permits", String.valueOf(localCurrPermits - permits));

               return true;

           else {

               jedisClient.hset(key, "curr_permits", String.valueOf(localCurrPermits));

               return false;

           }

       catch (Exception e) {

           e.printStackTrace();

           return false;

       }

   }

  

 

参考文章: https://blog.csdn.net/tianyaleixiaowu/article/details/74942405

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!