Skip to content

Commit

Permalink
refactor: adjust zstd compress
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 8, 2023
1 parent 7aab359 commit ba5ef2f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions middleware/cache_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ func TestNewGzipCompress(t *testing.T) {
result, _ = GzipDecompress(result.Bytes())
assert.Equal(data, result)
}

func TestNewZstdCompress(t *testing.T) {
assert := assert.New(t)
compressor := NewCacheZstdCompressor()
compressor.MinLength = 20

assert.False(compressor.IsValid("text", 1))
assert.True(compressor.IsValid("text", 100))

data := bytes.NewBufferString("hello world!hello world!hello world!")
result, compressionType, err := compressor.Compress(data)
assert.Equal(CompressionZstd, compressionType)
assert.Nil(err)
assert.NotEqual(data, result)
result, _ = ZstdDecompress(result.Bytes())
assert.Equal(data, result)
}
2 changes: 2 additions & 0 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func NewCompress(config CompressConfig) elton.Handler {
fillHeader(encoding)
c.BodyBuffer = newBuf
}
// 已有符合的压缩,处理完成跳出当前循环
break
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestCompressMiddleware(t *testing.T) {
encoding: "gzip",
etag: "W/123",
},
// reader pike
// reader pipe
{
newContext: func() *elton.Context {
req := httptest.NewRequest("GET", "/users/me", nil)
Expand Down
2 changes: 1 addition & 1 deletion middleware/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (z *ZstdCompressor) Compress(buf []byte, levels ...int) (*bytes.Buffer, err
if len(levels) != 0 && levels[0] != IgnoreCompression {
level = levels[0]
}
return GzipCompress(buf, level)
return ZstdCompress(buf, level)
}

// Pipe compress by pipe
Expand Down

0 comments on commit ba5ef2f

Please sign in to comment.