优化定时器的防刷机制;

This commit is contained in:
augushong
2022-02-28 22:20:10 +08:00
parent 0f0a1a72af
commit 0e17a2d9a5
3 changed files with 64 additions and 9 deletions

View File

@@ -28,11 +28,19 @@ class Timer extends Command
// 指令输出
$output->writeln('start timer');
$site_domain = sysconfig('site', 'site_domain');
if (empty($site_domain)) {
$output->writeln('请前往后台设置站点域名site_domain配置项');
return;
}
$client = new Client([
'base_uri' => sysconfig('site', 'site_domain'),
'base_uri' => $site_domain,
'verify' => false,
]);
while (true) {
try {
@@ -48,6 +56,10 @@ class Timer extends Command
$name = $config_item['name'];
if ($name == 'http_demo' && !env('adminsystem.is_demo', false)) {
continue;
}
$cache_key = 'timer_' . $name;
$cache_tag = 'system_timer';
@@ -74,10 +86,10 @@ class Timer extends Command
}
}
if (empty($list_promises)){
if (empty($list_promises)) {
$output->writeln(date('Y-m-d H:i:s') . ' no request');
}else{
} else {
$results = Utils::unwrap($list_promises);
$output->writeln(date('Y-m-d H:i:s') . ': request all finished');
}
@@ -106,8 +118,8 @@ class Timer extends Command
$data = array_merge($default, $config);
if ($data['frequency'] < 1) {
$data['frequency'] = 1;
if ($data['frequency'] < 0) {
$data['frequency'] = 0;
}
return $data;

View File

@@ -0,0 +1,35 @@
<?php
namespace app\common\controller;
use think\facade\Cache;
class TimerController extends ToolsController
{
protected $frequency = null;
public function initialize()
{
if (is_integer($this->frequency)) {
$this->protectVisit($this->frequency);
}
}
protected function protectVisit(int $frequency)
{
$cache_tag = 'timer_protect';
$cache_key = 'timer_protect_' . $this->request->url();
$last_exec_time = Cache::get($cache_key, 0);
if ($last_exec_time >= time() - $frequency) {
return $this->error('请不要频繁请求');
}
Cache::tag($cache_tag)->set($cache_key, time());
}
}

View File

@@ -4,13 +4,21 @@ declare(strict_types=1);
namespace app\tools\controller\timer;
use app\common\controller\ToolsController;
use app\common\controller\TimerController;
use think\facade\Console;
class ResetPassword extends ToolsController
class ResetPassword extends TimerController
{
protected $frequency = 600;
public function do()
{
if (!env('adminsystem.is_demo', false)) {
return $this->error('本功能只有在演示环境下在能使用', '', '/');
}
$output = Console::call('admin:resetPassword', [
'--password=123456'
]);