Skip to content

Commit 0672526

Browse files
committed
Refactor Client close()
1 parent 54eab65 commit 0672526

File tree

4 files changed

+38
-69
lines changed

4 files changed

+38
-69
lines changed

examples/etcd/put_double.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
$request = new Etcdserverpb\PutRequest();
1010
$request->setPrevKv(true);
1111

12-
Coroutine::create(function () use ($kvClient, $request) {
12+
$barrier = Coroutine\Barrier::make();
13+
14+
Coroutine::create(function () use ($kvClient, $request, $barrier) {
1315
$request->setKey('Hello~');
1416
$request->setValue('I am Swoole!');
1517
[$reply, $status] = $kvClient->Put($request);
@@ -21,7 +23,7 @@
2123
}
2224
});
2325

24-
Coroutine::create(function () use ($kvClient, $request) {
26+
Coroutine::create(function () use ($kvClient, $request, $barrier) {
2527
$request->setKey('Hey~');
2628
$request->setValue('How are u Etcd?');
2729
[$reply, $status] = $kvClient->Put($request);
@@ -33,6 +35,7 @@
3335
}
3436
});
3537

36-
// wait all of the responses back
37-
$kvClient->closeWait();
38+
// wait all the responses back
39+
$barrier::wait($barrier);
40+
$kvClient->close();
3841
});

examples/etcd/stress.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
$request->setPrevKv(true);
1313
$request->setValue('Swoole');
1414

15+
$barrier = Coroutine\Barrier::make();
1516
$start = microtime(true);
1617
for ($i = 10000; $i--;) {
17-
Coroutine::create(function () use ($kvClient, $request, $i) {
18+
Coroutine::create(function () use ($kvClient, $request, $i, $barrier) {
1819
$request->setKey("Hello{$i}");
1920
[$reply, $status] = $kvClient->Put($request);
2021
assert($reply->getPrevKv()->getKey() === "Hello{$i}");
@@ -25,8 +26,9 @@
2526
});
2627
}
2728

28-
// wait all of the responses back
29-
$kvClient->closeWait();
29+
// wait all the responses back
30+
$barrier::wait($barrier);
31+
$kvClient->close();
3032
echo 'use time: ' . (microtime(true) - $start) . "s\n";
3133
var_dump($kvClient->stats());
3234
var_dump(memory_get_usage(true));

src/Grpc/Client.php

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,10 @@ class Client
6666
*/
6767
protected $recvCid = 0;
6868
/**
69-
* The sign of if this Client is closing
70-
* @var int
71-
*/
72-
protected $closing = 0;
73-
/**
74-
* @var Channel
69+
* The sign of if Client->close() was called
70+
* @var bool
7571
*/
76-
protected $closeWaiter;
72+
protected $closed = false;
7773

7874
protected $host;
7975
protected $port;
@@ -169,36 +165,32 @@ protected function start()
169165
} // else: receiver not found, discard it
170166

171167
// push finished, check if close wait and no coroutine is waiting, if Y, stop recv loop
172-
if (!$this->closing || !empty($this->recvChannelMap)) {
168+
if (!$this->closed || !empty($this->recvChannelMap)) {
173169
continue;
174170
}
175171
}
176172

177173
// if you want to close it or retry connect failed, stop recv loop
178-
if ($this->closing) {
174+
if ($this->closed) {
179175
$need_break = true;
180176
} else {
181177
$need_break = !$this->client->connect();
182178
}
183179

184-
// ↑↓ We must `retry-connect` before we push `false` response
185-
// ↑↓ Then the pop channel coroutine can knows that if this client is available
186-
187-
// clear all, we will auto reconnect, but it need user retry again by himself
188-
if (!empty($this->recvChannelMap)) {
189-
foreach ($this->recvChannelMap as $the_channel) {
190-
$the_channel->close();
191-
}
192-
$this->recvChannelMap = [];
193-
}
194-
195180
if ($need_break) {
196181
break;
197182
}
198183
}
199184

200185
$this->recvCid = 0;
201-
$this->closed();
186+
$this->client->close();
187+
$this->closeWriteSide();
188+
while (!empty($this->recvChannelMap)) {
189+
foreach ($this->recvChannelMap as $index => $the_channel) {
190+
unset($this->recvChannelMap[$index]);
191+
$the_channel->close();
192+
}
193+
}
202194
});
203195

204196
// send wait
@@ -220,7 +212,7 @@ protected function start()
220212
$this->sendRetChannel->close();
221213

222214
$this->sendCid = 0;
223-
$this->closed();
215+
$this->closeReadSide();
224216
});
225217
}
226218

@@ -340,46 +332,25 @@ public function recv(int $streamId, float $timeout = null)
340332
return false;
341333
}
342334

343-
public function close(): void
335+
protected function closeReadSide(): void
344336
{
345-
if ($this->closing) {
346-
return;
337+
if ($this->recvCid > 0) {
338+
$this->client->close();
347339
}
348-
$this->closing = 2;
349-
// close write side first
350-
$this->sendChannel->close();
351-
$this->client->close();
352340
}
353341

354-
protected function closed(): void
342+
protected function closeWriteSide(): void
355343
{
356-
if ($this->closing > 0) {
357-
$this->closing--;
358-
}
359-
// close success and notify the close waiter
360-
if ($this->closeWaiter) {
361-
$closeWaiter = $this->closeWaiter;
362-
$this->closeWaiter = null;
363-
$closeWaiter->push(true);
344+
if ($this->sendCid > 0) {
345+
$this->sendChannel->close();
364346
}
365347
}
366348

367-
public function closeWait(): void
349+
public function close(): void
368350
{
369-
if ($this->closing) {
370-
return;
371-
}
372-
$this->closing = 2;
373-
$this->closeWaiter = $closeWaiter = new Channel;
374-
$n = 0;
375-
if ($this->recvCid > 0) {
376-
$n++;
377-
}
378-
if ($this->sendCid > 0) {
379-
$n++;
380-
}
381-
while ($n--) {
382-
$closeWaiter->pop();
383-
}
351+
$this->closed = true;
352+
// close write side first
353+
$this->closeWriteSide();
354+
$this->closeReadSide();
384355
}
385356
}

src/Grpc/VirtualClient.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,4 @@ public function close(): void
138138
// close non-static method body hook
139139
$this->client->close();
140140
}
141-
142-
public function closeWait(): void
143-
{
144-
// closeWait non-static method body hook
145-
$this->client->closeWait();
146-
}
147-
148-
}
141+
}

0 commit comments

Comments
 (0)