-
-
Notifications
You must be signed in to change notification settings - Fork 252
zstd-stream support #1433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
zstd-stream support #1433
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,11 +70,14 @@ | |
| use React\Promise\PromiseInterface; | ||
| use React\Socket\Connector as SocketConnector; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Zstd\UnCompress\Context as ZstdContext; | ||
|
|
||
| use function React\Async\coroutine; | ||
| use function React\Promise\all; | ||
| use function React\Promise\reject; | ||
| use function React\Promise\resolve; | ||
| use function Zstd\uncompress_init; | ||
| use function Zstd\uncompress_add; | ||
|
|
||
| /** | ||
| * The Discord client class. | ||
|
|
@@ -314,9 +317,16 @@ class Discord | |
| /** | ||
| * zlib decompressor. | ||
| * | ||
| * @var \InflateContext|false | ||
| * @var \InflateContext|false Zlib decompression context | ||
| */ | ||
| protected $zlibDecompressor; | ||
| protected $zlibDecompressor = false; | ||
|
|
||
| /** | ||
| * zstd decompressor. | ||
| * | ||
| * @var ZstdContext|false Zstd decompression context when ext-zstd is available | ||
| */ | ||
| protected $zstdDecompressor = false; | ||
|
|
||
| /** | ||
| * Tracks the number of payloads the client has sent in the past 60 seconds. | ||
|
|
@@ -739,7 +749,14 @@ public function handleWsMessage(Message $message): void | |
| $payload = $message->getPayload(); | ||
|
|
||
| if ($message->isBinary()) { | ||
| if ($this->zlibDecompressor) { | ||
| if ($this->zstdDecompressor !== false) { | ||
| $decompressed = uncompress_add($this->zstdDecompressor, $payload); | ||
| if ($decompressed !== false) { | ||
| $this->processWsMessage($decompressed); | ||
| } else { | ||
| $this->logger->error('failed to decompress zstd payload', ['payload' => $payload, 'payload hex' => bin2hex($payload)]); | ||
| } | ||
| } elseif ($this->zlibDecompressor !== false) { | ||
| $this->payloadBuffer .= $payload; | ||
|
|
||
| if ($message->getPayloadLength() < 4 || substr($payload, -4) !== "\x00\x00\xff\xff") { | ||
|
|
@@ -1652,10 +1669,14 @@ protected function buildParams(Deferred $deferred, string $gateway, ?SessionStar | |
| ]; | ||
|
|
||
| if ($this->useTransportCompression) { | ||
| if ($this->zlibDecompressor = inflate_init(ZLIB_ENCODING_DEFLATE)) { | ||
| // Prefer zstd-stream if available (better compression), fallback to zlib-stream | ||
| if (extension_loaded('zstd') && ($this->zstdDecompressor = uncompress_init())) { | ||
| $params['compress'] = 'zstd-stream'; | ||
| $this->logger->debug('using zstd-stream compression'); | ||
| } elseif ($this->zlibDecompressor = inflate_init(ZLIB_ENCODING_DEFLATE)) { | ||
| $params['compress'] = 'zlib-stream'; | ||
| $this->logger->debug('using zlib-stream compression'); | ||
|
Comment on lines
1671
to
+1678
|
||
| } | ||
| // @todo: add support for zstd-stream | ||
| } | ||
|
|
||
| $query = http_build_query($params); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.