-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 #1185 from retailnext/reuse-compression-objects
Reuse compression objects
- Loading branch information
Showing
4 changed files
with
151 additions
and
165 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,75 @@ | ||
package sarama | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/eapache/go-xerial-snappy" | ||
"github.com/pierrec/lz4" | ||
) | ||
|
||
var ( | ||
lz4WriterPool = sync.Pool{ | ||
New: func() interface{} { | ||
return lz4.NewWriter(nil) | ||
}, | ||
} | ||
|
||
gzipWriterPool = sync.Pool{ | ||
New: func() interface{} { | ||
return gzip.NewWriter(nil) | ||
}, | ||
} | ||
) | ||
|
||
func compress(cc CompressionCodec, level int, data []byte) ([]byte, error) { | ||
switch cc { | ||
case CompressionNone: | ||
return data, nil | ||
case CompressionGZIP: | ||
var ( | ||
err error | ||
buf bytes.Buffer | ||
writer *gzip.Writer | ||
) | ||
if level != CompressionLevelDefault { | ||
writer, err = gzip.NewWriterLevel(&buf, level) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
writer = gzipWriterPool.Get().(*gzip.Writer) | ||
defer gzipWriterPool.Put(writer) | ||
writer.Reset(&buf) | ||
} | ||
if _, err := writer.Write(data); err != nil { | ||
return nil, err | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
case CompressionSnappy: | ||
return snappy.Encode(data), nil | ||
case CompressionLZ4: | ||
writer := lz4WriterPool.Get().(*lz4.Writer) | ||
defer lz4WriterPool.Put(writer) | ||
|
||
var buf bytes.Buffer | ||
writer.Reset(&buf) | ||
|
||
if _, err := writer.Write(data); err != nil { | ||
return nil, err | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
case CompressionZSTD: | ||
return zstdCompressLevel(nil, data, level) | ||
default: | ||
return nil, PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", cc)} | ||
} | ||
} |
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,63 @@ | ||
package sarama | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"io/ioutil" | ||
"sync" | ||
|
||
"github.com/eapache/go-xerial-snappy" | ||
"github.com/pierrec/lz4" | ||
) | ||
|
||
var ( | ||
lz4ReaderPool = sync.Pool{ | ||
New: func() interface{} { | ||
return lz4.NewReader(nil) | ||
}, | ||
} | ||
|
||
gzipReaderPool sync.Pool | ||
) | ||
|
||
func decompress(cc CompressionCodec, data []byte) ([]byte, error) { | ||
switch cc { | ||
case CompressionNone: | ||
return data, nil | ||
case CompressionGZIP: | ||
var ( | ||
err error | ||
reader *gzip.Reader | ||
readerIntf = gzipReaderPool.Get() | ||
) | ||
if readerIntf != nil { | ||
reader = readerIntf.(*gzip.Reader) | ||
} else { | ||
reader, err = gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
defer gzipReaderPool.Put(reader) | ||
|
||
if err := reader.Reset(bytes.NewReader(data)); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.ReadAll(reader) | ||
case CompressionSnappy: | ||
return snappy.Decode(data) | ||
case CompressionLZ4: | ||
reader := lz4ReaderPool.Get().(*lz4.Reader) | ||
defer lz4ReaderPool.Put(reader) | ||
|
||
reader.Reset(bytes.NewReader(data)) | ||
return ioutil.ReadAll(reader) | ||
case CompressionZSTD: | ||
return zstdDecompress(nil, data) | ||
default: | ||
return nil, PacketDecodingError{fmt.Sprintf("invalid compression specified (%d)", cc)} | ||
} | ||
} |
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