-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.php
128 lines (103 loc) · 4.2 KB
/
app.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
<?php
require __DIR__ . '/vendor/autoload.php';
use App\Services\BucketManager;
use React\Stream\ThroughStream;
use Wpjscc\React\Limiter\TokenBucket;
use function Wpjscc\React\Limiter\getMilliseconds;
use React\EventLoop\Loop;
use function React\Async\async;
use function React\Async\await;
use App\Services\FileBandwidthService;
DEFINE('KB', 50);
define('BURST_RATE', 1024 * 1024 * 3 * KB);
define('FILL_RATE', 1024 * 1024 * KB);
$fileBandwidthService = new FileBandwidthService(1024*1024*getParam('--filekb', 300), 1024*1024* getParam('--filekb', 100), 1000);
$fileBandwidthService->run();
if (getParam('--every-minute-times')) {
BucketManager::setNumber((int) getParam('--every-minute-times'));
}
BucketManager::addBucket(BucketManager::getNumber());
BucketManager::initLimiter(BucketManager::getNumber(), 'minute');
$app = new FrameworkX\App(
new React\Http\Middleware\LimitConcurrentRequestsMiddleware(1), // 100 concurrent buffering handlers
new React\Http\Middleware\RequestBodyBufferMiddleware(0.5 * 1024 * 1024), // 2 MiB per request
);
$app->get('/', function () use ($fileBandwidthService) {
// return \React\Http\Message\Response::html(file_get_contents(__DIR__ . '/chat.html'));
$stream = new ThroughStream;
$fileBandwidthService->addStream($stream, __DIR__ . '/chat.html');
return new React\Http\Message\Response(
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/html; charset=utf-8',
),
$stream
);
});
$app->get('/health', function () {
return \React\Http\Message\Response::json([
'is_healthy' => true,
]);
});
$app->get('/chatgpt', new App\Controllers\ChatGPTController());
$app->get('/assets/{path}', function (Psr\Http\Message\ServerRequestInterface $request) use ($fileBandwidthService) {
$path = '/assets/' . $request->getAttribute('path');
if (file_exists(__DIR__ . $path)) {
$fileType = getFileType(__DIR__ . $path);
$contentType = 'application/javascript';
if ($fileType == 'css') {
$contentType = 'text/css';
}
$p = 0;
$size = filesize(__DIR__ . $path);
$bucket = new TokenBucket(BURST_RATE, FILL_RATE, 'sec');
$bucket->setContent(BURST_RATE);
$stream = new ThroughStream;
$async = async(function () use (&$async, $stream, $path, &$p, $size, $bucket) {
if ($size/1024 < KB) {
await($bucket->removeTokens(1024 * 1024 * ceil($size/1024)));
$stream->end(file_get_contents(__DIR__ . $path));
} else {
if (($size-$p)/1024 < KB) {
$start = getMilliseconds();
await($bucket->removeTokens(1024 * 1024 * ceil(($size-$p)/1024)));
$end = getMilliseconds();
var_dump($end-$start, ($size-$p));
$fp = fopen(__DIR__ . $path, 'r');
fseek($fp, $p);
$content = fread($fp, 1024 * KB);
$p += strlen($content);
fclose($fp);
$stream->end($content);
} else {
$start = getMilliseconds();
await($bucket->removeTokens(1024 * 1024 * KB));
$end = getMilliseconds();
var_dump($end-$start, KB);
$fp = fopen(__DIR__ . $path, 'r');
fseek($fp, $p);
$content = fread($fp, 1024 * KB);
$p += strlen($content);
fclose($fp);
if ($p >= $size) {
$stream->end($content);
} else {
$stream->write($content);
$async();
}
}
}
});
// Loop::addTimer(0.001, $async);
$fileBandwidthService->addStream($stream, __DIR__ . $path);
return new React\Http\Message\Response(
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => $contentType,
'Cache-Control' => 'max-age=3600'
),
$stream
);
}
});
$app->run();