I found this piece of code in a Github PR that aims at adding the Redis Sentinel support to a Redis client. (Redis sentinel is a service that will frequently check the state of multiple redis, and return the (redis) master’s IP.
I’m not an expert in PHP, so, I really don’t understand how it works, even though it seems to work correctly.
public function masterAddress(): PromiseInterface
{
$chain = reject(new RuntimeException('Initial reject promise'));
foreach ($this->urls as $url) {
$chain = $chain->then(function ($masterUrl) {
return $masterUrl;
}, function () use ($url) {
return $this->onError($url);
});
}
return $chain;
}
In this code:
$this->urls
are the different redis sentinel’s urls- $masterAddress (which is returned from the promise) is the master’s url
- By calling
$this->masterAddress()
I get the master’s URL
I don’t even understand how it could return an URL since the $url
variable isn’t even used, so, how the chain could be anything else than just a chain having a “reject”.
1