RateLimiterMiddleware.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\http\middleware\api;
  14. use app\Request;
  15. use think\exception\ValidateException;
  16. use think\facade\Cache;
  17. /**
  18. * 限流中间件
  19. * Class RateLimiterMiddleware
  20. * @author 等风来
  21. * @email 136327134@qq.com
  22. * @date 2023/1/4
  23. * @package app\http\middleware\api
  24. */
  25. class RateLimiterMiddleware
  26. {
  27. /**
  28. * @param Request $request
  29. * @param \Closure $next
  30. * @return mixed
  31. * @author 等风来
  32. * @email 136327134@qq.com
  33. * @date 2023/1/4
  34. */
  35. public function handle(Request $request, \Closure $next)
  36. {
  37. $option = $request->rule()->getOption();
  38. //是否开启限流
  39. if (isset($option['rateLimiter']) && $option['rateLimiter']) {
  40. $rule = trim(strtolower($request->rule()->getRule()));
  41. $uid = 0;
  42. if ($request->hasMacro('uid') && !empty($option['isUser'])) {
  43. $uid = $request->uid();
  44. }
  45. $key = md5($rule . $uid);
  46. $this->createLimit($key, $option['limitNum'], $option['expire']);
  47. }
  48. return $next($request);
  49. }
  50. /**
  51. * @param string $key
  52. * @param int $initNum
  53. * @param int $expire
  54. * @return bool
  55. * @author 等风来
  56. * @email 136327134@qq.com
  57. * @date 2023/1/4
  58. */
  59. protected function createLimit(string $key, int $initNum, int $expire)
  60. {
  61. $nowTime = time();
  62. /** @var \Redis $redis */
  63. $redis = Cache::store('redis')->handler();
  64. $script = <<<LUA
  65. local key = KEYS[1]
  66. local initNum = tonumber(ARGV[1])
  67. local expire = tonumber(ARGV[2])
  68. local nowTime = tonumber(ARGV[3])
  69. local limitVal = redis.call('get', key)
  70. if limitVal then
  71. limitVal = cjson.decode(limitVal)
  72. local newNum = math.min(initNum, (limitVal.num - 1) + ((initNum / expire) * (nowTime - limitVal.time)))
  73. if newNum <= 0 then
  74. return 0
  75. else
  76. local redisVal = {num = newNum, time = nowTime}
  77. redis.call('set', key, cjson.encode(redisVal))
  78. redis.call('expire', key, expire)
  79. return 1
  80. end
  81. else
  82. local redisVal = {num = initNum, time = nowTime}
  83. redis.call('set', key, cjson.encode(redisVal))
  84. redis.call('expire', key, expire)
  85. return 1
  86. end
  87. LUA;
  88. $result = $redis->eval($script, [$key, $initNum, $expire, $nowTime], 1);
  89. if (!$result) {
  90. throw new ValidateException('访问频次过多!');
  91. }
  92. return true;
  93. }
  94. }