-
-
Notifications
You must be signed in to change notification settings - Fork 998
Expand file tree
/
Copy pathRelayFactory.php
More file actions
175 lines (144 loc) · 5.05 KB
/
Copy pathRelayFactory.php
File metadata and controls
175 lines (144 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2026 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Connection;
use InvalidArgumentException;
use Predis\Command\RawCommand;
use Predis\NotSupportedException;
use Predis\Retry\Strategy\EqualBackoff;
use Predis\Retry\Strategy\ExponentialBackoff;
use Relay\Relay;
class RelayFactory extends Factory
{
/**
* @var string[]
*/
protected $schemes = [
'tcp' => RelayConnection::class,
'tls' => RelayConnection::class,
'unix' => RelayConnection::class,
'redis' => RelayConnection::class,
'rediss' => RelayConnection::class,
];
/**
* {@inheritDoc}
*/
public function define($scheme, $initializer)
{
throw new NotSupportedException('Does not allow to override existing initializer.');
}
/**
* {@inheritDoc}
*/
public function undefine($scheme)
{
throw new NotSupportedException('Does not allow to override existing initializer.');
}
/**
* {@inheritDoc}
*/
public function create($parameters): NodeConnectionInterface
{
$this->assertExtensions();
if (!$parameters instanceof ParametersInterface) {
$parameters = $this->createParameters($parameters);
}
$scheme = $parameters->scheme;
if (!isset($this->schemes[$scheme])) {
throw new InvalidArgumentException("Unknown connection scheme: '$scheme'.");
}
$initializer = $this->schemes[$scheme];
$client = $this->createClient($parameters);
$connection = new $initializer($parameters, $client);
$this->prepareConnection($connection);
return $connection;
}
/**
* Checks if the Relay extension is loaded in PHP.
*/
private function assertExtensions()
{
if (!extension_loaded('relay')) {
throw new NotSupportedException(
'The "relay" extension is required by this connection backend.'
);
}
}
/**
* Creates a new instance of the client.
*
* @return Relay
*/
private function createClient(ParametersInterface $parameters)
{
$client = new Relay();
// throw when errors occur and return `null` for non-existent keys
$client->setOption(Relay::OPT_PHPREDIS_COMPATIBILITY, false);
// use reply literals
$client->setOption(Relay::OPT_REPLY_LITERAL, true);
// whether to use in-memory caching
$client->setOption(Relay::OPT_USE_CACHE, $parameters->cache ?? true);
// set data serializer
$client->setOption(Relay::OPT_SERIALIZER, constant(sprintf(
'%s::SERIALIZER_%s',
Relay::class,
strtoupper($parameters->serializer ?? 'none')
)));
// set data compression algorithm
$client->setOption(Relay::OPT_COMPRESSION, constant(sprintf(
'%s::COMPRESSION_%s',
Relay::class,
strtoupper($parameters->compression ?? 'none')
)));
if ($parameters->isDisabledRetry()) {
$client->setOption(Relay::OPT_MAX_RETRIES, 0);
} else {
$client->setOption(Relay::OPT_MAX_RETRIES, $parameters->retry->getRetries());
$retryStrategy = $parameters->retry->getStrategy();
if ($retryStrategy instanceof ExponentialBackoff) {
$algorithm = Relay::BACKOFF_ALGORITHM_FULL_JITTER;
$base = $retryStrategy->getBase();
$cap = $retryStrategy->getCap();
} else {
$algorithm = Relay::BACKOFF_ALGORITHM_DEFAULT;
if ($retryStrategy instanceof EqualBackoff) {
$base = $cap = $retryStrategy->compute(0);
} else {
$base = $retryStrategy::DEFAULT_BASE;
$cap = $retryStrategy::DEFAULT_CAP;
}
}
$client->setOption(Relay::OPT_BACKOFF_ALGORITHM, $algorithm);
$client->setOption(Relay::OPT_BACKOFF_BASE, $base / 1000);
$client->setOption(Relay::OPT_BACKOFF_CAP, $cap / 1000);
}
return $client;
}
/**
* {@inheritdoc}
*/
protected function prepareConnection(NodeConnectionInterface $connection)
{
$parameters = $connection->getParameters();
if (isset($parameters->password) && strlen($parameters->password)) {
$cmdAuthArgs = isset($parameters->username) && strlen($parameters->username)
? [$parameters->username, $parameters->password]
: [$parameters->password];
$connection->addConnectCommand(
new RawCommand('AUTH', $cmdAuthArgs)
);
}
if (isset($parameters->database) && strlen($parameters->database)) {
$connection->addConnectCommand(
new RawCommand('SELECT', [$parameters->database])
);
}
}
}