Skip to content

Commit

Permalink
Merge pull request #3 from pixelfederation/fix-redis-ttl
Browse files Browse the repository at this point in the history
fix redis ttl
  • Loading branch information
majoskorec authored Jan 18, 2019
2 parents d72bbb6 + 8f011ea commit 3088453
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
.idea
18 changes: 15 additions & 3 deletions src/RedisStateStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class RedisStateStorage implements StateStorageInterface
{
private const BUCKET_EXPIRE_SECONDS = 120;
private const BUCKET_KEY_FORMAT = 'phystrix_bucket:%s_%s_%s';
private const CIRCUIT_EXPIRE_SECONDS = 86400;
private const CIRCUIT_OPEN_KEY_FORMAT = 'phystrix_circuit_open:%s';
private const CIRCUIT_TEST_KEY_FORMAT = 'phystrix_circuit_test:%s';

Expand All @@ -62,7 +63,14 @@ public function __construct(Redis $redis)
public function incrementBucket($commandKey, $type, $index): void
{
$key = $this->getBucketKey($commandKey, $type, $index);
if (!$this->redis->exists($key)) {
$this->redis->setex($key, self::BUCKET_EXPIRE_SECONDS, 1);
return;
}

$this->redis->incr($key);
$this->redis->expire($key, self::BUCKET_EXPIRE_SECONDS);

}

/**
Expand Down Expand Up @@ -95,7 +103,7 @@ public function openCircuit($commandKey, $sleepingWindowInMilliseconds): void
$openKey = $this->getCircuitOpenKey($commandKey);
$testKey = $this->getCircuitTestKey($commandKey);

$this->redis->set($openKey, true);
$this->redis->setex($openKey, self::CIRCUIT_EXPIRE_SECONDS, true);

$sleepingWindowInSeconds = ceil($sleepingWindowInMilliseconds / 1000);
$this->redis->setex($testKey, $sleepingWindowInSeconds, true);
Expand All @@ -107,7 +115,7 @@ public function openCircuit($commandKey, $sleepingWindowInMilliseconds): void
public function closeCircuit($commandKey): void
{
$key = $this->getCircuitOpenKey($commandKey);
$this->redis->set($key, false);
$this->redis->del([$key]);
}

/**
Expand All @@ -116,8 +124,12 @@ public function closeCircuit($commandKey): void
public function isCircuitOpen($commandKey): bool
{
$key = $this->getCircuitOpenKey($commandKey);
$value = $this->redis->get($key);
if ($value === null) {
return false;
}

return (bool) $this->redis->get($key);
return (bool)$value;
}

/**
Expand Down

0 comments on commit 3088453

Please sign in to comment.