-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCluster.php
209 lines (175 loc) · 5.03 KB
/
Cluster.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* This file is part of PHPinnacle/Cassis.
*
* (c) PHPinnacle Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace PHPinnacle\Cassis;
use function Amp\call;
use Amp\Promise;
use Amp\Socket;
use Amp\Uri\Uri;
final class Cluster
{
private const
STATE_NOT_CONNECTED = 0,
STATE_CONNECTING = 1,
STATE_CONNECTED = 2
;
/**
* @var Config
*/
private $config;
/**
* @var Streams
*/
private $streams;
/**
* @var int
*/
private $state = self::STATE_NOT_CONNECTED;
/**
* @var Events
*/
private $events;
/**
* @param Config $config
*/
public function __construct(Config $config)
{
$this->config = $config;
$this->streams = Streams::instance();
}
/**
* @param string $dsn
*
* @return self
*/
public static function build(string $dsn): self
{
return new self(Config::parse($dsn));
}
/**
* @return Promise<array>
*/
public function options(): Promise
{
return call(function () {
/** @var Connection $connection */
$connection = yield $this->open();
/** @var Response\Supported $response */
$response = yield $connection->send(new Request\Options);
$connection->close();
return $response->options;
});
}
/**
* @return Promise<Events>
*/
public function events(): Promise
{
return call(function () {
if ($this->events) {
return $this->events;
}
return $this->events = new Events(yield $this->startup());
});
}
/**
* @param string $keyspace
*
* @return Promise<void>
*/
public function connect(string $keyspace = null): Promise
{
return call(function () use ($keyspace) {
if ($this->state !== self::STATE_NOT_CONNECTED) {
throw Exception\ClientException::alreadyConnected();
}
$this->state = self::STATE_CONNECTING;
$session = new Session(yield $this->startup());
if ($keyspace !== null) {
yield $session->keyspace($keyspace);
}
$this->state = self::STATE_CONNECTED;
return $session;
});
}
/**
* @return Promise<Connection>
*/
private function startup()
{
return call(function () {
/** @var Connection $connection */
$connection = yield $this->open();
$request = new Request\Startup($this->config->options());
$response = yield $connection->send($request);
if ($response instanceof Response\Authenticate) {
yield $this->authenticate($connection);
}
return $connection;
});
}
/**
* @return Promise<Connection>
*/
private function open(): Promise
{
return call(function () {
$compressor = $this->detectCompressor();
foreach ($this->config->hosts() as $host) {
$connection = new Connection(new Uri($host), $this->streams, $compressor);
try {
yield $connection->open(
$this->config->tcpTimeout(),
$this->config->tcpAttempts(),
$this->config->tcpNoDelay()
);
return $connection;
} catch (Socket\ConnectException $error) {
continue;
}
}
throw Exception\ClientException::couldNotConnect();
});
}
/**
* @param Connection $connection
*
* @return Promise<Response\AuthSuccess>
*/
private function authenticate(Connection $connection): Promise
{
return call(function () use ($connection) {
$request = new Request\AuthResponse($this->config->user(), $this->config->password());
$response = yield $connection->send($request);
switch (true) {
case $response instanceof Response\AuthSuccess:
return $response;
default:
throw Exception\ServerException::unexpectedFrame($response->opcode);
}
});
}
/**
* @return Compressor
*/
private function detectCompressor(): Compressor
{
switch ($this->config->compression()) {
case Config::COMPRESSION_NONE:
return new Compressor\NoneCompressor;
case Config::COMPRESSION_LZ4:
return new Compressor\LzCompressor;
case Config::COMPRESSION_SNAPPY:
return new Compressor\SnappyCompressor;
default:
throw Exception\ConfigException::unknownCompressionMechanism($this->config->compression());
}
}
}