forked from benthosdev/benthos
/
processor_compress.go
70 lines (63 loc) · 1.94 KB
/
processor_compress.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
package pure
import (
"context"
"github.com/dafanshu/benthos/v4/internal/bundle"
"github.com/dafanshu/benthos/v4/internal/component/processor"
"github.com/dafanshu/benthos/v4/internal/docs"
"github.com/dafanshu/benthos/v4/internal/log"
"github.com/dafanshu/benthos/v4/internal/message"
)
func init() {
err := bundle.AllProcessors.Add(func(conf processor.Config, mgr bundle.NewManagement) (processor.V1, error) {
p, err := newCompress(conf.Compress, mgr)
if err != nil {
return nil, err
}
return processor.NewAutoObservedProcessor("compress", p, mgr), nil
}, docs.ComponentSpec{
Name: "compress",
Categories: []string{
"Parsing",
},
Summary: `
Compresses messages according to the selected algorithm. Supported compression
algorithms are: gzip, zlib, flate, snappy, lz4.`,
Description: `
The 'level' field might not apply to all algorithms.`,
Config: docs.FieldComponent().WithChildren(
docs.FieldString("algorithm", "The compression algorithm to use.").HasOptions("gzip", "zlib", "flate", "snappy", "lz4"),
docs.FieldInt("level", "The level of compression to use. May not be applicable to all algorithms."),
).ChildDefaultAndTypesFromStruct(processor.NewCompressConfig()),
})
if err != nil {
panic(err)
}
}
type compressProc struct {
level int
comp CompressFunc
log log.Modular
}
func newCompress(conf processor.CompressConfig, mgr bundle.NewManagement) (*compressProc, error) {
cor, err := strToCompressor(conf.Algorithm)
if err != nil {
return nil, err
}
return &compressProc{
level: conf.Level,
comp: cor,
log: mgr.Logger(),
}, nil
}
func (c *compressProc) Process(ctx context.Context, msg *message.Part) ([]*message.Part, error) {
newBytes, err := c.comp(c.level, msg.AsBytes())
if err != nil {
c.log.Errorf("Failed to compress message: %v\n", err)
return nil, err
}
msg.SetBytes(newBytes)
return []*message.Part{msg}, nil
}
func (c *compressProc) Close(context.Context) error {
return nil
}