Skip to content

Commit e8289a3

Browse files
feat: PHP + Promise
1 parent 3831c1d commit e8289a3

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
composer.lock
2+
package-lock.json
23
vendor
3-
logs
4+
node_modules
5+
logs

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,29 @@
88

99
- **使用场景:**
1010

11-
常见于Webhook的实现中
11+
消息推送,但不考虑结果状态
1212

1313
- **代码片段**
1414

15-
> framework/src/Request.class.php
15+
> framework/src/Request.class.php
1616
1717
### 2.2 **Promise**
1818

19+
- **相关简介:**
20+
21+
22+
1923
- **使用场景:**
2024

21-
常见于Webhook的实现中
25+
并发获取资源
2226

2327
- **代码片段**
2428

25-
> framework/src/Request.class.php
29+
> framework/src/Request.class.php
2630
2731
### 2.3 **Redis pipeline**
32+
33+
2834
### 2.4 **AMQP**
2935
### 2.5 **MySQL BatchInsert**
3036
### 2.6 **Deamon Process**

app/index.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,30 @@
1212

1313
use Monolog\Logger;
1414
use Monolog\Handler\StreamHandler;
15+
use GuzzleHttp\Client;
16+
use GuzzleHttp\Promise;
17+
use Psr\Http\Message\ResponseInterface;
1518

1619
$loglevel = defined('RUN_MODE') && RUN_MODE === 'production' ? Logger::NOTICE : Logger::DEBUG;
1720
$logger = new Logger('cobolphp');
1821
$logger->pushHandler(new StreamHandler(LOG_PATH . '/access.log', $loglevel));
1922

2023
// 非阻塞请求
2124
$request = new \cobolphp\Request($logger);
22-
$request->asyncRequest('http://shujuguan.cn');
25+
$request->asyncRequest('http://localhost:8008');
2326

2427
// Promise
25-
28+
$client = new Client();
29+
$promises = [];
30+
for ($i = 0; $i < 10; ++$i) {
31+
$promise = $client->requestAsync(
32+
'GET',
33+
'http://localhost:8008'
34+
);
35+
$promise
36+
->then(function (ResponseInterface $response) {
37+
echo $response->getBody() . PHP_EOL;
38+
});
39+
$promises[] = $promise;
40+
}
41+
Promise\all($promises)->wait();

build/devserver.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const http = require('http');
2+
const sleep = require('sleep-async')();
3+
4+
const server = http.createServer();
5+
const sleepMiliSecond = 1000;
6+
7+
server.on('request', function(req, res) {
8+
console.log('Received.');
9+
10+
sleep.sleep(sleepMiliSecond, function() {
11+
res.writeHead(200, {'Content-Type': 'text/json'});
12+
res.write('{"status": "ok"}');
13+
res.end();
14+
});
15+
});
16+
17+
server.listen(8008, 'localhost');

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": {
3-
"monolog/monolog": "^1.23"
3+
"monolog/monolog": "^1.23",
4+
"guzzlehttp/guzzle": "^6.3"
45
},
56
"require-dev": {
67
"psecio/iniscan": "^3.6"

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "cobolphp/framework",
3+
"description": "Strive to break through",
4+
"dependencies": {
5+
"sleep-async": "^1.0.5"
6+
}
7+
}

0 commit comments

Comments
 (0)