Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Fix build without cgo
Browse files Browse the repository at this point in the history
The github.com/DataDog/zstd library wraps a c library, so it requires
cgo to compile. Add zstd wrapper funcs to sarama and put the zstd
import behind a build flag requiring cgo. Without cgo, the wrapper
funcs return an error saying zstd doesn't work without cgo.
  • Loading branch information
Muir Manders committed Oct 2, 2018
1 parent 1a7ecd8 commit 3dd2f63
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 2 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"time"

"github.com/DataDog/zstd"
"github.com/eapache/go-xerial-snappy"
"github.com/pierrec/lz4"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ func (m *Message) encode(pe packetEncoder) error {
m.compressedCache = buf.Bytes()
payload = m.compressedCache
case CompressionZSTD:
c, err := zstd.CompressLevel(nil, m.Value, m.CompressionLevel)
c, err := zstdCompressLevel(nil, m.Value, m.CompressionLevel)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,7 +218,7 @@ func (m *Message) decode(pd packetDecoder) (err error) {
if m.Value == nil {
break
}
if m.Value, err = zstd.Decompress(nil, m.Value); err != nil {
if m.Value, err = zstdDecompress(nil, m.Value); err != nil {
return err
}
if err := m.decodeSet(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions record_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"time"

"github.com/DataDog/zstd"
"github.com/eapache/go-xerial-snappy"
"github.com/pierrec/lz4"
)
Expand Down Expand Up @@ -195,7 +194,7 @@ func (b *RecordBatch) decode(pd packetDecoder) (err error) {
return err
}
case CompressionZSTD:
if recBuffer, err = zstd.Decompress(nil, recBuffer); err != nil {
if recBuffer, err = zstdDecompress(nil, recBuffer); err != nil {
return err
}
default:
Expand Down Expand Up @@ -254,7 +253,7 @@ func (b *RecordBatch) encodeRecords(pe packetEncoder) error {
}
b.compressedRecords = buf.Bytes()
case CompressionZSTD:
c, err := zstd.CompressLevel(nil, raw, b.CompressionLevel)
c, err := zstdCompressLevel(nil, raw, b.CompressionLevel)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions zstd_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build cgo

package sarama

import "github.com/DataDog/zstd"

func zstdDecompress(dst, src []byte) ([]byte, error) {
return zstd.Decompress(dst, src)
}

func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
return zstd.CompressLevel(dst, src, level)
}
17 changes: 17 additions & 0 deletions zstd_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build !cgo

package sarama

import (
"errors"
)

var errZstdCgo = errors.New("zstd compression requires building with cgo enabled")

func zstdDecompress(dst, src []byte) ([]byte, error) {
return nil, errZstdCgo
}

func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
return nil, errZstdCgo
}

0 comments on commit 3dd2f63

Please sign in to comment.