From 4188d65d9dd9f88ad13bd6bb21832df90cc0b810 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 12:18:40 +0200 Subject: [PATCH 1/9] remove a query --- src/Abuse/Adapters/TimeLimit.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index a0beaa1..fee3fd4 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -301,8 +301,25 @@ public function check(): bool } $key = $this->parseKey(); + $time = $this->time; - if ($this->limit > $this->count($key, $this->time)) { + /** + * @var $result Document + */ + $result = Authorization::skip(function () use ($key, $time) { + return $this->db->findOne(TimeLimit::COLLECTION, [ + Query::equal('key', [$key]), + Query::equal('time', [$time]), + ]); + }); + + $count = 0; + if(!empty($result)){ + $count = (int)$result->getAttribute('count', 0); + var_dump($count); + } + + if ($this->limit > $count) { $this->hit($key, $this->time); return false; } From 78f78a6941a2c55585f53576e8b21da20b7fa5fa Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 12:48:05 +0200 Subject: [PATCH 2/9] remove a query --- docker-compose.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 034d2b7..f913ed2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,10 @@ services: - abuse depends_on: - mysql + volumes: + - ./phpunit.xml:/code/phpunit.xml + - ./src:/code/src + - ./tests:/code/tests networks: abuse: \ No newline at end of file From 9be7c69f18f9f74728650398532fc3364f1ed14b Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 14:33:29 +0200 Subject: [PATCH 3/9] remove a query --- src/Abuse/Adapters/TimeLimit.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index fee3fd4..d2ff260 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -208,22 +208,14 @@ protected function count(string $key, string $datetime): int /** * @param string $key * @param string $datetime - * + * @param Document|false $data * @return void - * @throws AuthorizationException|Structure|\Exception + * @throws AuthorizationException + * @throws Structure */ - protected function hit(string $key, string $datetime): void + protected function hit(string $key, string $datetime, Document|false $data): void { - if (0 == $this->limit) { // No limit no point for counting - return; - } - - Authorization::skip(function () use ($datetime, $key) { - $data = $this->db->findOne(TimeLimit::COLLECTION, [ - Query::equal('key', [$key]), - Query::equal('time', [$datetime]), - ]); - + Authorization::skip(function () use ($datetime, $key, $data) { if ($data === false) { $data = [ '$permissions' => [], @@ -304,9 +296,9 @@ public function check(): bool $time = $this->time; /** - * @var $result Document + * @var $data Document */ - $result = Authorization::skip(function () use ($key, $time) { + $data = Authorization::skip(function () use ($key, $time) { return $this->db->findOne(TimeLimit::COLLECTION, [ Query::equal('key', [$key]), Query::equal('time', [$time]), @@ -314,13 +306,12 @@ public function check(): bool }); $count = 0; - if(!empty($result)){ - $count = (int)$result->getAttribute('count', 0); - var_dump($count); + if(!empty($data)){ + $count = (int)$data->getAttribute('count', 0); } if ($this->limit > $count) { - $this->hit($key, $this->time); + $this->hit($key, $this->time, $data); return false; } From 03bfbf8e7ae88b9ca11c88c7809199fa49cddd99 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 17:07:29 +0200 Subject: [PATCH 4/9] remove a query --- src/Abuse/Adapters/TimeLimit.php | 97 ++++++++++++++++---------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index d2ff260..03db4e0 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -39,9 +39,9 @@ class TimeLimit implements Adapter protected int $limit = 0; /** - * @var int|null + * @var Document|null */ - protected ?int $count = null; + protected ?Document $document = null; /** * @var array @@ -64,7 +64,6 @@ public function __construct(string $key, int $limit, int $seconds, Database $db) } /** - * @throws Limit * @throws Duplicate * @throws Exception|\Exception */ @@ -182,56 +181,48 @@ protected function count(string $key, string $datetime): int if (0 == $this->limit) { // No limit no point for counting return 0; } - - if (!\is_null($this->count)) { // Get fetched result - return $this->count; + if (!\is_null($this->document)) { // Get fetched result + return (int)$this->document['count']; } - $result = Authorization::skip(function () use ($key, $datetime) { - return $this->db->find(TimeLimit::COLLECTION, [ - Query::equal('key', [$key]), - Query::equal('time', [$datetime]), - ]); - }); + $this->document = $this->getDocument($key, $datetime); - if (\count($result) === 1) { - $result = $result[0]->getAttribute('count', 0); - } else { - $result = 0; + if (\is_null($this->document)) { + return 0; } - $this->count = (int)$result; - - return $this->count; + return $this->document->getAttribute('count', 0); } /** * @param string $key * @param string $datetime - * @param Document|false $data + * * @return void - * @throws AuthorizationException - * @throws Structure + * @throws AuthorizationException|Structure|\Exception */ - protected function hit(string $key, string $datetime, Document|false $data): void + protected function hit(string $key, string $datetime): void { + $data = $this->getDocument($key, $datetime); + Authorization::skip(function () use ($datetime, $key, $data) { - if ($data === false) { - $data = [ + if (empty($data)) { + $document = new Document([ '$permissions' => [], 'key' => $key, 'time' => $datetime, 'count' => 1, '$collection' => TimeLimit::COLLECTION, - ]; - $this->db->createDocument(TimeLimit::COLLECTION, new Document($data)); + ]); + + $this->db->createDocument(TimeLimit::COLLECTION, $document); + $this->document = $document; + } else { - $data->setAttribute('count', $data->getAttribute('count', 0) + 1); - $this->db->updateDocument(TimeLimit::COLLECTION, $data->getId(), $data); + $this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(),'count'); + $this->document['count']++; } }); - - $this->count++; } /** @@ -293,25 +284,9 @@ public function check(): bool } $key = $this->parseKey(); - $time = $this->time; - - /** - * @var $data Document - */ - $data = Authorization::skip(function () use ($key, $time) { - return $this->db->findOne(TimeLimit::COLLECTION, [ - Query::equal('key', [$key]), - Query::equal('time', [$time]), - ]); - }); - $count = 0; - if(!empty($data)){ - $count = (int)$data->getAttribute('count', 0); - } - - if ($this->limit > $count) { - $this->hit($key, $this->time, $data); + if ($this->limit > $this->count($key, $this->time)) { + $this->hit($key, $this->time); return false; } @@ -356,4 +331,28 @@ public function time(): string { return $this->time; } + + /** + * @param $key + * @param $datetime + * @return Document|null + * @throws \Exception + */ + protected function getDocument($key, $datetime): ?Document + { + if(!empty($this->document)){ + return $this->document; + } + + $result = Authorization::skip(function () use ($key, $datetime) { + return $this->db->findOne(TimeLimit::COLLECTION, [ + Query::equal('key', [$key]), + Query::equal('time', [$datetime]), + ]); + }); + + $this->document = $result === false ? null :$result; + return $this->document; + } + } From 6c08ba0fd85da2e7f88ec474b7e0a7d5b34b40f7 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 17:11:09 +0200 Subject: [PATCH 5/9] remove extra query --- composer.json | 2 +- composer.lock | 41 +++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index 3d3fea1..802b8ee 100755 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": ">=8.0", "ext-pdo": "*", "ext-curl": "*", - "utopia-php/database": "0.28.*" + "utopia-php/database": "dev-increment as 0.28.99" }, "require-dev": { "phpunit/phpunit": "^9.4", diff --git a/composer.lock b/composer.lock index 4991d5a..b90b5ed 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a62cb0b4e78a919c5e3ede9f1d0eb8f1", + "content-hash": "7931149eb908213613f6e4ddff7f7efb", "packages": [ { "name": "utopia-php/cache", @@ -57,26 +57,26 @@ }, { "name": "utopia-php/database", - "version": "0.28.0", + "version": "dev-increment", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "ef6506af1c09c22f5dc1e7859159d323f7fafa94" + "reference": "051078fa6ec1bdbc3d35c170096656180976cb04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/ef6506af1c09c22f5dc1e7859159d323f7fafa94", - "reference": "ef6506af1c09c22f5dc1e7859159d323f7fafa94", + "url": "https://api.github.com/repos/utopia-php/database/zipball/051078fa6ec1bdbc3d35c170096656180976cb04", + "reference": "051078fa6ec1bdbc3d35c170096656180976cb04", "shasum": "" }, "require": { + "ext-pdo": "*", "php": ">=8.0", "utopia-php/cache": "0.8.*", "utopia-php/framework": "0.*.*" }, "require-dev": { "ext-mongodb": "*", - "ext-pdo": "*", "ext-redis": "*", "fakerphp/faker": "^1.14", "mongodb/mongodb": "1.8.0", @@ -105,22 +105,22 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.28.0" + "source": "https://github.com/utopia-php/database/tree/increment" }, - "time": "2022-10-31T09:58:46+00:00" + "time": "2022-11-08T13:56:09+00:00" }, { "name": "utopia-php/framework", - "version": "0.23.4", + "version": "0.25.0", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "97f64aa1732af92b967c3576f16967dc762ad47b" + "reference": "c524f681254255c8204fbf7919c53bf3b4982636" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/97f64aa1732af92b967c3576f16967dc762ad47b", - "reference": "97f64aa1732af92b967c3576f16967dc762ad47b", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/c524f681254255c8204fbf7919c53bf3b4982636", + "reference": "c524f681254255c8204fbf7919c53bf3b4982636", "shasum": "" }, "require": { @@ -148,9 +148,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.23.4" + "source": "https://github.com/utopia-php/framework/tree/0.25.0" }, - "time": "2022-10-31T11:57:14+00:00" + "time": "2022-11-02T09:49:57+00:00" } ], "packages-dev": [ @@ -3865,9 +3865,18 @@ "time": "2015-12-17T08:42:14+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/database", + "version": "dev-increment", + "alias": "0.28.99", + "alias_normalized": "0.28.99.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/database": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { From 9a0269a64c87ef92985619af53a292d00433642d Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 17:16:57 +0200 Subject: [PATCH 6/9] remove extra query --- src/Abuse/Adapters/TimeLimit.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index 03db4e0..aa17710 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -181,12 +181,12 @@ protected function count(string $key, string $datetime): int if (0 == $this->limit) { // No limit no point for counting return 0; } + if (!\is_null($this->document)) { // Get fetched result return (int)$this->document['count']; } $this->document = $this->getDocument($key, $datetime); - if (\is_null($this->document)) { return 0; } @@ -333,6 +333,7 @@ public function time(): string } /** + * Return the Unique document from database * @param $key * @param $datetime * @return Document|null From 451c81d3ff3896dc51b318e620ad85c785f971b9 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 9 Nov 2022 17:26:35 +0200 Subject: [PATCH 7/9] remove extra query --- src/Abuse/Adapters/TimeLimit.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index aa17710..406468c 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -183,7 +183,7 @@ protected function count(string $key, string $datetime): int } if (!\is_null($this->document)) { // Get fetched result - return (int)$this->document['count']; + return $this->document['count']; } $this->document = $this->getDocument($key, $datetime); @@ -333,7 +333,7 @@ public function time(): string } /** - * Return the Unique document from database + * Return the unique document from database * @param $key * @param $datetime * @return Document|null From 9861dd98bc2bef1e7f3e5f2ed6b805da2910f935 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 20 Nov 2022 18:09:59 +0200 Subject: [PATCH 8/9] try catch --- src/Abuse/Adapters/TimeLimit.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index 406468c..868098a 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -8,7 +8,6 @@ use Utopia\Database\Document; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Duplicate; -use Utopia\Database\Exception\Limit; use Utopia\Database\Exception\Structure; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; @@ -204,8 +203,9 @@ protected function count(string $key, string $datetime): int protected function hit(string $key, string $datetime): void { $data = $this->getDocument($key, $datetime); - + $data = null; Authorization::skip(function () use ($datetime, $key, $data) { + if (empty($data)) { $document = new Document([ '$permissions' => [], @@ -215,8 +215,15 @@ protected function hit(string $key, string $datetime): void '$collection' => TimeLimit::COLLECTION, ]); - $this->db->createDocument(TimeLimit::COLLECTION, $document); - $this->document = $document; + try { + $this->db->createDocument(TimeLimit::COLLECTION, $document); + $this->document = $document; + } catch (Duplicate $e){ + // In case of race Condition + var_dump($e->getCode()); + var_dump($e->getMessage()); + die; + } } else { $this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(),'count'); From 726019ed88ead60ffb39a56ddc5aa63423becd7f Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 20 Nov 2022 18:15:56 +0200 Subject: [PATCH 9/9] revert for 37 pr --- src/Abuse/Adapters/TimeLimit.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index 868098a..c5d1059 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -215,15 +215,8 @@ protected function hit(string $key, string $datetime): void '$collection' => TimeLimit::COLLECTION, ]); - try { - $this->db->createDocument(TimeLimit::COLLECTION, $document); - $this->document = $document; - } catch (Duplicate $e){ - // In case of race Condition - var_dump($e->getCode()); - var_dump($e->getMessage()); - die; - } + $this->db->createDocument(TimeLimit::COLLECTION, $document); + $this->document = $document; } else { $this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(),'count');