github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/compress/lz4/lz4.go (about) 1 package lz4 2 3 import ( 4 "io" 5 "sync" 6 7 "github.com/pierrec/lz4/v4" 8 ) 9 10 var ( 11 readerPool sync.Pool 12 writerPool sync.Pool 13 ) 14 15 // Codec is the implementation of a compress.Codec which supports creating 16 // readers and writers for kafka messages compressed with lz4. 17 type Codec struct{} 18 19 // Code implements the compress.Codec interface. 20 func (c *Codec) Code() int8 { return 3 } 21 22 // Name implements the compress.Codec interface. 23 func (c *Codec) Name() string { return "lz4" } 24 25 // NewReader implements the compress.Codec interface. 26 func (c *Codec) NewReader(r io.Reader) io.ReadCloser { 27 z, _ := readerPool.Get().(*lz4.Reader) 28 if z != nil { 29 z.Reset(r) 30 } else { 31 z = lz4.NewReader(r) 32 } 33 return &reader{Reader: z} 34 } 35 36 // NewWriter implements the compress.Codec interface. 37 func (c *Codec) NewWriter(w io.Writer) io.WriteCloser { 38 z, _ := writerPool.Get().(*lz4.Writer) 39 if z != nil { 40 z.Reset(w) 41 } else { 42 z = lz4.NewWriter(w) 43 } 44 return &writer{Writer: z} 45 } 46 47 type reader struct{ *lz4.Reader } 48 49 func (r *reader) Close() (err error) { 50 if z := r.Reader; z != nil { 51 r.Reader = nil 52 z.Reset(nil) 53 readerPool.Put(z) 54 } 55 return 56 } 57 58 type writer struct{ *lz4.Writer } 59 60 func (w *writer) Close() (err error) { 61 if z := w.Writer; z != nil { 62 w.Writer = nil 63 err = z.Close() 64 z.Reset(nil) 65 writerPool.Put(z) 66 } 67 return 68 }