Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for redis cluster #86

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ services:
ports:
- "9307:3306"

adminer:
image: adminer
container_name: abuse-adminer
restart: always
ports:
- 9506:8080
networks:
- abuse

redis:
image: redis:6.0-alpine
container_name: redis
Expand All @@ -30,5 +39,53 @@ services:
- ./src:/code/src
- ./tests:/code/tests

redis-insight:
image: redis/redisinsight:latest
restart: unless-stopped
networks:
- abuse
environment:
- REDIS_HOSTS=redis-cluster-0:6379
ports:
- "8081:5540"

redis-cluster-0:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
- REDIS_CLUSTER_CREATOR=yes
- REDIS_CLUSTER_REPLICAS=0
networks:
- abuse
depends_on:
- redis-cluster-1
- redis-cluster-2
- redis-cluster-3

redis-cluster-1:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse

redis-cluster-2:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse

redis-cluster-3:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse

networks:
abuse:
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="Application Test Suite">
Expand Down
2 changes: 0 additions & 2 deletions src/Abuse/Adapters/TimeLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ abstract protected function count(string $key, string $datetime): int;

abstract protected function hit(string $key, string $datetime): void;



/**
* Check
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Utopia\Abuse\Adapters\Database;
namespace Utopia\Abuse\Adapters\TimeLimit;

use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Database\Database as UtopiaDB;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
Expand All @@ -13,7 +13,7 @@
use Utopia\Database\Validator\Authorization;
use Utopia\Exception;

class TimeLimit extends TimeLimitAdapter
class Database extends TimeLimit
{
public const COLLECTION = 'abuse';

Expand Down Expand Up @@ -106,7 +106,7 @@ public function setup(): void

try {
$this->db->createCollection(
TimeLimit::COLLECTION,
self::COLLECTION,
$attributes,
$indexes
);
Expand Down Expand Up @@ -138,7 +138,7 @@ protected function count(string $key, string $datetime): int

/** @var array<Document> $result */
$result = Authorization::skip(function () use ($key, $datetime) {
return $this->db->find(TimeLimit::COLLECTION, [
return $this->db->find(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand Down Expand Up @@ -170,7 +170,7 @@ protected function hit(string $key, string $datetime): void
}

Authorization::skip(function () use ($datetime, $key) {
$data = $this->db->findOne(TimeLimit::COLLECTION, [
$data = $this->db->findOne(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand All @@ -181,14 +181,14 @@ protected function hit(string $key, string $datetime): void
'key' => $key,
'time' => $datetime,
'count' => 1,
'$collection' => TimeLimit::COLLECTION,
'$collection' => self::COLLECTION,
];

try {
$this->db->createDocument(TimeLimit::COLLECTION, new Document($data));
$this->db->createDocument(self::COLLECTION, new Document($data));
} catch (Duplicate $e) {
// Duplicate in case of race condition
$data = $this->db->findOne(TimeLimit::COLLECTION, [
$data = $this->db->findOne(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand All @@ -198,14 +198,14 @@ protected function hit(string $key, string $datetime): void
if (\is_numeric($count)) {
$this->count = intval($count);
}
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(self::COLLECTION, $data->getId(), 'count');
} else {
throw new \Exception('Document Not Found');
}
}
} else {
/** @var Document $data */
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(self::COLLECTION, $data->getId(), 'count');
}
});

Expand Down Expand Up @@ -237,7 +237,7 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
$queries[] = Query::limit($limit);
}

return $this->db->find(TimeLimit::COLLECTION, $queries);
return $this->db->find(self::COLLECTION, $queries);
});

return $results;
Expand All @@ -255,12 +255,12 @@ public function cleanup(string $datetime): bool
{
Authorization::skip(function () use ($datetime) {
do {
$documents = $this->db->find(TimeLimit::COLLECTION, [
$documents = $this->db->find(self::COLLECTION, [
Query::lessThan('time', $datetime),
]);

foreach ($documents as $document) {
$this->db->deleteDocument(TimeLimit::COLLECTION, $document->getId());
$this->db->deleteDocument(self::COLLECTION, $document->getId());
}
} while (! empty($documents));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php

namespace Utopia\Abuse\Adapters\Redis;
namespace Utopia\Abuse\Adapters\TimeLimit;

use Redis as Client;
use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;
use Utopia\Abuse\Adapters\TimeLimit;

class TimeLimit extends TimeLimitAdapter
class Redis extends TimeLimit
{
public const NAMESPACE = 'abuse';

/**
* @var Client
* @var \Redis
*/
protected Client $redis;
protected \Redis $redis;

public function __construct(string $key, int $limit, int $seconds, Client $redis)
public function __construct(string $key, int $limit, int $seconds, \Redis $redis)
{
$this->redis = $redis;
$this->key = $key;
Expand Down
Loading
Loading