Skip to content

Commit bd22de9

Browse files
committed
init
0 parents  commit bd22de9

File tree

10 files changed

+527
-0
lines changed

10 files changed

+527
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/.idea

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 [jiaxincui](https://github.com/jiaxincui) <jenkincei@hotmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "jiaxincui/laravel-throttler",
3+
"version": "0.3.0",
4+
"description": "Multiple policy rate limit for laravel",
5+
"keywords": ["laravel", "throttler", "rate limit"],
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "jiaxincui",
11+
"email": "jenkincei@hotmail.com",
12+
"role": "Developer"
13+
}
14+
],
15+
"support": {
16+
"email": "jenkincei@hotmail.com",
17+
"source":"https://github.com/jiaxincui/laravel-throttler"
18+
},
19+
"minimum-stability": "stable",
20+
"require": {
21+
"illuminate/cache": "~5.0|~6.0|~7.0",
22+
"illuminate/container": "~5.0|~6.0|~7.0",
23+
"illuminate/support": "~5.0|~6.0|~7.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Jiaxincui\\Throttler\\": "src/"
28+
}
29+
},
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"Jiaxincui\\Throttler\\ThrottlerServiceProvider"
34+
]
35+
}
36+
}
37+
}

config/throttler.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| 限速器 key 前缀
8+
|--------------------------------------------------------------------------
9+
|
10+
| 如果有多个应用存在 key 重复的可能你可以设置一个适当的前缀,如果你确定没有重复的可能可忽略。
11+
|
12+
*/
13+
14+
'prefix' => '',
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| 限速器
19+
|--------------------------------------------------------------------------
20+
|
21+
| 如果为空,默认使用 Illuminate\Cache\RateLimiter
22+
| 如果要自定义限速器实现,要确保其实现 Jiaxincui\Throttler\Contracts\Limiter 接口
23+
|
24+
*/
25+
26+
'limiter' => '',
27+
28+
/*
29+
|--------------------------------------------------------------------------
30+
| 限速方案
31+
|--------------------------------------------------------------------------
32+
|
33+
| 在使用中没有明确指定 guard 将使用 default 作为限速方案。
34+
| 每个方案可以设置多条策略,如 default 方案限制每秒钟5次,并且每分钟10次,每小时50次,每24小时100次,可以像下面这样设置:
35+
| 'default' => [
36+
| [5, 1],
37+
| [10, 60],
38+
| [50, 60*60],
39+
| [100, 60*60*24]
40+
| ]
41+
|
42+
| 也可以设置多条方案,如下将设置一个 custom 的方案:
43+
| 'custom' => [
44+
| [5, 1],
45+
| [10, 60]
46+
| ]
47+
|
48+
*/
49+
50+
'guards' => [
51+
'default' => [
52+
[5, 1]
53+
],
54+
55+
'custom' => [
56+
]
57+
]
58+
];

