PHP code example of pudongping / hyperf-throttle-requests
1. Go to this page and download the library: Download pudongping/hyperf-throttle-requests library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pudongping / hyperf-throttle-requests example snippets
/**
*
*
* Created by PhpStorm
* User: Alex
* Date: 2023-06-21 11:36
*/
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests;
#[AutoController(prefix: "throttle-requests")]
#[ThrottleRequests]
class ThrottleRequestsController
{
public function t1()
{
return [
'name' => 'alex'
];
}
public function t2()
{
return [
'name' => 'harry'
];
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests;
#[AutoController(prefix: "throttle-requests")]
#[ThrottleRequests(key: "test-throttle", maxAttempts: 5, decaySeconds: 15, prefix: "TR:")]
class ThrottleRequestsController
{
public function t1()
{
return [
'name' => 'alex'
];
}
public function t2()
{
return [
'name' => 'harry'
];
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests;
#[AutoController(prefix: "throttle-requests")]
class ThrottleRequestsController
{
#[ThrottleRequests(key: "test-throttle", maxAttempts: 5, decaySeconds: 15, prefix: "TR:")]
public function t1()
{
return [
'name' => 'alex'
];
}
#[ThrottleRequests(key: "test-throttle", maxAttempts: 5, decaySeconds: 15, prefix: "TR:")]
public function t2()
{
return [
'name' => 'harry'
];
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
#[AutoController(prefix: "throttle-requests")]
class ThrottleRequestsController
{
public function t1()
{
throttle_requests(rateLimits: "5,15");
return [
'name' => 'alex'
];
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Handler\ThrottleRequestsHandler;
use Hyperf\Context\ApplicationContext;
#[AutoController(prefix: "throttle-requests")]
class ThrottleRequestsController
{
public function t2()
{
ApplicationContext::getContainer()->get(ThrottleRequestsHandler::class)->handle(5, 15);
return [
'name' => 'harry'
];
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Handler\ThrottleRequestsHandler;
use Hyperf\Context\ApplicationContext;
use Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests;
use App\Helper\ThrottleRequestsHelper;
use Hyperf\HttpServer\Contract\RequestInterface;
#[AutoController(prefix: "throttle-requests")]
class ThrottleRequestsController
{
public function __construct(protected RequestInterface $request)
{
}
#[ThrottleRequests(generateKeyCallable: [ThrottleRequestsHelper::class, "generateKeyCallable"])]
public function t1()
{
return [
'name' => 'alex'
];
}
public function t2()
{
ApplicationContext::getContainer()
->get(ThrottleRequestsHandler::class)
->handle(
5,
15,
generateKeyCallable: [$this, 'generateKeyCallable']
);
return [
'name' => 'harry'
];
}
public function generateKeyCallable()
{
return 'alex_' . $this->request->url();
}
}
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Pudongping\HyperfThrottleRequests\Handler\ThrottleRequestsHandler;
use Hyperf\Context\ApplicationContext;
use Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests;
use App\Helper\ThrottleRequestsHelper;
#[AutoController(prefix: "throttle-requests")]
class ThrottleRequestsController
{
#[ThrottleRequests(tooManyAttemptsCallback: [ThrottleRequestsHelper::class, 'tooManyAttemptsCallback'])]
public function t1()
{
return [
'name' => 'alex'
];
}
public function t2()
{
ApplicationContext::getContainer()
->get(ThrottleRequestsHandler::class)
->handle(
5,
15,
tooManyAttemptsCallback: function () {
var_dump('请求过于频繁');
throw new \RuntimeException('请求过于频繁', 429);
}
);
return [
'name' => 'harry'
];
}
}
declare(strict_types=1);
namespace App\Helper;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Context\ApplicationContext;
class ThrottleRequestsHelper
{
public function __construct(protected RequestInterface $request)
{
}
public static function generateKeyCallable()
{
$request = ApplicationContext::getContainer()->get(RequestInterface::class);
return $request->getUri()->getPath();
}
public static function tooManyAttemptsCallback()
{
var_dump('Too Many Attempts.');
throw new \RuntimeException('请求过于频繁', 429);
}
}