Skip to content

Commit

Permalink
Merge pull request #17 from flownative/16-make-timeouts-configurable
Browse files Browse the repository at this point in the history
Make timeouts configurable
  • Loading branch information
robertlemke committed Sep 6, 2022
2 parents 1e356cc + 2b16009 commit 88a764c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Classes/RedisBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class RedisBackend extends IndependentAbstractBackend implements TaggableBackend
protected ?bool $frozen = null;
protected string $hostname = '127.0.0.1';
protected int $port = 6379;
protected float $timeout = 5;
protected float $readWriteTimeout = 1;
protected array $sentinels = [];
protected string $service = 'mymaster';
protected int $database = 0;
Expand All @@ -67,8 +69,10 @@ public function __construct(EnvironmentConfiguration $environmentConfiguration,
$this->client = $this->getRedisClient();

if ($this->logErrors && class_exists(Bootstrap::class) && Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) {
$this->logger = Bootstrap::$staticObjectManager->get(LoggerInterface::class);
$this->throwableStorage = Bootstrap::$staticObjectManager->get(ThrowableStorageInterface::class);
if (Bootstrap::$staticObjectManager->isRegistered(LoggerInterface::class)) {
$this->logger = Bootstrap::$staticObjectManager->get(LoggerInterface::class);
$this->throwableStorage = Bootstrap::$staticObjectManager->get(ThrowableStorageInterface::class);
}
}
}

Expand Down Expand Up @@ -506,6 +510,22 @@ public function setLogErrors(bool $logErrors): void
$this->logErrors = $logErrors;
}

/**
* @param float|int|string $timeout
*/
public function setTimeout($timeout): void
{
$this->timeout = (float)$timeout;
}

/**
* @param float|int|string $readWriteTimeout
*/
public function setReadWriteTimeout($readWriteTimeout): void
{
$this->readWriteTimeout = (float)$readWriteTimeout;
}

/**
* @param string|bool $value
* @return string|bool
Expand Down Expand Up @@ -555,7 +575,9 @@ private function getRedisClient(): \Predis\Client
try {
$options = [
'parameters' => [
'database' => $this->database
'database' => $this->database,
'timeout' => $this->timeout,
'read_write_timeout' => $this->readWriteTimeout
]
];

Expand Down
8 changes: 8 additions & 0 deletions Configuration/Caches.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Flownative_Example_CacheWithStandaloneRedis:
hostname: '%env:REDIS_HOST%'
password: '%env:REDIS_PASSWORD%'
port: 6379
timeout: 3
readWriteTimeout: 0.5
database: 0
deduplicateErrors: true
logErrors: true

Flownative_Example_CacheWithRedisSentinel:
backend: 'Flownative\RedisSentinel\RedisBackend'
Expand All @@ -15,4 +19,8 @@ Flownative_Example_CacheWithRedisSentinel:
- 'tcp://10.101.213.147:26379'
service: 'mymaster'
password: 'password'
timeout: 3
readWriteTimeout: 0.5
database: 0
deduplicateErrors: true
logErrors: true
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Flow_Mvc_Routing_Route:
password: '%env:REDIS_PASSWORD%'
port: '%env:REDIS_PORT%'
database: 0
timeout: 5
readWriteTimeout: 0.5

Flow_Mvc_Routing_Resolve:
backend: 'Flownative\RedisSentinel\RedisBackend'
Expand All @@ -43,6 +45,16 @@ Flow_Mvc_Routing_Resolve:
Of course you can also set concrete values instead of using environment
variables.

Note that you can set two different timeouts:

- "timeout" (default: 5) specifies the time in seconds to wait while
connecting to Redis
- "readWriteTimeout" (default: 1) specifies the time to wait during a read
or write operation

You can specify float numbers as timeout values. For example, use `0.5` for
setting the timeout to half a second.

When Redis is running in a high availability setup with Sentinel servers, you
need to configure the Redis Backend to access the Sentinel servers instead of
the actual Redis nodes.
Expand All @@ -60,6 +72,8 @@ Flow_Mvc_Routing_Route:
service: 'mymaster'
password: 'a-very-long-password'
database: 0
timeout: 0.5
readWriteTimeout: 0.1

Flow_Mvc_Routing_Resolve:
backend: 'Flownative\RedisSentinel\RedisBackend'
Expand Down

0 comments on commit 88a764c

Please sign in to comment.