forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lz4.go
74 lines (58 loc) · 1.42 KB
/
lz4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package lz4
import (
"io"
"sync"
"github.com/pierrec/lz4"
kafka "github.com/segmentio/kafka-go"
)
func init() {
kafka.RegisterCompressionCodec(NewCompressionCodec())
}
const (
Code = 3
)
type CompressionCodec struct{}
func NewCompressionCodec() *CompressionCodec {
return &CompressionCodec{}
}
// Code implements the kafka.CompressionCodec interface.
func (CompressionCodec) Code() int8 { return Code }
// Name implements the kafka.CompressionCodec interface.
func (CompressionCodec) Name() string { return "lz4" }
// NewReader implements the kafka.CompressionCodec interface.
func (CompressionCodec) NewReader(r io.Reader) io.ReadCloser {
z := readerPool.Get().(*lz4.Reader)
z.Reset(r)
return &reader{z}
}
// NewWriter implements the kafka.CompressionCodec interface.
func (CompressionCodec) NewWriter(w io.Writer) io.WriteCloser {
z := writerPool.Get().(*lz4.Writer)
z.Reset(w)
return &writer{z}
}
type reader struct{ *lz4.Reader }
func (r *reader) Close() (err error) {
if z := r.Reader; z != nil {
r.Reader = nil
z.Reset(nil)
readerPool.Put(z)
}
return
}
type writer struct{ *lz4.Writer }
func (w *writer) Close() (err error) {
if z := w.Writer; z != nil {
w.Writer = nil
err = z.Close()
z.Reset(nil)
writerPool.Put(z)
}
return
}
var readerPool = sync.Pool{
New: func() interface{} { return lz4.NewReader(nil) },
}
var writerPool = sync.Pool{
New: func() interface{} { return lz4.NewWriter(nil) },
}