Skip to content

Commit

Permalink
Expose DataCompressor struct attributes (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsord authored and flashmob committed Oct 31, 2019
1 parent c1ab4a4 commit 51f7dda
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions backends/p_compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func init() {

// compressedData struct will be compressed using zlib when printed via fmt
type DataCompressor struct {
extraHeaders []byte
data *bytes.Buffer
ExtraHeaders []byte
Data *bytes.Buffer
// the pool is used to recycle buffers to ease up on the garbage collector
pool *sync.Pool
Pool *sync.Pool
}

// newCompressedData returns a new CompressedData
Expand All @@ -49,44 +49,44 @@ func newCompressor() *DataCompressor {
},
}
return &DataCompressor{
pool: &p,
Pool: &p,
}
}

// Set the extraheaders and buffer of data to compress
func (c *DataCompressor) set(b []byte, d *bytes.Buffer) {
c.extraHeaders = b
c.data = d
c.ExtraHeaders = b
c.Data = d
}

// String implements the Stringer interface.
// Can only be called once!
// This is because the compression buffer will be reset and compressor will be returned to the pool
func (c *DataCompressor) String() string {
if c.data == nil {
if c.Data == nil {
return ""
}
//borrow a buffer form the pool
b := c.pool.Get().(*bytes.Buffer)
b := c.Pool.Get().(*bytes.Buffer)
// put back in the pool
defer func() {
b.Reset()
c.pool.Put(b)
c.Pool.Put(b)
}()

var r *bytes.Reader
w, _ := zlib.NewWriterLevel(b, zlib.BestSpeed)
r = bytes.NewReader(c.extraHeaders)
r = bytes.NewReader(c.ExtraHeaders)
_, _ = io.Copy(w, r)
_, _ = io.Copy(w, c.data)
_, _ = io.Copy(w, c.Data)
_ = w.Close()
return b.String()
}

// clear it, without clearing the pool
func (c *DataCompressor) clear() {
c.extraHeaders = []byte{}
c.data = nil
c.ExtraHeaders = []byte{}
c.Data = nil
}

func Compressor() Decorator {
Expand Down

0 comments on commit 51f7dda

Please sign in to comment.