1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Clients\Redis; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
5
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
6
|
|
|
use PHPDaemon\Network\ClientConnection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package NetworkClients |
10
|
|
|
* @subpackage RedisClient |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Pool extends \PHPDaemon\Network\Client { |
14
|
|
|
public $servConnSub = []; |
15
|
|
|
|
16
|
|
|
protected $currentMasterAddr; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @TODO |
|
|
|
|
20
|
|
|
* @param string $key |
21
|
|
|
* @param integer $timeout |
22
|
|
|
* @return Lock |
23
|
|
|
*/ |
24
|
|
|
public function lock($key, $timeout) { |
25
|
|
|
return new Lock($key, $timeout, $this); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Easy wrapper for queue of eval's |
30
|
|
|
* @param callable $cb |
|
|
|
|
31
|
|
|
* @return MultiEval |
32
|
|
|
*/ |
33
|
|
|
public function meval($cb = null) { |
34
|
|
|
return new MultiEval($cb, $this); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Wrapper for scans commands |
39
|
|
|
* @param string $cmd Command |
40
|
|
|
* @param array $args Arguments |
41
|
|
|
* @param cllable $cbEnd Callback |
|
|
|
|
42
|
|
|
* @param integer $limit Limit |
|
|
|
|
43
|
|
|
* @return AutoScan |
44
|
|
|
*/ |
45
|
|
|
public function autoscan($cmd, $args = [], $cbEnd = null, $limit = null) { |
46
|
|
|
return new AutoScan($this, $cmd, $args, $cbEnd, $limit); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Setting default config options |
51
|
|
|
* Overriden from NetworkClient::getConfigDefaults |
52
|
|
|
* @return array|bool |
53
|
|
|
*/ |
54
|
|
|
protected function getConfigDefaults() { |
55
|
|
|
return [ |
56
|
|
|
/* [string|array] Default servers */ |
|
|
|
|
57
|
|
|
'servers' => 'tcp://127.0.0.1', |
58
|
|
|
|
59
|
|
|
/* [integer] Default port */ |
|
|
|
|
60
|
|
|
'port' => 6379, |
61
|
|
|
|
62
|
|
|
/* [integer] Maximum connections per server */ |
63
|
|
|
'maxconnperserv' => 32, |
64
|
|
|
|
65
|
|
|
/* [integer] Maximum allowed size of packet */ |
66
|
|
|
'max-allowed-packet' => new \PHPDaemon\Config\Entry\Size('1M'), |
67
|
|
|
|
68
|
|
|
/* [boolean] If true, race condition between UNSUBSCRIBE and PUBLISH will be journaled */ |
69
|
|
|
'log-pub-sub-race-condition' => true, |
70
|
|
|
|
71
|
|
|
/* [integer] Select storage number */ |
72
|
|
|
'select' => null, |
73
|
|
|
|
74
|
|
|
/* [integer] <master name> for Sentinel */ |
75
|
|
|
'sentinel-master' => null, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @TODO |
|
|
|
|
81
|
|
|
* @param string $chan |
82
|
|
|
* @return integer |
83
|
|
|
*/ |
84
|
|
|
public function getLocalSubscribersCount($chan) { |
85
|
|
|
foreach ($this->servConnSub as $conn) { |
86
|
|
|
return $conn->getLocalSubscribersCount($chan); |
87
|
|
|
} |
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Magic __call |
93
|
|
|
* Example: |
94
|
|
|
* $redis->lpush('mylist', microtime(true)); |
95
|
|
|
* @param string $name Command name |
|
|
|
|
96
|
|
|
* @param array $args Arguments |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function __call($cmd, $args) { |
100
|
|
|
$cb = null; |
101
|
|
View Code Duplication |
for ($i = sizeof($args) - 1; $i >= 0; --$i) { |
|
|
|
|
102
|
|
|
$a = $args[$i]; |
103
|
|
|
if ((is_array($a) || is_object($a)) && is_callable($a)) { |
104
|
|
|
$cb = CallbackWrapper::wrap($a); |
105
|
|
|
$args = array_slice($args, 0, $i); |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
elseif ($a !== null) { |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
reset($args); |
113
|
|
|
$cmd = strtoupper($cmd); |
114
|
|
|
|
115
|
|
|
if ($this->sendSubCommand($cmd, $args, $cb)) { |
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($cmd === 'SENTINEL' || !isset($this->config->sentinelmaster->value)) { |
|
|
|
|
120
|
|
|
$this->sendCommand(null, $cmd, $args, $cb); |
|
|
|
|
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
if ($this->currentMasterAddr !== null) { |
124
|
|
|
$this->sendCommand($this->currentMasterAddr, $cmd, $args, $cb); |
|
|
|
|
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
$this->sentinel('get-master-addr-by-name', $this->config->sentinelmaster->value, function($redis) use ($cmd, $args, $cb) { |
|
|
|
|
128
|
|
|
$this->currentMasterAddr = 'tcp://' . $redis->result[0] .':' . $redis->result[1]; |
129
|
|
|
$this->sendCommand($this->currentMasterAddr, $cmd, $args, $cb); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @TODO |
|
|
|
|
135
|
|
|
* @param string $addr |
136
|
|
|
* @param string $cmd |
137
|
|
|
* @param array $args |
138
|
|
|
* @param callable $cb |
139
|
|
|
* @callback $cb ( ) |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
protected function sendCommand($addr, $cmd, $args, $cb) { |
143
|
|
|
$this->getConnection($addr, function ($conn) use ($cmd, $args, $cb) { |
144
|
|
|
if (!$conn->isConnected()) { |
145
|
|
|
call_user_func($cb, false); |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($this->sendSubCommand($cmd, $args, $cb)) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$conn->command($cmd, $args, $cb); |
154
|
|
|
}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @TODO |
|
|
|
|
159
|
|
|
* @param string $cmd |
160
|
|
|
* @param array $args |
161
|
|
|
* @param callable $cb |
162
|
|
|
* @callback $cb ( ) |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
protected function sendSubCommand($cmd, $args, $cb) { |
166
|
|
|
if (in_array($cmd, ['SUBSCRIBE', 'PSUBSCRIBE', 'UNSUBSCRIBE', 'PUNSUBSCRIBE', 'UNSUBSCRIBEREAL'])) { |
167
|
|
|
foreach ($this->servConnSub as $conn) { |
168
|
|
|
$conn->command($cmd, $args, $cb); |
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.