gorilla/websocket · error

websocket: invalid compression level

Error message

websocket: invalid compression level

What it means

SetCompressionLevel validates the flate compression level (must be between flate.BestSpeed=1 and flate.BestCompression=9, per isValidCompressionLevel) and returns this error when the given level is out of range. The connection's compression level is left unchanged.

Source

Thrown at conn.go:1227

// Deprecated: Use the NetConn method.
func (c *Conn) UnderlyingConn() net.Conn {
	return c.conn
}

// EnableWriteCompression enables and disables write compression of
// subsequent text and binary messages. This function is a noop if
// compression was not negotiated with the peer.
func (c *Conn) EnableWriteCompression(enable bool) {
	c.enableWriteCompression = enable
}

// SetCompressionLevel sets the flate compression level for subsequent text and
// binary messages. This function is a noop if compression was not negotiated
// with the peer. See the compress/flate package for a description of
// compression levels.
func (c *Conn) SetCompressionLevel(level int) error {
	if !isValidCompressionLevel(level) {
		return errors.New("websocket: invalid compression level")
	}
	c.compressionLevel = level
	return nil
}

// FormatCloseMessage formats closeCode and text as a WebSocket close message.
// An empty message is returned for code CloseNoStatusReceived.
func FormatCloseMessage(closeCode int, text string) []byte {
	if closeCode == CloseNoStatusReceived {
		// Return empty message because it's illegal to send
		// CloseNoStatusReceived. Return non-nil value in case application
		// checks for nil.
		return []byte{}
	}
	buf := make([]byte, 2+len(text))
	binary.BigEndian.PutUint16(buf, uint16(closeCode))
	copy(buf[2:], text)
	return buf

View on GitHub (pinned to e064f32e36)

Solutions

  1. Pass a level between flate.BestSpeed (1) and flate.BestCompression (9)
  2. Use flate.DefaultCompression (-1) if you want the default, which resolves to a valid internal level
  3. Clamp user/config-supplied compression levels before calling SetCompressionLevel
  4. Remember the call is a no-op if compression was never negotiated via the subprotocol handshake

Example fix

// before
c.SetCompressionLevel(flate.NoCompression) // 0 -> error
// after
c.SetCompressionLevel(flate.BestSpeed) // 1-9 accepted
Defensive patterns

Strategy: validation

Validate before calling

func validCompressionLevel(level int) bool {
    return level >= flate.BestSpeed && level <= flate.BestCompression // 1..9
}
if validCompressionLevel(level) {
    conn.SetCompressionLevel(level)
}

Type guard

func isValidLevel(l int) bool { return l >= 1 && l <= 9 }

Prevention

When it happens

Trigger: Calling Conn.SetCompressionLevel with a value outside 1-9 (e.g. 0, 10, or flate.NoCompression=0, which is rejected).

Common situations: Passing flate.NoCompression (0) or flate.HuffmanOnly (-2) assuming all compress/flate constants are valid; off-by-one level values from configuration.

Understand the failure class

Background: "must be positive", "Invalid value": how libraries reject invalid parameter values (ValueError, ArgumentError, INVALID_PARAMETER_VALUE) — this error's family across 28 libraries.

Related errors


AI-assisted analysis of gorilla/websocket@e064f32e36 (2026-08-31). Data as JSON: /api/errors/bb3a7d0510d073c9. Report an issue: GitHub.