-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCoroutineTest.php
135 lines (119 loc) · 4.43 KB
/
CoroutineTest.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
<?php
/**
* 协程示例控制器
*
* @author [email protected]
* @copyright Chengdu pinguo Technology Co.,Ltd.
*/
namespace App\Controllers;
use PG\MSF\Controllers\Controller;
use PG\MSF\Client\Http\Client;
class CoroutineTest extends Controller
{
/**
* 异步回调的方式实现 C || (A && B)
*/
public function actionCallBackMode()
{
$client = new \swoole_redis;
$client->connect('127.0.0.1', 6379, function (\swoole_redis $client, $result) {
$client->get('apiCacheForABCallBack', function (\swoole_redis $client, $result) {
if (!$result) {
swoole_async_dns_lookup("www.baidu.com", function($host, $ip) use ($client) {
$cli = new \swoole_http_client($ip, 443, true);
$cli->setHeaders([
'Host' => $host,
]);
$apiA = "";
$cli->get('/', function ($cli) use ($client, $apiA) {
$apiA = $cli->body;
swoole_async_dns_lookup("www.qiniu.com", function($host, $ip) use ($client, $apiA) {
$cli = new \swoole_http_client($ip, 443, true);
$cli->setHeaders([
'Host' => $host,
]);
$apiB = "";
$cli->get('/', function ($cli) use ($client, $apiA, $apiB) {
$apiB = $cli->body;
if ($apiA && $apiB) {
$client->set('apiCacheForABCallBack', $apiA . $apiB, function (\swoole_redis $client, $result) {});
$this->outputJson($apiA . $apiB);
} else {
$this->outputJson('', 'error');
}
});
});
});
});
} else {
$this->outputJson($result);
}
});
});
}
/**
* 协程的方式实现 C || (A && B)
*/
public function actionCoroutineMode()
{
// 从Redis获取get apiCacheForABCoroutine
$response = yield $this->getRedisPool('p1')->get('apiCacheForABCoroutine');
if (!$response) {
// 从远程拉取数据
$request = [
'https://www.baidu.com/',
'https://www.qiniu.com/',
];
/**
* @var Client $client
*/
$client = $this->getObject(Client::class);
$results = yield $client->goConcurrent($request);
// 写入redis
$this->getRedisPool('p1')->set('apiCacheForABCoroutine', $results[0]['body'] . $results[1]['body'])->break();
$response = $results[0]['body'] . $results[1]['body'];
}
// 响应结果
$this->output($response);
}
// 姿势一
public function actionNested()
{
$result = [];
/**
* @var Client $client1
*/
$client1 = $this->getObject(Client::class, ['http://www.baidu.com/']);
yield $client1->goDnsLookup();
/**
* @var Client $client2
*/
$client2 = $this->getObject(Client::class, ['http://www.qq.com/']);
yield $client2->goDnsLookup();
$result[] = yield $client1->goGet('/');
$result[] = yield $client2->goGet('/');
$this->outputJson([strlen($result[0]['body']), strlen($result[1]['body'])]);
}
// 姿势二
public function actionUnNested()
{
$result = [];
/**
* @var Client $client1
*/
$client1 = $this->getObject(Client::class, ['http://www.baidu.com/']);
$dns[] = $client1->goDnsLookup();
/**
* @var Client $client2
*/
$client2 = $this->getObject(Client::class, ['http://www.qq.com/']);
$dns[] = $client2->goDnsLookup();
yield $dns[0];
yield $dns[1];
$req[] = $client1->goGet('/');
$req[] = $client2->goGet('/');
$result[] = yield $req[0];
$result[] = yield $req[1];
$this->outputJson([strlen($result[0]['body']), strlen($result[1]['body'])]);
}
}