mirror of https://github.com/gorilla/websocket.git
impl: compressContextTakeover
This commit is contained in:
parent
36c43970ee
commit
a366cdf616
|
@ -53,7 +53,7 @@ func isValidCompressionLevel(level int) bool {
|
||||||
return minCompressionLevel <= level && level <= maxCompressionLevel
|
return minCompressionLevel <= level && level <= maxCompressionLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func compressNoContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
|
func compressNoContextTakeover(w io.WriteCloser, level int, dict []byte) io.WriteCloser {
|
||||||
p := &flateWriterPools[level-minCompressionLevel]
|
p := &flateWriterPools[level-minCompressionLevel]
|
||||||
tw := &truncWriter{w: w}
|
tw := &truncWriter{w: w}
|
||||||
fw, _ := p.Get().(*flate.Writer)
|
fw, _ := p.Get().(*flate.Writer)
|
||||||
|
@ -65,15 +65,14 @@ func compressNoContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
|
||||||
return &flateWriteWrapper{fw: fw, tw: tw, p: p}
|
return &flateWriteWrapper{fw: fw, tw: tw, p: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func compressContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
|
func compressContextTakeover(w io.WriteCloser, level int, dict []byte) io.WriteCloser {
|
||||||
p := &flateWriterPools[level-minCompressionLevel]
|
p := &flateWriterPools[level-minCompressionLevel]
|
||||||
tw := &truncWriter{w: w}
|
tw := &truncWriter{w: w}
|
||||||
fw, _ := p.Get().(*flate.Writer)
|
|
||||||
if fw == nil {
|
// WriterDict's Reset just restores the dictionary.
|
||||||
fw, _ = flate.NewWriter(tw, level)
|
// Initialization is done with New. (If possible get struct from sync.Pool)
|
||||||
} else {
|
fw, _ := flate.NewWriterDict(tw, level, dict)
|
||||||
fw.Reset(tw)
|
|
||||||
}
|
|
||||||
return &flateWriteWrapper{fw: fw, tw: tw, p: p}
|
return &flateWriteWrapper{fw: fw, tw: tw, p: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
conn.go
14
conn.go
|
@ -243,7 +243,7 @@ type Conn struct {
|
||||||
|
|
||||||
enableWriteCompression bool
|
enableWriteCompression bool
|
||||||
compressionLevel int
|
compressionLevel int
|
||||||
newCompressionWriter func(io.WriteCloser, int) io.WriteCloser
|
newCompressionWriter func(io.WriteCloser, int, []byte) 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
|
||||||
|
@ -505,9 +505,14 @@ 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 := c.newCompressionWriter(c.writer, c.compressionLevel)
|
|
||||||
mw.compress = true
|
mw.compress = true
|
||||||
c.writer = w
|
switch {
|
||||||
|
case c.contextTakeover:
|
||||||
|
c.writer = c.newCompressionWriter(c.writer, c.compressionLevel, c.dict)
|
||||||
|
// no-context-takeover
|
||||||
|
default:
|
||||||
|
c.writer = c.newCompressionWriter(c.writer, c.compressionLevel, nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return c.writer, nil
|
return c.writer, nil
|
||||||
}
|
}
|
||||||
|
@ -758,6 +763,9 @@ func (c *Conn) WriteMessage(messageType int, data []byte) error {
|
||||||
if _, err = w.Write(data); err != nil {
|
if _, err = w.Write(data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if c.contextTakeover {
|
||||||
|
c.AddDict(data)
|
||||||
|
}
|
||||||
return w.Close()
|
return w.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue