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 (
|
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
|
fw.Reset(tw)
|
||||||
var err error
|
return &flateWriteWrapper{fw: fw, tw: tw}
|
||||||
if i == nil {
|
|
||||||
fw, err = flate.NewWriter(tw, 3)
|
|
||||||
} else {
|
|
||||||
fw = i.(*flate.Writer)
|
|
||||||
fw.Reset(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
|
||||||
|
|
8
conn.go
8
conn.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
4
doc.go
4
doc.go
|
@ -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
|
||||||
|
@ -162,7 +162,7 @@
|
||||||
// Per message compression of messages written to a connection can be enabled
|
// Per message compression of messages written to a connection can be enabled
|
||||||
// or disabled by calling the corresponding Conn method:
|
// or disabled by calling the corresponding Conn method:
|
||||||
//
|
//
|
||||||
// conn.EnableWriteCompression(true)
|
// conn.EnableWriteCompression(true)
|
||||||
//
|
//
|
||||||
// Currently this package does not support compression with "context takeover".
|
// Currently this package does not support compression with "context takeover".
|
||||||
// This means that messages must be compressed and decompressed in isolation,
|
// This means that messages must be compressed and decompressed in isolation,
|
||||||
|
|
Loading…
Reference in New Issue