forked from mirror/websocket
Compression improvements
- Remove unnecessary error return from compressNoContextTakeover. - Simplify use of sync.Pool. - Fix formatting in compression documentation.
This commit is contained in:
parent
2db2f66488
commit
6c51b25bc8
|
@ -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, _ := flateWriterPool.Get().(*flate.Writer)
|
||||
fw.Reset(tw)
|
||||
}
|
||||
return &flateWriteWrapper{fw: fw, tw: tw}, err
|
||||
return &flateWriteWrapper{fw: fw, tw: tw}
|
||||
}
|
||||
|
||||
// truncWriter is an io.Writer that writes all but the last four bytes of the
|
||||
|
|
8
conn.go
8
conn.go
|
@ -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
|
||||
}
|
||||
|
|
2
doc.go
2
doc.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue