forked from catalyst/moodle-cachestore_redissentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinel.php
180 lines (128 loc) · 4.15 KB
/
sentinel.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
<?php
class sentinel {
private $sentinels = array();
public $connecttimeout = 1;
public $readtimeout = 1;
public $persistent = true;
private $flags;
private $connected;
private $socket;
private $pingonconnect = false;
public function __construct($sentinels) {
$this->sentinels = $sentinels;
$this->flags = STREAM_CLIENT_CONNECT;
$this->connected = false;
}
public function __destruct() {
if (!$this->persistent && $this->connected) {
$this->disconnect();
}
}
public function connecttopool() {
if ($this->connected) {
return true;
}
foreach ($this->sentinels as $sentinel) {
if ($this->connect($sentinel)) {
return true;
}
}
throw new \Exception('Unable to connect to sentinel pool');
}
private function connect($sentinel) {
if ($this->persistent) {
$this->socket = @stream_socket_client($sentinel, $errorno, $errstr, $this->connecttimeout, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
} else {
$this->socket = @stream_socket_client($sentinel, $errorno, $errstr, $this->connecttimeout);
}
if (!$this->socket) {
$this->connected = false;
return false;
}
$this->connected = true;
stream_set_blocking($this->socket, true);
stream_set_timeout($this->socket, $this->readtimeout);
// Test sentinel is alive
if ($this->pingonconnect) {
fwrite($this->socket, "PING\n");
if (trim(fgets($this->socket)) != '+PONG') {
fclose($this->socket);
$this->connected = false;
return false;
}
}
return true;
}
public function disconnect() {
fclose($this->socket);
$this->connected = false;
}
public function get_master_addr($name) {
$cmd = "get-master-addr-by-name $name";
$this->command($cmd);
if (!$resp = $this->readreply()) {
return false;
}
$ret = new \stdClass();
$ret->ip = $resp[0];
$ret->port = $resp[1];
return ($ret);
}
private function command($command) {
if (!$this->connected) {
$this->connecttopool();
}
if (!$this->connected) {
return false;
}
$cmd = "SENTINEL $command\n";
$cmdlen = strlen($cmd);
$lastwrite = 0;
for ($written = 0; $written < $cmdlen; $written += $lastwrite) {
$lastwrite = fwrite($this->socket, substr($cmd, $written));
if ($lastwrite === false || $lastwrite == 0) {
$this->connected = false;
throw new \Exception('Failed to write command to stream');
}
}
}
private function readreply() {
if (!$this->connected) {
return false;
}
$resp = fgets($this->socket);
$type = substr($resp, 0, 1);
switch($type) {
// Error response
case '-':
throw new \Exception('Error response received: '.$resp);
break;
// In-line response
case '+':
$response = substr($resp, 1);
return(substr($resp, 1));
// Defined size response
case '$':
$size = (int) substr($resp, 1);
$resp = stream_get_contents($this->socket, $size+2);
if ($resp === false) {
throw new \Exception('Failed to read from stream');
}
return (trim($resp));
// Int response
case ':':
return ((int)substr($reply,1));
// Multi line response
case '*':
$multireponse = array();
$size = (int) substr($resp, 1);
for ($i=0;$i<$size; $i++) {
$multireponse[] = $this->readreply();
}
return($multireponse);
// Unknown reesponse
default:
throw new \Exception('Unknown read response from stream');
}
}
}