11<?php
2+
23namespace Imi \RateLimit ;
34
4- use Imi \ Redis \ RedisManager ;
5+ use bandwidthThrottle \ tokenBucket \ BlockingConsumer ;
56use bandwidthThrottle \tokenBucket \Rate ;
6- use Imi \ RateLimit \ Storage \ ImiRedisStorage ;
7+ use bandwidthThrottle \ tokenBucket \ TimeoutException ;
78use bandwidthThrottle \tokenBucket \TokenBucket ;
89use Imi \RateLimit \Exception \RateLimitException ;
9- use bandwidthThrottle \ tokenBucket \ BlockingConsumer ;
10- use bandwidthThrottle \ tokenBucket \ TimeoutException ;
10+ use Imi \ RateLimit \ Storage \ ImiRedisStorage ;
11+ use Imi \ Redis \ RedisManager ;
1112
1213/**
13- * 限流器手动调用类
14+ * 限流器手动调用类.
1415 */
1516abstract class RateLimiter
1617{
1718 /**
1819 * 限流
1920 *
20- * @param string $name 限流器名称
21- * @param int $capacity 总容量
21+ * @param string $name 限流器名称
22+ * @param int $capacity 总容量
2223 * @param callable $callback 触发限流的回调
23- * @param int $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同
24- * @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year
25- * @param integer $deduct 每次扣除数量,默认为1
26- * @param string $poolName 连接池名称,留空取默认 redis 连接池
24+ * @param int $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同
25+ * @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year
26+ * @param int $deduct 每次扣除数量,默认为1
27+ * @param string $poolName 连接池名称,留空取默认 redis 连接池
28+ *
2729 * @return mixed
2830 */
2931 public static function limit ($ name , $ capacity , $ callback = null , $ fill = null , $ unit = 'second ' , $ deduct = 1 , $ poolName = null )
3032 {
31- if (null === $ fill )
33+ if (null === $ fill )
3234 {
3335 $ fill = $ capacity ;
3436 }
3537 $ storage = new ImiRedisStorage ($ name , RedisManager::getInstance ($ poolName ));
36- $ rate = new Rate ($ fill , $ unit );
37- $ bucket = new TokenBucket ($ capacity , $ rate , $ storage );
38+ $ rate = new Rate ($ fill , $ unit );
39+ $ bucket = new TokenBucket ($ capacity , $ rate , $ storage );
3840 $ bucket ->bootstrap ($ capacity );
39- if (!$ bucket ->consume ($ deduct ))
41+ if (!$ bucket ->consume ($ deduct ))
4042 {
41- if ($ callback )
43+ if ($ callback )
4244 {
4345 return $ callback ($ name );
4446 }
@@ -54,34 +56,39 @@ public static function limit($name, $capacity, $callback = null, $fill = null, $
5456 }
5557
5658 /**
57- * 限流,允许超时等待
59+ * 限流,允许超时等待.
5860 *
59- * @param string $name 限流器名称
60- * @param int $capacity 总容量
61- * @param callable $callback 触发限流的回调
61+ * @param string $name 限流器名称
62+ * @param int $capacity 总容量
63+ * @param callable $callback 触发限流的回调
6264 * @param int|null $blockingTimeout 超时时间,单位:秒;为 null 不限制
63- * @param int $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同
64- * @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year
65- * @param integer $deduct 每次扣除数量,默认为1
66- * @param string $poolName 连接池名称,留空取默认 redis 连接池
65+ * @param int $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同
66+ * @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year
67+ * @param int $deduct 每次扣除数量,默认为1
68+ * @param string $poolName 连接池名称,留空取默认 redis 连接池
69+ *
6770 * @return mixed
6871 */
6972 public static function limitBlock ($ name , $ capacity , $ callback = null , $ blockingTimeout = null , $ fill = null , $ unit = 'second ' , $ deduct = 1 , $ poolName = null )
7073 {
71- if (null === $ fill )
74+ if (null === $ fill )
7275 {
7376 $ fill = $ capacity ;
7477 }
7578 $ storage = new ImiRedisStorage ($ name , RedisManager::getInstance ($ poolName ), $ blockingTimeout );
76- $ rate = new Rate ($ fill , $ unit );
77- $ bucket = new TokenBucket ($ capacity , $ rate , $ storage );
79+ $ rate = new Rate ($ fill , $ unit );
80+ $ bucket = new TokenBucket ($ capacity , $ rate , $ storage );
7881 $ bucket ->bootstrap ($ capacity );
7982 $ consumer = new BlockingConsumer ($ bucket , $ blockingTimeout );
80- try {
83+ try
84+ {
8185 $ consumer ->consume ($ deduct );
86+
8287 return true ;
83- } catch (TimeoutException $ ex ) {
84- if ($ callback )
88+ }
89+ catch (TimeoutException $ ex )
90+ {
91+ if ($ callback )
8592 {
8693 return $ callback ($ name );
8794 }
@@ -93,10 +100,11 @@ public static function limitBlock($name, $capacity, $callback = null, $blockingT
93100 }
94101
95102 /**
96- * 默认限流回调
103+ * 默认限流回调.
97104 *
98105 * @param string $name 限流器名称
99- * @return void
106+ *
107+ * @return mixed
100108 */
101109 public static function defaultCallback ($ name )
102110 {
0 commit comments