Compression improvements

- Remove unnecessary error return from compressNoContextTakeover.
- Simplify use of sync.Pool.
- Fix formatting in compression documentation.
This commit is contained in:
Gary Burd 2016-12-27 17:05:16 -05:00
parent 2db2f66488
commit 6c51b25bc8
3 changed files with 18 additions and 27 deletions

View File

@ -13,8 +13,13 @@ import (
)
var (
flateWriterPool = sync.Pool{}
flateReaderPool = sync.Pool{}
flateWriterPool = sync.Pool{New: func() interface{} {
fw, _ := flate.NewWriter(nil, 3)
return fw
}}
flateReaderPool = sync.Pool{New: func() interface{} {
return flate.NewReader(nil)
}}
)
func decompressNoContextTakeover(r io.Reader) io.ReadCloser {
@ -24,26 +29,16 @@ func decompressNoContextTakeover(r io.Reader) io.ReadCloser {
// Add final block to squelch unexpected EOF error from flate reader.
"\x01\x00\x00\xff\xff"
i := flateReaderPool.Get()
if i == nil {
i = flate.NewReader(nil)
}
i.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
return &flateReadWrapper{i.(io.ReadCloser)}
fr, _ := flateReaderPool.Get().(io.ReadCloser)
fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
return &flateReadWrapper{fr}
}
func compressNoContextTakeover(w io.WriteCloser) (io.WriteCloser, error) {
func compressNoContextTakeover(w io.WriteCloser) io.WriteCloser {
tw := &truncWriter{w: w}
i := flateWriterPool.Get()
var fw *flate.Writer
var err error
if i == nil {
fw, err = flate.NewWriter(tw, 3)
} else {
fw = i.(*flate.Writer)
fw.Reset(tw)
}
return &flateWriteWrapper{fw: fw, tw: tw}, err
fw, _ := flateWriterPool.Get().(*flate.Writer)
fw.Reset(tw)
return &flateWriteWrapper{fw: fw, tw: tw}
}
// truncWriter is an io.Writer that writes all but the last four bytes of the

View File

@ -235,7 +235,7 @@ type Conn struct {
writeErr error
enableWriteCompression bool
newCompressionWriter func(io.WriteCloser) (io.WriteCloser, error)
newCompressionWriter func(io.WriteCloser) io.WriteCloser
// Read fields
reader io.ReadCloser // the current reader returned to the application
@ -444,11 +444,7 @@ func (c *Conn) NextWriter(messageType int) (io.WriteCloser, error) {
}
c.writer = mw
if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) {
w, err := c.newCompressionWriter(c.writer)
if err != nil {
c.writer = nil
return nil, err
}
w := c.newCompressionWriter(c.writer)
mw.compress = true
c.writer = w
}

4
doc.go
View File

@ -150,7 +150,7 @@
// application's responsibility to check the Origin header before calling
// Upgrade.
//
// Compression [Experimental]
// Compression
//
// Per message compression extensions (RFC 7692) are experimentally supported
// by this package in a limited capacity. Setting the EnableCompression option
@ -162,7 +162,7 @@
// Per message compression of messages written to a connection can be enabled
// or disabled by calling the corresponding Conn method:
//
// conn.EnableWriteCompression(true)
// conn.EnableWriteCompression(true)
//
// Currently this package does not support compression with "context takeover".
// This means that messages must be compressed and decompressed in isolation,