@@ -9,43 +9,45 @@ import (
9
9
"sync"
10
10
)
11
11
12
- // CompressionMode represents the modes available to the deflate extension.
12
+ // CompressionMode represents the modes available to the permessage- deflate extension.
13
13
// See https://tools.ietf.org/html/rfc7692
14
14
//
15
- // Works in all browsers except Safari which does not implement the deflate extension.
15
+ // Works in all modern browsers except Safari which does not implement the permessage-deflate extension.
16
+ //
17
+ // Compression is only used if the peer supports the mode selected.
16
18
type CompressionMode int
17
19
18
20
const (
19
- // CompressionDisabled disables the deflate extension.
20
- //
21
- // Use this if you are using a predominantly binary protocol with very
22
- // little duplication in between messages or CPU and memory are more
23
- // important than bandwidth.
21
+ // CompressionDisabled disables the negotiation of the permessage-deflate extension.
24
22
//
25
- // This is the default.
23
+ // This is the default. Do not enable compression without benchmarking for your particular use case first.
26
24
CompressionDisabled CompressionMode = iota
27
25
28
- // CompressionContextTakeover uses a 32 kB sliding window and flate.Writer per connection.
29
- // It reuses the sliding window from previous messages.
30
- // As most WebSocket protocols are repetitive, this can be very efficient.
31
- // It carries an overhead of 32 kB + 1.2 MB for every connection compared to CompressionNoContextTakeover.
26
+ // CompressionNoContextTakeover compresses each message greater than 512 bytes. Each message is compressed with
27
+ // a new 1.2 MB flate.Writer pulled from a sync.Pool. Each message is read with a 40 KB flate.Reader pulled from
28
+ // a sync.Pool.
32
29
//
33
- // Sometime in the future it will carry 65 kB overhead instead once https://github.com/golang/go/issues/36919
34
- // is fixed.
30
+ // This means less efficient compression as the sliding window from previous messages will not be used but the
31
+ // memory overhead will be lower as there will be no fixed cost for the flate.Writer nor the 32 KB sliding window.
32
+ // Especially if the connections are long lived and seldom written to.
35
33
//
36
- // If the peer negotiates NoContextTakeover on the client or server side, it will be
37
- // used instead as this is required by the RFC.
38
- CompressionContextTakeover
34
+ // Thus, it uses less memory than CompressionContextTakeover but compresses less efficiently.
35
+ //
36
+ // If the peer does not support CompressionNoContextTakeover then we will fall back to CompressionDisabled.
37
+ CompressionNoContextTakeover
39
38
40
- // CompressionNoContextTakeover grabs a new flate.Reader and flate.Writer as needed
41
- // for every message. This applies to both server and client side .
39
+ // CompressionContextTakeover compresses each message greater than 128 bytes reusing the 32 KB sliding window from
40
+ // previous messages. i.e compression context across messages is preserved .
42
41
//
43
- // This means less efficient compression as the sliding window from previous messages
44
- // will not be used but the memory overhead will be lower if the connections
45
- // are long lived and seldom used.
42
+ // As most WebSocket protocols are text based and repetitive, this compression mode can be very efficient.
46
43
//
47
- // The message will only be compressed if greater than 512 bytes.
48
- CompressionNoContextTakeover
44
+ // The memory overhead is a fixed 32 KB sliding window, a fixed 1.2 MB flate.Writer and a sync.Pool of 40 KB flate.Reader's
45
+ // that are used when reading and then returned.
46
+ //
47
+ // Thus, it uses more memory than CompressionNoContextTakeover but compresses more efficiently.
48
+ //
49
+ // If the peer does not support CompressionContextTakeover then we will fall back to CompressionNoContextTakeover.
50
+ CompressionContextTakeover
49
51
)
50
52
51
53
func (m CompressionMode ) opts () * compressionOptions {
0 commit comments