Repository: syncthing/syncthing
File: lib/protocol/protocol.go (Go)

Code before:
		if chdrSize > 1<<16-1 {
			panic("impossibly large header")
		}

		overhead := 2 + chdrSize + 4
		maxCompressed := overhead + lz4.CompressBound(size)
		cbuf := BufferPool.Get(maxCompressed)
		defer BufferPool.Put(cbuf)

		compressedSize, err = lz4Compress(buf[2+hdrSize+4:], cbuf[overhead:])
		if err != nil || compressedSize+overhead >= totSize {
			goto write
		}

		buf, hdr, hdrSize = cbuf, chdr, chdrSize
		totSize = compressedSize + overhead

		// Header length
		binary.BigEndian.PutUint16(buf, uint16(hdrSize))
		// Header
		if _, err := hdr.MarshalTo(buf[2:]); err != nil {
			return errors.Wrap(err, "marshalling header")
		}
		// Message length
		binary.BigEndian.PutUint32(buf[2+chdrSize:], uint32(compressedSize))
	}

write:
	n, err := c.cw.Write(buf[:totSize])

	if hdr.Compression == MessageCompressionLZ4 {
		l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message (%d uncompressed)), err=%v", n, hdrSize, compressedSize, size, err)
	} else {
		l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message), err=%v", n, hdrSize, size, err)
	}
	if err != nil {
		return errors.Wrap(err, "writing message")
	}
	return nil
}

func (c *rawConnection) typeOf(msg message) MessageType {
	switch msg.(type) {
	case *ClusterConfig:
		return MessageTypeClusterConfig
	case *Index:
		return MessageTypeIndex
	case *IndexUpdate:
		return MessageTypeIndexUpdate
	case *Request:
		return MessageTypeRequest

Review comment (suggestion):
I think it would be simpler/cleaner if this code would stay in a separate function, e.g. `compressedMessage(msg message, uncompressedSize int) ([]byte, bool, error)` that returns false if compressing increases the size. Obviously it's entirely possible I am missing something that makes this annoying/ugly.

Code after:

	n, err := c.cw.Write(buf)

	l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message), err=%v", n, hdrSize, size, err)
	if err != nil {
		return errors.Wrap(err, "writing message")
	}
	return nil
}

// Write msg out compressed, given its uncompressed marshaled payload and overhead.
//
// The first return value indicates whether compression succeeded.
// If not, the caller should retry without compression.
func (c *rawConnection) writeCompressedMessage(msg message, marshaled []byte, overhead int) (ok bool, err error) {
	hdr := Header{
		Type:        c.typeOf(msg),
		Compression: MessageCompressionLZ4,
	}
	hdrSize := hdr.ProtoSize()
	if hdrSize > 1<<16-1 {
		panic("impossibly large header")
	}

	cOverhead := 2 + hdrSize + 4
	maxCompressed := cOverhead + lz4.CompressBound(len(marshaled))
	buf := BufferPool.Get(maxCompressed)
	defer BufferPool.Put(buf)

	compressedSize, err := lz4Compress(marshaled, buf[cOverhead:])
	totSize := compressedSize + cOverhead
	if err != nil || totSize >= len(marshaled)+overhead {
		return false, nil
	}

	// Header length
	binary.BigEndian.PutUint16(buf, uint16(hdrSize))
	// Header
	if _, err := hdr.MarshalTo(buf[2:]); err != nil {
		return true, errors.Wrap(err, "marshalling header")
	}
	// Message length
	binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(compressedSize))

	n, err := c.cw.Write(buf[:totSize])
	l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message (%d uncompressed)), err=%v", n, hdrSize, compressedSize, len(marshaled), err)
	if err != nil {
		return true, errors.Wrap(err, "writing message")
	}
	return true, nil
}