-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathLazyClient.php
221 lines (192 loc) · 6.35 KB
/
LazyClient.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
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Clue\React\Redis;
use Evenement\EventEmitter;
use React\Stream\Util;
use React\EventLoop\LoopInterface;
/**
* @internal
*/
class LazyClient extends EventEmitter implements Client
{
private $target;
/** @var Factory */
private $factory;
private $closed = false;
private $promise;
private $loop;
private $idlePeriod = 60.0;
private $idleTimer;
private $pending = 0;
private $subscribed = array();
private $psubscribed = array();
/**
* @param $target
*/
public function __construct($target, Factory $factory, LoopInterface $loop)
{
$args = array();
\parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
if (isset($args['idle'])) {
$this->idlePeriod = (float)$args['idle'];
}
$this->target = $target;
$this->factory = $factory;
$this->loop = $loop;
}
private function client()
{
if ($this->promise !== null) {
return $this->promise;
}
$self = $this;
$pending =& $this->promise;
$idleTimer=& $this->idleTimer;
$subscribed =& $this->subscribed;
$psubscribed =& $this->psubscribed;
$loop = $this->loop;
return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
// connection completed => remember only until closed
$redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
$pending = null;
// foward unsubscribe/punsubscribe events when underlying connection closes
$n = count($subscribed);
foreach ($subscribed as $channel => $_) {
$self->emit('unsubscribe', array($channel, --$n));
}
$n = count($psubscribed);
foreach ($psubscribed as $pattern => $_) {
$self->emit('punsubscribe', array($pattern, --$n));
}
$subscribed = array();
$psubscribed = array();
if ($idleTimer !== null) {
$loop->cancelTimer($idleTimer);
$idleTimer = null;
}
});
// keep track of all channels and patterns this connection is subscribed to
$redis->on('subscribe', function ($channel) use (&$subscribed) {
$subscribed[$channel] = true;
});
$redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
$psubscribed[$pattern] = true;
});
$redis->on('unsubscribe', function ($channel) use (&$subscribed) {
unset($subscribed[$channel]);
});
$redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
unset($psubscribed[$pattern]);
});
Util::forwardEvents(
$redis,
$self,
array(
'message',
'subscribe',
'unsubscribe',
'pmessage',
'psubscribe',
'punsubscribe',
)
);
return $redis;
}, function (\Exception $e) use (&$pending) {
// connection failed => discard connection attempt
$pending = null;
throw $e;
});
}
public function __call($name, $args)
{
if ($this->closed) {
return \React\Promise\reject(new \RuntimeException(
'Connection closed (ENOTCONN)',
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
));
}
$that = $this;
return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
$that->awake();
return \call_user_func_array(array($redis, $name), $args)->then(
function ($result) use ($that) {
$that->idle();
return $result;
},
function ($error) use ($that) {
$that->idle();
throw $error;
}
);
});
}
public function end()
{
if ($this->promise === null) {
$this->close();
}
if ($this->closed) {
return;
}
$that = $this;
return $this->client()->then(function (Client $redis) use ($that) {
$redis->on('close', function () use ($that) {
$that->close();
});
$redis->end();
});
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
// either close active connection or cancel pending connection attempt
if ($this->promise !== null) {
$this->promise->then(function (Client $redis) {
$redis->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
$this->promise->cancel();
$this->promise = null;
}
}
if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
$this->emit('close');
$this->removeAllListeners();
}
/**
* @internal
*/
public function awake()
{
++$this->pending;
if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
}
/**
* @internal
*/
public function idle()
{
--$this->pending;
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
$idleTimer =& $this->idleTimer;
$promise =& $this->promise;
$idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
$promise->then(function (Client $redis) {
$redis->close();
});
$promise = null;
$idleTimer = null;
});
}
}
}