@marcel91200 / caddy Mirror · read-only / commits / 03e0a010
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 14:30 UTC

encode: Configurable compression level for zstd (#6140)

* Add zstd compression level support * Refactored zstd levels to string arguments fastest, default, better, best * Add comment with list of all available levels * Corrected data types for config --------- Co-authored-by: Evgeny Blinov <e.a.blinov@gmail.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

dev-polymer committed Apr 16, 2024 at 03:21 UTC 03e0a010d1bfad6b15d9d0abba9430e77dc43e1c
1 file changed +48 -2
modules/caddyhttp/encode/zstd/zstd.go
+48 -2
@@ -15,6 +15,8 @@
15 package caddyzstd
16
17 import (
18 + "fmt"
19 +
20 "github.com/klauspost/compress/zstd"
21
22 "github.com/caddyserver/caddy/v2"
@@ -27,7 +29,13 @@ func init() {
29 }
30
31 // Zstd can create Zstandard encoders.
30 -type Zstd struct{}
32 +type Zstd struct {
33 + // The compression level. Accepted values: fastest, better, best, default.
34 + Level string `json:"level,omitempty"`
35 +
36 + // Compression level refer to type constants value from zstd.SpeedFastest to zstd.SpeedBestCompression
37 + level zstd.EncoderLevel
38 +}
39
40 // CaddyModule returns the Caddy module information.
41 func (Zstd) CaddyModule() caddy.ModuleInfo {
@@ -39,6 +47,37 @@ func (Zstd) CaddyModule() caddy.ModuleInfo {
47
48 // UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
49 func (z *Zstd) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
50 + d.Next() // consume option name
51 + if !d.NextArg() {
52 + return nil
53 + }
54 + levelStr := d.Val()
55 + if ok, _ := zstd.EncoderLevelFromString(levelStr); !ok {
56 + return d.Errf("unexpected compression level, use one of '%s', '%s', '%s', '%s'",
57 + zstd.SpeedFastest,
58 + zstd.SpeedBetterCompression,
59 + zstd.SpeedBestCompression,
60 + zstd.SpeedDefault,
61 + )
62 + }
63 + z.Level = levelStr
64 + return nil
65 +}
66 +
67 +// Provision provisions z's configuration.
68 +func (z *Zstd) Provision(ctx caddy.Context) error {
69 + if z.Level == "" {
70 + z.Level = zstd.SpeedDefault.String()
71 + }
72 + var ok bool
73 + if ok, z.level = zstd.EncoderLevelFromString(z.Level); !ok {
74 + return fmt.Errorf("unexpected compression level, use one of '%s', '%s', '%s', '%s'",
75 + zstd.SpeedFastest,
76 + zstd.SpeedDefault,
77 + zstd.SpeedBetterCompression,
78 + zstd.SpeedBestCompression,
79 + )
80 + }
81 return nil
82 }
83
@@ -51,7 +90,13 @@ func (z Zstd) NewEncoder() encode.Encoder {
90 // The default of 8MB for the window is
91 // too large for many clients, so we limit
92 // it to 128K to lighten their load.
54 - writer, _ := zstd.NewWriter(nil, zstd.WithWindowSize(128<<10), zstd.WithEncoderConcurrency(1), zstd.WithZeroFrames(true))
93 + writer, _ := zstd.NewWriter(
94 + nil,
95 + zstd.WithWindowSize(128<<10),
96 + zstd.WithEncoderConcurrency(1),
97 + zstd.WithZeroFrames(true),
98 + zstd.WithEncoderLevel(z.level),
99 + )
100 return writer
101 }
102
@@ -59,4 +104,5 @@ func (z Zstd) NewEncoder() encode.Encoder {
104 var (
105 _ encode.Encoding = (*Zstd)(nil)
106 _ caddyfile.Unmarshaler = (*Zstd)(nil)
107 + _ caddy.Provisioner = (*Zstd)(nil)
108 )