From a9f2da52358a8dea794f1005729b6ad1c084e9ba Mon Sep 17 00:00:00 2001 From: wpjscc Date: Mon, 13 Jan 2025 18:46:51 +0800 Subject: [PATCH] stream support queue --- src/Bandwidth.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Bandwidth.php b/src/Bandwidth.php index b210199..e4bfb54 100644 --- a/src/Bandwidth.php +++ b/src/Bandwidth.php @@ -66,13 +66,17 @@ public function file(string $path, $p = 0, $length = -1) public function stream($stream) { $_stream = new \React\Stream\ThroughStream(); - $stream->on('data', function ($data) use ($_stream) { - $this->concurrent->concurrent(function() use ($_stream, $data){ + + $concurrent = $this->queue ? $this->concurrent : new Concurrent(1); + + $stream->on('data', function ($data) use ($_stream, $concurrent) { + $concurrent->concurrent(function() use ($_stream, $data){ return $this->bucket->removeTokens(1024 * strlen($data))->then(function () use ($_stream, $data) { $_stream->write($data); }); }); }); + return $_stream; }