Skip to content

Commit 95b5a97

Browse files
committed
Fix Swoole error
1 parent c3d47b5 commit 95b5a97

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

frameworks/swoole/PostgreSQL.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ class PostgreSQL
77

88
public static function init(): void
99
{
10-
$parts = parse_url(getenv('DATABASE_URL'));
10+
$dsn = getenv('DATABASE_URL');
11+
if (!$dsn) {
12+
return;
13+
}
14+
15+
$parts = parse_url($dsn);
1116
$host = $parts['host'] ?? 'localhost';
1217
$port = $parts['port'] ?? 5432;
1318
$db = ltrim($parts['path'] ?? '/benchmark', '/');

frameworks/swoole/meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"pipelined",
1313
"limited-conn",
1414
"json",
15+
"json-tls",
1516
"json-comp",
1617
"upload",
1718
"static",
@@ -20,4 +21,4 @@
2021
"api-16",
2122
"async-db"
2223
]
23-
}
24+
}

frameworks/swoole/swoole.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535

3636
$http = new Server('0.0.0.0', 8080);
3737
$http->set([
38-
'worker_num' => swoole_cpu_num(),
39-
'enable_reuse_port' => true,
40-
'enable_coroutine' => false,
41-
'package_max_length' => 30 * 1024 * 1024,
42-
'http_compression_level' => 0
38+
'worker_num' => swoole_cpu_num(),
39+
'enable_reuse_port' => true,
40+
'enable_coroutine' => false,
41+
'package_max_length' => 30 * 1024 * 1024
4342
]);
4443

4544
$http->on('workerStart', function (Server $server, int $workerId) {
@@ -67,13 +66,13 @@
6766

6867
if (preg_match('#^/json/(\d+)$#', $path, $matches)) {
6968
$count = min((int)$matches[1], count($dataset));
70-
$m = (int)($request->get['m'] ?? 1);
69+
$m = (int)($request->get['m'] ?? 1);
7170
if ($m === 0) $m = 1;
7271
$items = [];
7372
for ($i = 0; $i < $count; $i++) {
74-
$item = $dataset[$i];
73+
$item = $dataset[$i];
7574
$item['total'] = $item['price'] * $item['quantity'] * $m;
76-
$items[] = $item;
75+
$items[] = $item;
7776
}
7877
$response->header['Content-Type'] = 'application/json';
7978
$response->end(json_encode(['items' => $items, 'count' => $count], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
@@ -120,11 +119,40 @@
120119

121120
$port = $http->listen('0.0.0.0', 8443, SWOOLE_TCP | SWOOLE_SSL);
122121
$port->set([
123-
'open_http2_protocol' => true,
124-
'ssl_cert_file' => '/certs/server.crt',
125-
'ssl_key_file' => '/certs/server.key',
126-
'package_max_length' => 30 * 1024 * 1024,
127-
'http_compression_level' => 0,
122+
'open_http2_protocol' => true,
123+
'ssl_cert_file' => '/certs/server.crt',
124+
'ssl_key_file' => '/certs/server.key',
125+
'package_max_length' => 30 * 1024 * 1024
128126
]);
129127

128+
$port2 = $http->listen('0.0.0.0', 8081, SWOOLE_TCP | SWOOLE_SSL);
129+
$port2->set([
130+
'ssl_cert_file' => '/certs/server.crt',
131+
'ssl_key_file' => '/certs/server.key',
132+
'package_max_length' => 30 * 1024 * 1024,
133+
'http_compression' => false,
134+
'open_http_protocol' => true,
135+
]);
136+
$port2->on('request', function (Request $request, Response $response) use ($dataset) {
137+
$path = $request->server['request_uri'];
138+
if (preg_match('#^/json/(\d+)$#', $path, $matches)) {
139+
$count = min((int)$matches[1], count($dataset));
140+
$m = (int)($request->get['m'] ?? 1);
141+
if ($m === 0) $m = 1;
142+
$items = [];
143+
for ($i = 0; $i < $count; $i++) {
144+
$item = $dataset[$i];
145+
$item['total'] = $item['price'] * $item['quantity'] * $m;
146+
$items[] = $item;
147+
}
148+
$response->header['Content-Type'] = 'application/json';
149+
$response->end(json_encode(['items' => $items, 'count' => $count], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
150+
return;
151+
}
152+
153+
$response->status(404);
154+
$response->header['Content-Type'] = 'text/plain';
155+
$response->end('404 Not Found');
156+
});
157+
130158
$http->start();

0 commit comments

Comments
 (0)