Skip to content

Commit

Permalink
New clear limit function
Browse files Browse the repository at this point in the history
  • Loading branch information
snipershady committed Nov 6, 2022
1 parent 26b9c71 commit 29e65a0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 33 deletions.
50 changes: 25 additions & 25 deletions composer.lock

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

7 changes: 7 additions & 0 deletions src/Service/AbstractRateLimiterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public abstract function isLimited(string $key, int $limit, int $ttl): bool;
*/
public abstract function isLimitedWithBan(string $key, int $limit, int $ttl, int $maxAttempts, int $banTimeFrame, int $banTtl, ?string $clientIp): bool;

/**
* <p>Delete the limited key</p>
* @param string $key <p>key to set free from limiter</p>
* @return bool
*/
public abstract function clearRateLimitedKey(string $key): bool;

/**
* <p>Verify if <b>ttl</b> parameter is positive integer. Throws InvalidArgumentException</p>
* @param int $ttl
Expand Down
9 changes: 9 additions & 0 deletions src/Service/RateLimiterServiceAPCu.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ public function isLimitedWithBan(string $key, int $limit, int $ttl, int $maxAtte
return $actual;
}

/**
* {@inheritDoc}
*/
public function clearRateLimitedKey(string $key): bool {
$this->checkKey($key);
return apcu_delete($key);
}


}
8 changes: 8 additions & 0 deletions src/Service/RateLimiterServiceRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ public function isLimitedWithBan(string $key, int $limit, int $ttl, int $maxAtte
return $actual;
}

/**
* {@inheritDoc}
*/
public function clearRateLimitedKey(string $key): bool {
$this->checkKey($key);
return (bool)$this->redis->del($key);
}

}
60 changes: 52 additions & 8 deletions tests/RateLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function tearDown(): void {
apcu_clear_cache();
}

public function testApcuTTLexpiration() {
public function testApcuTTLexpiration(): void {
//$this->markTestSkipped();
$limit = 2;
$ttl = 3;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testApcuTTLexpiration() {
$this->assertTrue($result); // limit reached
}

public function testRedisTTLexpiration() {
public function testRedisTTLexpiration(): void {
//$this->markTestSkipped();
$limit = 2;
$ttl = 3;
Expand Down Expand Up @@ -114,7 +114,7 @@ public function testRedisTTLexpiration() {
$this->assertTrue($result); // limit reached
}

public function testLimitApcu() {
public function testLimitApcu(): void {
//$this->markTestSkipped();
$limiter = AbstractRateLimiterService::factory(CacheEnum::APCU);
$key = "test" . microtime(true);
Expand All @@ -133,7 +133,7 @@ public function testLimitApcu() {
$this->assertTrue($countTrue === ($attempts - $limit));
}

public function testLimitRedis() {
public function testLimitRedis(): void {
//$this->markTestSkipped();
$this->redis = new Client("tcp://$this->servername:$this->port?persistent=redis01");

Expand All @@ -154,7 +154,7 @@ public function testLimitRedis() {
$this->assertTrue($countTrue === ($attempts - $limit));
}

public function testLimitRedisLimitOne() {
public function testLimitRedisLimitOne(): void {
//$this->markTestSkipped();

$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
Expand All @@ -174,7 +174,7 @@ public function testLimitRedisLimitOne() {
$this->assertTrue($countTrue === ($attempts - $limit));
}

public function testLimitRedisLimitOneAgain() {
public function testLimitRedisLimitOneAgain(): void {
//$this->markTestSkipped();
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
$key = "test" . microtime(true);
Expand All @@ -196,7 +196,7 @@ public function testLimitRedisLimitOneAgain() {
$this->assertTrue($result);
}

public function testLimitRedisLimitOneAgainTtlExpire() {
public function testLimitRedisLimitOneAgainTtlExpire(): void {
//$this->markTestSkipped();
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
$key = "test" . microtime(true);
Expand All @@ -216,7 +216,7 @@ public function testLimitRedisLimitOneAgainTtlExpire() {
$this->assertEquals($currentTtl, ($ttl - $sleep));
}

public function testLimitRedisLimitOneAgainTtlExpireFiveSeconds() {
public function testLimitRedisLimitOneAgainTtlExpireFiveSeconds(): void {
//$this->markTestSkipped();
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
$key = "test" . microtime(true);
Expand All @@ -236,4 +236,48 @@ public function testLimitRedisLimitOneAgainTtlExpireFiveSeconds() {
$this->assertEquals($currentTtl, ($ttl - $sleep));
}

public function testLimitRedisAndDeleteKey(): void {
$limiter = AbstractRateLimiterService::factory(CacheEnum::REDIS, $this->redis);
$key = "test" . microtime(true);
$limit = 1;
$ttl = 60;
$sleep = 5;
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertFalse($result);
$currentTtl = $this->redis->ttl($key);
$this->assertEquals($currentTtl, $ttl);
sleep($sleep);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertTrue($result);
$currentTtl = $this->redis->ttl($key);
$this->assertEquals($currentTtl, ($ttl - $sleep));
$limiter->clearRateLimitedKey($key);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertFalse($result);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertTrue($result);
}

public function testLimitApcuAndDeleteKey(): void {
//$this->markTestSkipped();
$limiter = AbstractRateLimiterService::factory(CacheEnum::APCU);
$key = "test" . microtime(true);
$limit = 1;
$ttl = 60;
$sleep = 5;

$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertFalse($result);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertTrue($result);
sleep($sleep);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertTrue($result);
$limiter->clearRateLimitedKey($key);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertFalse($result);
$result = $limiter->isLimited($key, $limit, $ttl);
$this->assertTrue($result);
}

}

0 comments on commit 29e65a0

Please sign in to comment.