-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from Kraigie/jb3/zstd
Support zstd-stream gateway compression
- Loading branch information
Showing
9 changed files
with
233 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Gateway Compression | ||
|
||
Nostrum supports either the `zlib-stream` or `zstd-stream` gateway compression | ||
methods, as documented | ||
[here](https://discord.com/developers/docs/topics/gateway#encoding-and-compression) | ||
|
||
Most users are fine to leave the `gateway_compression` configuration option set | ||
to `:zlib` (default), but users looking for a potential reduction in payload | ||
sizes from the Discord gateway can optionally set `:zstd` here. | ||
|
||
## Using `:zstd` compression | ||
|
||
Using `:zstd` depends on the [`:ezstd`](https://hex.pm/packages/ezstd) library, | ||
so you will have to add this dependency to your `mix.exs` file: | ||
|
||
```elixir | ||
defp deps do | ||
[ | ||
{:nostrum, ...}, | ||
{:ezstd, "~> 1.1"} # new dependency | ||
] | ||
end | ||
``` | ||
|
||
|
||
> #### `:ezstd` NIFs {: .info} | ||
> | ||
> Some functionality of `:ezstd` depends on Erlang NIFs (Natively Implemented | ||
> Functions). This means that a proper compiler installation as well as other | ||
> build tools like `git` may be necessary at the stage where you compile your | ||
> dependencies. | ||
> | ||
> It may be useful to run `mix deps.compile` in any build systems to ensure that | ||
> your application does not need build utilities in the built application image. | ||
Once you have this additional dependency installed in your project, set the | ||
`:nostrum`, `:gateway_compression` configuration option to `:zstd` and Nostrum | ||
should pick up on it. | ||
|
||
You will need to run `mix deps.get` and `mix deps.compile` to install and | ||
compile the new `:ezstd` dependency. | ||
|
||
> #### Nostrum detection of `:ezstd` {: .tip} | ||
> | ||
> Since the check for `:ezstd` takes place when you compile Nostrum, you might | ||
> need to run `mix deps.compile --force nostrum` to ensure that Nostrum is | ||
> recompiled and recognises the newly installed `:ezstd` dependency. | ||
> | ||
> Not doing this may mean that your compiled Nostrum version is still using | ||
> dummy handlers that will error out even when `:ezstd` is installed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Nostrum.Shard.Session.Compression do | ||
@moduledoc """ | ||
A behaviour for compression methods supported by the Discord gateway to implement. | ||
See the modules nested under this behaviour for reference implementations. | ||
> ### Internal module {: .info} | ||
> | ||
> This module is intended for exclusive usage inside of nostrum, and is | ||
> documented for completeness and people curious to look behind the covers. | ||
""" | ||
|
||
@doc """ | ||
Create a new compression context that can be passed as an argument to other | ||
methods within the behaviour to inflate data or reset the context to a | ||
blank state. | ||
""" | ||
@callback create_context() :: reference() | ||
|
||
@doc """ | ||
Decompress a frame received from Discord over the gateway. Should return an | ||
iolist of the decompressed data. | ||
""" | ||
@callback inflate(reference(), iodata()) :: iolist() | ||
|
||
@doc """ | ||
Reset a decompression context to a blank slate, this is useful after a websocket | ||
resume has taken place or something similar requiring the reset of the state | ||
for a shard. | ||
""" | ||
@callback reset_context(reference()) :: reference() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Nostrum.Shard.Session.Compression.Zlib do | ||
@moduledoc """ | ||
Implementation of compression methods for the `zlib` compression algorithm. | ||
> ### Internal module {: .info} | ||
> | ||
> This module is intended for exclusive usage inside of nostrum, and is | ||
> documented for completeness and people curious to look behind the covers. | ||
""" | ||
|
||
@behaviour Nostrum.Shard.Session.Compression | ||
|
||
@spec create_context() :: :zlib.zstream() | ||
def create_context do | ||
context = :zlib.open() | ||
:zlib.inflateInit(context) | ||
|
||
context | ||
end | ||
|
||
@spec inflate(:zlib.zstream(), iodata()) :: iolist() | ||
def inflate(ctx, frame) do | ||
:zlib.inflate(ctx, frame) | ||
end | ||
|
||
@spec reset_context(:zlib.zstream()) :: :zlib.zstream() | ||
def reset_context(ctx) do | ||
:zlib.inflateReset(ctx) | ||
|
||
ctx | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Nostrum.Shard.Session.Compression.Zstd do | ||
@moduledoc """ | ||
Implementation of compression methods for the `zstd` compression algorithm. | ||
> ### Internal module {: .info} | ||
> | ||
> This module is intended for exclusive usage inside of nostrum, and is | ||
> documented for completeness and people curious to look behind the covers. | ||
""" | ||
|
||
@behaviour Nostrum.Shard.Session.Compression | ||
|
||
def check_available! do | ||
if not Code.ensure_loaded?(:ezstd) do | ||
zstd_missing() | ||
end | ||
end | ||
|
||
@spec zstd_missing() :: no_return | ||
defp zstd_missing do | ||
raise ArgumentError, """ | ||
Cannot use the :zstd gateway compression option without optional dependency :ezstd. | ||
See https://kraigie.github.io/nostrum/gateway_compression.html for more information. | ||
""" | ||
end | ||
|
||
if Code.ensure_loaded?(:ezstd) do | ||
@zstd_buffer_size 16_000 | ||
|
||
@spec create_context() :: reference() | {:error, any()} | ||
def create_context do | ||
:ezstd.create_decompression_context(@zstd_buffer_size) | ||
end | ||
|
||
@spec inflate(reference(), iodata()) :: iolist() | {:error, any()} | ||
def inflate(ctx, frame) do | ||
:ezstd.decompress_streaming(ctx, frame) | ||
end | ||
|
||
@spec reset_context(reference()) :: reference() | ||
def reset_context(_ctx) do | ||
create_context() | ||
end | ||
else | ||
@spec create_context() :: reference() | {:error, any()} | ||
def create_context do | ||
zstd_missing() | ||
end | ||
|
||
@spec inflate(reference(), iodata()) :: iodata() | {:error, any()} | ||
def inflate(_ctx, _frame) do | ||
zstd_missing() | ||
end | ||
|
||
@spec reset_context(reference()) :: reference() | ||
def reset_context(_ctx) do | ||
zstd_missing() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters