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 ( var (
flateWriterPool = sync.Pool{} flateWriterPool = sync.Pool{New: func() interface{} {
flateReaderPool = sync.Pool{} fw, _ := flate.NewWriter(nil, 3)
return fw
}}
flateReaderPool = sync.Pool{New: func() interface{} {
return flate.NewReader(nil)
}}
) )
func decompressNoContextTakeover(r io.Reader) io.ReadCloser { 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. // Add final block to squelch unexpected EOF error from flate reader.
"\x01\x00\x00\xff\xff" "\x01\x00\x00\xff\xff"
i := flateReaderPool.Get() fr, _ := flateReaderPool.Get().(io.ReadCloser)
if i == nil { fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
i = flate.NewReader(nil) return &flateReadWrapper{fr}
}
i.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
return &flateReadWrapper{i.(io.ReadCloser)}
} }
func compressNoContextTakeover(w io.WriteCloser) (io.WriteCloser, error) { func compressNoContextTakeover(w io.WriteCloser) io.WriteCloser {
tw := &truncWriter{w: w} tw := &truncWriter{w: w}
i := flateWriterPool.Get() fw, _ := flateWriterPool.Get().(*flate.Writer)
var fw *flate.Writer
var err error
if i == nil {
fw, err = flate.NewWriter(tw, 3)
} else {
fw = i.(*flate.Writer)
fw.Reset(tw) fw.Reset(tw)
} return &flateWriteWrapper{fw: fw, tw: tw}
return &flateWriteWrapper{fw: fw, tw: tw}, err
} }
// truncWriter is an io.Writer that writes all but the last four bytes of the // 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 writeErr error
enableWriteCompression bool enableWriteCompression bool
newCompressionWriter func(io.WriteCloser) (io.WriteCloser, error) newCompressionWriter func(io.WriteCloser) io.WriteCloser
// Read fields // Read fields
reader io.ReadCloser // the current reader returned to the application 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 c.writer = mw
if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) { if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) {
w, err := c.newCompressionWriter(c.writer) w := c.newCompressionWriter(c.writer)
if err != nil {
c.writer = nil
return nil, err
}
mw.compress = true mw.compress = true
c.writer = w c.writer = w
} }

2
doc.go
View File

@ -150,7 +150,7 @@
// application's responsibility to check the Origin header before calling // application's responsibility to check the Origin header before calling
// Upgrade. // Upgrade.
// //
// Compression [Experimental] // Compression
// //
// Per message compression extensions (RFC 7692) are experimentally supported // Per message compression extensions (RFC 7692) are experimentally supported
// by this package in a limited capacity. Setting the EnableCompression option // by this package in a limited capacity. Setting the EnableCompression option