-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rate limits using Redis and filtering by two rules using user ID and IP.
- Loading branch information
Showing
51 changed files
with
1,224 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
RELEASE_VERSION=0.1.25 | ||
RELEASE_VERSION=1.2.2-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
return [ | ||
/** | ||
* Can be a host, or the path to a unix domain socket. | ||
* Starting from version 5.0.0 it is possible to specify schema. | ||
*/ | ||
'connection' => env('REDIS_CONNECTION', 'localhost:6379'), | ||
|
||
/** | ||
* Value in seconds (default is 0 meaning it will use default_socket_timeout) | ||
*/ | ||
'timeout' => 2.0, | ||
|
||
/** | ||
* Value in milliseconds | ||
*/ | ||
'retry_interval' => 2, | ||
|
||
/** | ||
* Value in seconds (default is 0 meaning it will use default_socket_timeout) | ||
*/ | ||
'retry_timeout' => 2.0, | ||
|
||
/** | ||
* Prepend to any key on a connection level | ||
*/ | ||
'prefix' => 'CT:', | ||
|
||
/** | ||
* The number of retries, meaning if you set this option to n, there will be a maximum n+1 attempts overall. | ||
*/ | ||
'max_retries' => 5, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Bootloader; | ||
|
||
use App\Config\RedisConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Redis; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Core\Container; | ||
|
||
class RedisBootloader extends Bootloader | ||
{ | ||
public function __construct(private RedisConfig $config, private LoggerInterface $logger) | ||
{ | ||
} | ||
|
||
public function boot(Container $container): void | ||
{ | ||
$container->bindSingleton(Redis::class, fn (): Redis => $this->resolve()); | ||
} | ||
|
||
protected function resolve(): Redis | ||
{ | ||
$redis = new Redis(); | ||
|
||
if ($this->config->getHost() === '') { | ||
return $redis; | ||
} | ||
|
||
$uri = "{$this->config->getHost()}:{$this->config->getPort()}"; | ||
|
||
$this->logger->info("Resolving a connection to a Redis instance [{$uri}]"); | ||
|
||
$status = $redis->pconnect( | ||
$this->config->getHost(), | ||
$this->config->getPort(), | ||
$this->config->getTimeout(), | ||
null, | ||
$this->config->getRetryInterval(), | ||
$this->config->getRetryTimeout(), | ||
); | ||
|
||
if (! $status) { | ||
$this->logger->emergency("Connection to a Redis instance failed [{$uri}]: {$redis->getLastError()}"); | ||
|
||
throw new \RedisException("Unable to connect to Redis"); | ||
} | ||
|
||
if (! $redis->ping()) { | ||
$this->logger->emergency("PING to a Redis instance is not successful [{$uri}]: {$redis->getLastError()}"); | ||
|
||
throw new \RedisException("Unable to connect to Redis"); | ||
} | ||
|
||
$redis->setOption(Redis::OPT_PREFIX, $this->config->getPrefix()); | ||
$redis->setOption(Redis::OPT_MAX_RETRIES, $this->config->getMaxRetries()); | ||
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); | ||
|
||
$this->logger->info("Connection to a Redis instance has been established [{$uri}]"); | ||
|
||
return $redis; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.