Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 700e48b

Browse files
committed
Update: default laravel mode is sandbox
1 parent d0a6236 commit 700e48b

File tree

11 files changed

+53
-271
lines changed

11 files changed

+53
-271
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"ext-posix": "*",
2323
"ext-sockets": "*",
2424
"ext-pdo": "*",
25-
"cloudtay/ripple": "^1.0.0",
26-
"cloudtay/ripple-http": "dev-main",
27-
"cloudtay/ripple-websocket": "dev-main"
25+
"cloudtay/ripple": "^1.0",
26+
"cloudtay/ripple-http": "^1.0",
27+
"cloudtay/ripple-websocket": "^1.0"
2828
},
2929
"require-dev": {
3030
"amphp/mysql": "^3.0",

example/workerman4.php

+14-24
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,44 @@
3434

3535
include_once __DIR__ . '/../vendor/autoload.php';
3636

37+
use Ripple\Driver\Workerman\AsyncTcpConnection;
3738
use Ripple\Driver\Workerman\Driver4;
38-
use Ripple\Utils\Output;
39-
use Workerman\Timer;
39+
use Ripple\Http\Guzzle;
40+
use Workerman\Connection\TcpConnection;
4041
use Workerman\Worker;
4142

4243
use function Co\async;
43-
use function Co\delay;
4444

4545
$worker = new Worker('tcp://127.0.0.1:28008');
4646
$worker->onWorkerStart = function () {
47-
$timerId = Timer::add(0.1, function () {
48-
Output::info("memory usage: " . \memory_get_usage());
49-
});
50-
51-
$timerId2 = Timer::add(1, function () {
52-
Output::info("memory usage: " . \memory_get_usage());
53-
\gc_collect_cycles();
54-
});
47+
$connect = new AsyncTcpConnection('ssl://www.google.com:443');
48+
$connect->onConnect = function (TcpConnection $connection) {
49+
\var_dump('Connected');
50+
$connection->send("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", true);
51+
};
5552

56-
delay(function () use ($timerId) {
57-
Timer::del($timerId);
58-
}, 3);
53+
$connect->onMessage = function (TcpConnection $connection, $data) {
54+
\var_dump($data);
55+
};
56+
$connect->connectViaProxy('socks5://127.0.0.1:1080');
5957
};
6058

6159
$worker->onMessage = function ($connection, $data) {
62-
// //方式1
63-
async(function ($r) use ($connection) {
60+
async(function () use ($connection) {
6461
\Co\sleep(3);
65-
6662
$fileContent = \Co\IO::File()->getContents(__FILE__);
67-
6863
$hash = \hash('sha256', $fileContent);
6964
$connection->send("[await] File content hash: {$hash}" . \PHP_EOL);
70-
71-
$r();
7265
});
7366

7467
//使用原生guzzle实现异步请求
7568
try {
76-
$response = \Ripple\Http\Guzzle::newClient()->get('https://www.baidu.com/');
69+
$response = Guzzle::newClient()->get('https://www.baidu.com/');
7770
\var_dump($response->getStatusCode());
7871
$connection->send("[async] Response status code: {$response->getStatusCode()}" . \PHP_EOL);
7972
} catch (Throwable $exception) {
8073
$connection->send("[async] Exception: {$exception->getMessage()}" . \PHP_EOL);
81-
8274
}
83-
84-
8575
$connection->send("say {$data}");
8676
};
8777

example/workerman5.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
include_once __DIR__ . '/../vendor/autoload.php';
3636

3737
use Ripple\Driver\Workerman\Driver5;
38+
use Ripple\Http\Guzzle;
3839
use Ripple\Utils\Output;
3940
use Workerman\Timer;
4041
use Workerman\Worker;
@@ -60,25 +61,20 @@
6061

6162
$worker->onMessage = function ($connection, $data) {
6263
// //方式1
63-
async(function ($r) use ($connection) {
64+
async(function () use ($connection) {
6465
\Co\sleep(3);
65-
6666
$fileContent = \Co\IO::File()->getContents(__FILE__);
67-
6867
$hash = \hash('sha256', $fileContent);
6968
$connection->send("[await] File content hash: {$hash}" . \PHP_EOL);
70-
71-
$r();
7269
});
7370

7471
//使用原生guzzle实现异步请求
7572
try {
76-
$response = \Ripple\Http\Guzzle::newClient()->get('https://www.baidu.com/');
73+
$response = Guzzle::newClient()->get('https://www.baidu.com/');
7774
\var_dump($response->getStatusCode());
7875
$connection->send("[async] Response status code: {$response->getStatusCode()}" . \PHP_EOL);
7976
} catch (Throwable $exception) {
8077
$connection->send("[async] Exception: {$exception->getMessage()}" . \PHP_EOL);
81-
8278
}
8379

8480
$connection->send("say {$data}");

src/Laravel/.env.example

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
PRP_HTTP_LISTEN=http://127.0.0.1:8008
22
PRP_HTTP_WORKERS=4
33
PRP_HTTP_RELOAD=0
4-
PRP_HTTP_SANDBOX=1

src/Laravel/Driver.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class Driver extends Command
8181
'HTTP_LISTEN' => 'string',
8282
'HTTP_WORKERS' => 'int',
8383
'HTTP_RELOAD' => 'bool',
84-
'HTTP_SANDBOX' => 'bool',
8584
'HTTP_ISOLATION' => 'bool',
8685
];
8786

@@ -236,9 +235,8 @@ private function start(): void
236235

237236
$listen = Config::get('ripple.HTTP_LISTEN', 'http://127.0.0.1:8008');
238237
$count = intval(Config::get('ripple.HTTP_WORKERS', 4));
239-
$sandbox = \Ripple\Driver\Utils\Config::value2bool(Config::get('ripple.HTTP_SANDBOX', 1));
240238

241-
$this->manager->addWorker(new Worker($listen, $count, $sandbox));
239+
$this->manager->addWorker(new Worker($listen, $count));
242240
$this->manager->run();
243241
}
244242

src/Laravel/Worker.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ class Worker extends \Ripple\Worker
7878
/**
7979
* @param string $address
8080
* @param int $count
81-
* @param bool $sandbox
8281
*/
8382
public function __construct(
8483
private readonly string $address = 'http://127.0.0.1:8008',
8584
int $count = 4,
86-
private readonly bool $sandbox = true,
8785
) {
8886
$this->name = 'http-server';
8987
$this->count = $count;
@@ -156,7 +154,7 @@ public function boot(): void
156154
$this->server->onRequest(function (
157155
Request $request
158156
) {
159-
$application = $this->sandbox ? clone $this->application : $this->application;
157+
$application = clone $this->application;
160158
/*** @var Kernel $kernel */
161159
$kernel = $application->make(Kernel::class);
162160

@@ -201,10 +199,8 @@ public function boot(): void
201199
} catch (Throwable $e) {
202200
$this->dispatchEvent($application, new WorkerErrorOccurred($this->application, $application, $e));
203201
} finally {
204-
if ($this->sandbox) {
205-
unset($application);
206-
}
207202
unset($laravelRequest, $response, $laravelResponse, $kernel);
203+
unset($application);
208204
}
209205
});
210206
$this->server->listen();

src/Laravel/config/ripple.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@
3838
return [
3939
'HTTP_LISTEN' => Env::get('PRP_HTTP_LISTEN', 'http://127.0.0.1:8008'),
4040
'HTTP_WORKERS' => Env::get('PRP_HTTP_WORKERS', 4),
41-
'HTTP_RELOAD' => Env::get('PRP_HTTP_RELOAD', 0),
42-
'HTTP_SANDBOX' => Env::get('PRP_HTTP_SANDBOX', 1),
41+
'HTTP_RELOAD' => Env::get('PRP_HTTP_RELOAD', 0)
4342
];

src/Workerman/Driver4.php

+25-22
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
namespace Ripple\Driver\Workerman;
3636

3737
use Closure;
38-
use Co\System;
39-
use Ripple\Stream;
4038
use Ripple\Kernel;
39+
use Ripple\Process;
40+
use Ripple\Stream;
4141
use Throwable;
4242
use Workerman\Events\EventInterface;
4343
use Workerman\Worker;
@@ -73,7 +73,10 @@ class Driver4 implements EventInterface
7373
protected array $_timer = [];
7474

7575
/*** @var array */
76-
protected array $_fd2ids = [];
76+
protected array $_fd2RIDs = [];
77+
78+
/*** @var array */
79+
protected array $_fd2WIDs = [];
7780

7881
/*** @var array */
7982
protected array $_signal2ids = [];
@@ -113,12 +116,10 @@ public function add($fd, $flag, $func, $args = []): bool|int
113116
}
114117
}
115118

116-
// 未找到回调
117119
if (!isset($closure)) {
118120
return false;
119121
}
120122

121-
// 注册信号处理器
122123
$id = onSignal($fd, $closure);
123124
$this->_signal2ids[$fd] = string2int($id);
124125
return string2int($id);
@@ -127,47 +128,41 @@ public function add($fd, $flag, $func, $args = []): bool|int
127128
}
128129

129130
case EventInterface::EV_TIMER:
130-
// 定时器
131131
$this->_timer[] = $timerId = repeat(function () use ($func, $args) {
132132
try {
133133
call_user_func_array($func, $args);
134134
} catch (Throwable $e) {
135135
Worker::stopAll(250, $e);
136136
}
137137
}, $fd);
138-
139138
return string2int($timerId);
140139

141140
case EventInterface::EV_TIMER_ONCE:
142-
// 一次性定时器
143141
$this->_timer[] = $timerId = delay(function () use ($func, $args) {
144142
try {
145143
call_user_func_array($func, $args);
146144
} catch (Throwable $e) {
147145
Worker::stopAll(250, $e);
148146
}
149147
}, $fd);
150-
151148
return string2int($timerId);
152149

153150
case EventInterface::EV_READ:
154-
// 读事件
155151
$stream = new Stream($fd);
156152
$eventId = $stream->onReadable(function (Stream $stream) use ($func) {
157153
$func($stream->stream);
158154
});
159155

160-
$this->_fd2ids[$stream->id][] = string2int($eventId);
156+
$this->_fd2RIDs[$stream->id][] = string2int($eventId);
161157
return string2int($eventId);
162158

163159
case EventInterface::EV_WRITE:
164-
// 写事件
165160
$stream = new Stream($fd);
166-
$eventId = $stream->onWritable(function (Stream $stream) use ($func) {
161+
$eventId = $stream->onWriteable(function (Stream $stream) use ($func) {
167162
$func($stream->stream);
168163
});
169164

170-
$this->_fd2ids[$stream->id][] = string2int($eventId);
165+
$this->_fd2WIDs[$stream->id][] = string2int($eventId);
171166
return string2int($eventId);
172167
}
173168
return false;
@@ -192,19 +187,26 @@ public function del($fd, $flag): void
192187
}
193188

194189
if ($flag === EventInterface::EV_READ || $flag === EventInterface::EV_WRITE) {
195-
// 取消读写事件监听
190+
if (!$fd) {
191+
return;
192+
}
193+
196194
$streamId = get_resource_id($fd);
197-
if (isset($this->_fd2ids[$streamId])) {
198-
foreach ($this->_fd2ids[$streamId] as $id) {
199-
$this->cancel($id);
195+
if ($flag === EventInterface::EV_READ) {
196+
foreach ($this->_fd2RIDs[$streamId] ?? [] as $eventId) {
197+
cancel(int2string($eventId));
198+
}
199+
unset($this->_fd2RIDs[$streamId]);
200+
} else {
201+
foreach ($this->_fd2WIDs[$streamId] ?? [] as $eventId) {
202+
cancel(int2string($eventId));
200203
}
201-
unset($this->_fd2ids[$streamId]);
204+
unset($this->_fd2WIDs[$streamId]);
202205
}
203206
return;
204207
}
205208

206209
if ($flag === EventInterface::EV_SIGNAL) {
207-
// 取消信号监听
208210
$signalId = $this->_signal2ids[$fd] ?? null;
209211
if ($signalId) {
210212
$this->cancel($signalId);
@@ -247,8 +249,9 @@ public function loop(): void
247249
Driver4::$baseProcessId = (Kernel::getInstance()->supportProcessControl() ? getmypid() : posix_getpid());
248250
} elseif (Driver4::$baseProcessId !== (Kernel::getInstance()->supportProcessControl() ? getmypid() : posix_getpid())) {
249251
Driver4::$baseProcessId = (Kernel::getInstance()->supportProcessControl() ? getmypid() : posix_getpid());
250-
cancelAll();
251-
System::Process()->forkedTick();
252+
Process::getInstance()->processedInMain(static function () {
253+
Process::getInstance()->forgetEvents();
254+
});
252255
}
253256
wait();
254257

src/Workerman/Driver5.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@
3434

3535
namespace Ripple\Driver\Workerman;
3636

37-
use Co\System;
3837
use Revolt\EventLoop;
3938
use Ripple\Kernel;
39+
use Ripple\Process;
4040
use Workerman\Events\EventInterface;
4141

4242
use function array_shift;
4343
use function Co\cancel;
44-
use function Co\cancelAll;
4544
use function Co\delay;
4645
use function Co\repeat;
4746
use function Co\stop;
@@ -95,9 +94,9 @@ public function run(): void
9594
Driver5::$baseProcessId = (getmypid());
9695
} elseif (Driver5::$baseProcessId !== (getmypid())) {
9796
Driver5::$baseProcessId = (getmypid());
98-
99-
cancelAll();
100-
System::Process()->forkedTick();
97+
Process::getInstance()->processedInMain(static function () {
98+
Process::getInstance()->forgetEvents();
99+
});
101100
}
102101

103102
wait();

0 commit comments

Comments
 (0)