-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(output-http): Add more compression options (#341)
- Loading branch information
Showing
3 changed files
with
117 additions
and
33 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
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 |
---|---|---|
@@ -1,8 +1,111 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"compress/zlib" | ||
"net/http" | ||
|
||
"github.com/golang/snappy" | ||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
type CompressionStrategy string | ||
|
||
var ( | ||
CompressionStrategyNone CompressionStrategy = "none" | ||
CompressionStrategyGzip CompressionStrategy = "gzip" | ||
CompressionStrategyNone CompressionStrategy = "none" | ||
CompressionStrategyGzip CompressionStrategy = "gzip" | ||
CompressionStrategyZstd CompressionStrategy = "zstd" | ||
CompressionStrategyZlib CompressionStrategy = "zlib" | ||
CompressionStrategySnappy CompressionStrategy = "snappy" | ||
) | ||
|
||
type Compressor struct { | ||
Strategy CompressionStrategy | ||
} | ||
|
||
func (c *Compressor) Compress(in *bytes.Buffer) (*bytes.Buffer, error) { | ||
switch c.Strategy { | ||
case CompressionStrategyGzip: | ||
return c.gzipCompress(in) | ||
case CompressionStrategyZstd: | ||
return c.zstdCompress(in) | ||
case CompressionStrategyZlib: | ||
return c.zlibCompress(in) | ||
case CompressionStrategySnappy: | ||
return c.snappyCompress(in) | ||
default: | ||
return in, nil | ||
} | ||
} | ||
|
||
func (c *Compressor) gzipCompress(in *bytes.Buffer) (*bytes.Buffer, error) { | ||
out := &bytes.Buffer{} | ||
g := gzip.NewWriter(out) | ||
|
||
_, err := g.Write(in.Bytes()) | ||
if err != nil { | ||
return out, err | ||
} | ||
|
||
if err := g.Close(); err != nil { | ||
return out, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (c *Compressor) zstdCompress(in *bytes.Buffer) (*bytes.Buffer, error) { | ||
out := &bytes.Buffer{} | ||
|
||
z, err := zstd.NewWriter(out) | ||
if err != nil { | ||
return out, err | ||
} | ||
|
||
_, err = z.Write(in.Bytes()) | ||
if err != nil { | ||
return out, err | ||
} | ||
|
||
if err := z.Close(); err != nil { | ||
return out, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (c *Compressor) zlibCompress(in *bytes.Buffer) (*bytes.Buffer, error) { | ||
out := &bytes.Buffer{} | ||
z := zlib.NewWriter(out) | ||
|
||
_, err := z.Write(in.Bytes()) | ||
if err != nil { | ||
return out, err | ||
} | ||
|
||
if err := z.Close(); err != nil { | ||
return out, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (c *Compressor) snappyCompress(in *bytes.Buffer) (*bytes.Buffer, error) { | ||
compressed := snappy.Encode(nil, in.Bytes()) | ||
|
||
return bytes.NewBuffer(compressed), nil | ||
} | ||
|
||
func (c *Compressor) AddHeaders(req *http.Request) { | ||
switch c.Strategy { | ||
case CompressionStrategyGzip: | ||
req.Header.Set("Content-Encoding", "gzip") | ||
case CompressionStrategyZstd: | ||
req.Header.Set("Content-Encoding", "zstd") | ||
case CompressionStrategyZlib: | ||
req.Header.Set("Content-Encoding", "deflate") | ||
case CompressionStrategySnappy: | ||
req.Header.Set("Content-Encoding", "snappy") | ||
} | ||
} |
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