限流類(Throttler)提供了一種非常簡(jiǎn)單的方法,可以將用戶要執(zhí)行的活動(dòng)限制為在設(shè)定的時(shí)間段內(nèi)只能進(jìn)行一定次數(shù)的嘗試。 這最常用于對(duì) API 進(jìn)行速率限制,或限制用戶針對(duì)表單進(jìn)行的嘗試次數(shù),以幫助防止暴力攻擊。 該類可用于你根據(jù)設(shè)置的時(shí)間來(lái)進(jìn)行限制的操作。
插件地址: http://m.wtbis.cn/plugin/36
限流類
緩存依據(jù)的是Support\Cache的 instance()
, 其他類只要是實(shí)現(xiàn) get($key, $default = null)
, set($key, $value, $ttl = null)
, delete($key)
funtion就行.
項(xiàng)目地址:https://github.com/nsp-team/webman-throttler
composer require nsp-team/webman-throttler
默認(rèn) 開(kāi)啟全局中間件限流
return [ '' => [ \NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class, ] ];
你也可以啟用路由中間件,控制接口請(qǐng)求速率限制
例如:Route::group('/sys/user', static function () { Route::post('/test', [User::class, 'test']); })->middleware([ \NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class ]);
想要限制速度,可以提升 seconds 值,它表示 “桶”完全重新裝滿所需的時(shí)間,capacity 表示桶的容量; 或者減少capacity的值,這樣同樣可以減少訪問(wèn)量
報(bào)錯(cuò)
"Entry \"NspTeam\WebmanThrottler\Throttle\Throttler\" cannot be resolved: Parameter $cache of __construct() has no value defined or guessable\nFull definition:\nObject (\n class = NspTeam\WebmanThrottler\Throttle\Throttler\n lazy = false\n __construct(\n $cache = #UNDEFINED#\n )\n)
最近工作太忙,在寫java,非常不好意思,這個(gè)插件寫的比較早,可能跟框架有不適配了。給大家造成困擾了,源碼很少,原理一看就明白,開(kāi)發(fā)者可以自行改動(dòng)適配,歡迎 PR