readme.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
## About
2+
3+
Multiple policy rate limit for laravel
4+
5+
适用于 Laravel 的多策略速率限制包
6+
7+
## 应用场景
8+
9+
+ 多策略的Api请求速率控制
10+
11+
+ 多策略的队列速率控制
12+
13+
## 安装
14+
15+
```terminal
16+
composer require jiaxincui/laravel-throttler
17+
```
18+
19+
## 配置文件
20+
21+
此命令将配置文件copy至`config/throttler.php`
22+
23+
```terminal
24+
php artisan vendor:publish --provider="Jiaxincui\Throttler\ThrottlerServiceProvider"
25+
```
26+
27+
+ `prefix` 限速器 key 前缀,如果有多个应用存在 key 重复的可能你可以设置一个适当的前缀,如果你确定没有重复的可能可忽略。
28+
29+
+ `limiter` 使用的限速器,如果为空,默认使用`Iluminate\Cache\RateLimiter`, 如果要自定义限速器实现,要确保其实现 `Jiaxincui\Throttler\Contracts\Limiter` 接口
30+
31+
+ `guard` 限速方案,在使用中没有明确指定 `guard` 将使用 `default` 作为限速方案。
32+
33+
每个方案可以设置多条策略,如 default 方案限制每秒钟5次,并且每分钟10次,每小时50次,每24小时100次,可以像下面这样设置:
34+
35+
```php
36+
[
37+
'guards' => [
38+
'default' => [
39+
[5, 1],
40+
[10, 60],
41+
[50, 60*60],
42+
[100, 60*60*24]
43+
],
44+
45+
// 'custom' => [
46+
// [5, 1],
47+
// [10, 60]
48+
// ]
49+
]
50+
];
51+
52+
```
53+
54+
55+
### 使用
56+
57+
+ 一般情况下你只需要在配置文件设置策略即可。
58+
59+
60+
```php
61+
use Jiaxincui\Throttler\Facades\Throttler;
62+
63+
Throttler::for($request->ip)
64+
->then(function() {
65+
// 通过
66+
var_dump('Hello world!');
67+
}, function($availableIn) {
68+
// 未通过
69+
var_dump($availableIn);
70+
});
71+
72+
// 使用其他限速方案
73+
Throttler::guard('custom')
74+
->for($request->ip)
75+
->then(function() {
76+
// 通过
77+
var_dump('Hello world!');
78+
}, function($availableIn) {
79+
// 未通过
80+
var_dump($availableIn);
81+
});
82+
```
83+
84+
+ 如果你要在运行时设置限制策略,可以像下面这样做:
85+
86+
```php
87+
use Jiaxincui\Throttler\Facades\Throttler;
88+
89+
Throttler::for($user->id)
90+
->tap([5, 1])
91+
->tap([10, 60])
92+
->tap([50, 60*60])
93+
->then(function() {
94+
// 通过
95+
var_dump('Hello world!');
96+
}, function($availableIn) {
97+
// 未通过
98+
var_dump($availableIn);
99+
});
100+
101+
// 或者
102+
$taps = [
103+
[5, 1],
104+
[10, 60],
105+
[50, 60 * 60]
106+
];
107+
108+
Throttler::throttle($user->id)
109+
->tap($taps)
110+
->then(function() {
111+
// 通过
112+
var_dump('Hello world!');
113+
}, function($availableIn) {
114+
// 未通过
115+
var_dump($availableIn);
116+
});
117+
```
118+
119+
+ 你也可以在控制器的构造方法里通过依赖注入使用它
120+
121+
```php
122+
<?php
123+
124+
namespace App\Http\Controllers;
125+
126+
use Jiaxincui\Throttler\Throttler;
127+
128+
class UserController extends Controller
129+
{
130+
protected $throttler;
131+
132+
/**
133+
* Create a new controller instance.
134+
*
135+
* @param Throttler $throttler
136+
*/
137+
public function __construct(Throttler $throttler)
138+
{
139+
$this->throttler = $throttler;
140+
}
141+
public function index(Request $request)
142+
{
143+
$this->throttler->for($request->ip)
144+
->then(function () {
145+
// 通过
146+
}, function ($availableIn) {
147+
// 未通过
148+
} );
149+
}
150+
151+
}
152+
```
153+
154+
## Api
155+
156+
| 方法 | 返回 | 说明 |
157+
| :-- | :-- | :-- |
158+
| `for(string $key)` | `$this` | 传入`$key`创建一个限速器 |
159+
| `guard(string $guradName)` | `$this` | 应用一个限速方案 |
160+
| `tap(array $tapAndTaps)` | `$this` | 添加一个或多个限速策略 |
161+
| `then(callable $callback, callable $failure)`| `null` | `$callback` 成功通过的回调, `$failure`未通过回调,参数`$availableIn`为距离下次可通过的秒数 |
162+
163+
164+
## License
165+
166+
[MIT](https://github.com/jiaxincui/laravel-throttler/blob/master/LICENSE.md) © [JiaxinCui](https://github.com/jiaxincui)
167+

src/Contracts/RateLimiter.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
namespace Jiaxincui\Throttler\Contracts;
5+
6+
7+
interface RateLimiter
8+
{
9+
10+
/**
11+
* Determine if the given key has been "accessed" too many times.
12+
*
13+
* @param string $key
14+
* @param int $maxAttempts
15+
* @return bool
16+
*/
17+
public function tooManyAttempts($key, $maxAttempts);
18+
19+
/**
20+
* Increment the counter for a given key for a given decay time.
21+
*
22+
* @param string $key
23+
* @param int $decaySeconds
24+
* @return int
25+
*/
26+
public function hit($key, $decaySeconds);
27+
28+
/**
29+
* Get the number of seconds until the "key" is accessible again.
30+
*
31+
* @param string $key
32+
* @return int
33+
*/
34+
public function availableIn($key);
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Jiaxincui\Throttler\Exceptions;
4+
5+
use Exception;
6+
7+
class ThrottlerException extends Exception
8+
{
9+
//
10+
}

src/Facades/Throttler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
namespace Jiaxincui\Throttler\Facades;
5+
6+
use Illuminate\Support\Facades\Facade;
7+
8+
class Throttler extends Facade
9+
{
10+
protected static function getFacadeAccessor()
11+
{
12+
return \Jiaxincui\Throttler\Throttler::class;
13+
}
14+
}

0 commit comments

Comments
 (0)