Debian Codegraph @

vendor/clue/redis-react/src/LazyClient.php

raw
1 <?php
2
3 namespace Clue\React\Redis;
4
5 use Evenement\EventEmitter;
6 use React\Stream\Util;
7 use React\EventLoop\LoopInterface;
8
9 /**
10 * @internal
11 */
12 class LazyClient extends EventEmitter implements Client
13 {
14 private $target;
15 /** @var Factory */
16 private $factory;
17 private $closed = false;
18 private $promise;
19
20 private $loop;
21 private $idlePeriod = 60.0;
22 private $idleTimer;
23 private $pending = 0;
24
25 private $subscribed = array();
26 private $psubscribed = array();
27
28 /**
29 * @param $target
30 */
31 public function __construct($target, Factory $factory, LoopInterface $loop)
32 {
33 $args = array();
34 \parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
35 if (isset($args['idle'])) {
36 $this->idlePeriod = (float)$args['idle'];
37 }
38
39 $this->target = $target;
40 $this->factory = $factory;
41 $this->loop = $loop;
42 }
43
44 private function client()
45 {
46 if ($this->promise !== null) {
47 return $this->promise;
48 }
49
50 $self = $this;
51 $pending =& $this->promise;
52 $idleTimer=& $this->idleTimer;
53 $subscribed =& $this->subscribed;
54 $psubscribed =& $this->psubscribed;
55 $loop = $this->loop;
56 return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
57 // connection completed => remember only until closed
58 $redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
59 $pending = null;
60
61 // foward unsubscribe/punsubscribe events when underlying connection closes
62 $n = count($subscribed);
63 foreach ($subscribed as $channel => $_) {
64 $self->emit('unsubscribe', array($channel, --$n));
65 }
66 $n = count($psubscribed);
67 foreach ($psubscribed as $pattern => $_) {
68 $self->emit('punsubscribe', array($pattern, --$n));
69 }
70 $subscribed = array();
71 $psubscribed = array();
72
73 if ($idleTimer !== null) {
74 $loop->cancelTimer($idleTimer);
75 $idleTimer = null;
76 }
77 });
78
79 // keep track of all channels and patterns this connection is subscribed to
80 $redis->on('subscribe', function ($channel) use (&$subscribed) {
81 $subscribed[$channel] = true;
82 });
83 $redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
84 $psubscribed[$pattern] = true;
85 });
86 $redis->on('unsubscribe', function ($channel) use (&$subscribed) {
87 unset($subscribed[$channel]);
88 });
89 $redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
90 unset($psubscribed[$pattern]);
91 });
92
93 Util::forwardEvents(
94 $redis,
95 $self,
96 array(
97 'message',
98 'subscribe',
99 'unsubscribe',
100 'pmessage',
101 'psubscribe',
102 'punsubscribe',
103 )
104 );
105
106 return $redis;
107 }, function (\Exception $e) use (&$pending) {
108 // connection failed => discard connection attempt
109 $pending = null;
110
111 throw $e;
112 });
113 }
114
115 public function __call($name, $args)
116 {
117 if ($this->closed) {
118 return \React\Promise\reject(new \RuntimeException(
119 'Connection closed (ENOTCONN)',
120 defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
121 ));
122 }
123
124 $that = $this;
125 return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
126 $that->awake();
127 return \call_user_func_array(array($redis, $name), $args)->then(
128 function ($result) use ($that) {
129 $that->idle();
130 return $result;
131 },
132 function ($error) use ($that) {
133 $that->idle();
134 throw $error;
135 }
136 );
137 });
138 }
139
140 public function end()
141 {
142 if ($this->promise === null) {
143 $this->close();
144 }
145
146 if ($this->closed) {
147 return;
148 }
149
150 $that = $this;
151 return $this->client()->then(function (Client $redis) use ($that) {
152 $redis->on('close', function () use ($that) {
153 $that->close();
154 });
155 $redis->end();
156 });
157 }
158
159 public function close()
160 {
161 if ($this->closed) {
162 return;
163 }
164
165 $this->closed = true;
166
167 // either close active connection or cancel pending connection attempt
168 if ($this->promise !== null) {
169 $this->promise->then(function (Client $redis) {
170 $redis->close();
171 }, function () {
172 // ignore to avoid reporting unhandled rejection
173 });
174 if ($this->promise !== null) {
175 $this->promise->cancel();
176 $this->promise = null;
177 }
178 }
179
180 if ($this->idleTimer !== null) {
181 $this->loop->cancelTimer($this->idleTimer);
182 $this->idleTimer = null;
183 }
184
185 $this->emit('close');
186 $this->removeAllListeners();
187 }
188
189 /**
190 * @internal
191 */
192 public function awake()
193 {
194 ++$this->pending;
195
196 if ($this->idleTimer !== null) {
197 $this->loop->cancelTimer($this->idleTimer);
198 $this->idleTimer = null;
199 }
200 }
201
202 /**
203 * @internal
204 */
205 public function idle()
206 {
207 --$this->pending;
208
209 if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
210 $idleTimer =& $this->idleTimer;
211 $promise =& $this->promise;
212 $idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
213 $promise->then(function (Client $redis) {
214 $redis->close();
215 });
216 $promise = null;
217 $idleTimer = null;
218 });
219 }
220 }
221 }