Skip to content

Commit

Permalink
fix compress writer (#48)
Browse files Browse the repository at this point in the history
* fix compress writer
* add pool buffer

Before we just decorate the given writer writer, err = gzip.NewWriterLevel(w.Writer, w.compressionLevel) but the gzip/zlibWriter call writer.Write more than once in order to compress input

Now we use a buffer with gzip/zlibWriter and send once the compressed data to the given writer

PS: it seems gzip/zlibWriter write compressed data when gzip/zlibWriter.Close() was called
  • Loading branch information
instabledesign authored Oct 18, 2019
1 parent 85d2b76 commit d0ca496
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
34 changes: 28 additions & 6 deletions writer/compress.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package writer

import (
"bytes"
"compress/gzip"
"compress/zlib"
"io"
"sync"
)

// CompressType is the compress writer compression type
Expand All @@ -29,27 +31,47 @@ func (w *CompressWriter) Write(p []byte) (int, error) {
return w.Writer.Write(p)
}

var writer io.WriteCloser
var compressWriter io.WriteCloser
var err error
buf := newBuffer()
defer bufPool.Put(buf)

switch w.compressionType {
case CompressGzip:
writer, err = gzip.NewWriterLevel(w.Writer, w.compressionLevel)
compressWriter, err = gzip.NewWriterLevel(buf, w.compressionLevel)
case CompressZlib:
writer, err = zlib.NewWriterLevel(w.Writer, w.compressionLevel)
compressWriter, err = zlib.NewWriterLevel(buf, w.compressionLevel)
}
if err != nil {
return 0, err
}

n, err := writer.Write(p)
if err != nil {
if n, err := compressWriter.Write(p); err != nil {
compressWriter.Close()
return n, err
}
return n, writer.Close()
compressWriter.Close()

return w.Writer.Write(buf.Bytes())
}

// NewCompressWriter will return a new compress writer
func NewCompressWriter(writer io.Writer, compressionType CompressType, compressionLevel int) *CompressWriter {
return &CompressWriter{Writer: writer, compressionType: compressionType, compressionLevel: compressionLevel}
}

// 1k bytes buffer by default
var bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 1024))
},
}

func newBuffer() *bytes.Buffer {
b := bufPool.Get().(*bytes.Buffer)
if b != nil {
b.Reset()
return b
}
return bytes.NewBuffer(nil)
}
4 changes: 2 additions & 2 deletions writer/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestCompressWriter_Write_CompressGzip(t *testing.T) {
w := writer.NewCompressWriter(buffer, writer.CompressGzip, 2)

i, err := w.Write([]byte("fake_data"))
assert.Equal(t, 9, i)
assert.Equal(t, 33, i)
assert.Nil(t, err)
}

Expand All @@ -43,6 +43,6 @@ func TestCompressWriter_Write_CompressZlib(t *testing.T) {
w := writer.NewCompressWriter(buffer, writer.CompressZlib, 2)

i, err := w.Write([]byte("fake_data"))
assert.Equal(t, 9, i)
assert.Equal(t, 21, i)
assert.Nil(t, err)
}

0 comments on commit d0ca496

Please sign in to comment.