/
buffer.go
160 lines (135 loc) · 3.48 KB
/
buffer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package splunkhecexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter"
import (
"bytes"
"compress/gzip"
"errors"
"io"
"sync"
)
var (
errOverCapacity = errors.New("over capacity")
)
type buffer interface {
io.Writer
io.Reader
io.Closer
Reset()
Len() int
Empty() bool
}
type cancellableBytesWriter struct {
innerWriter *bytes.Buffer
maxCapacity uint
}
func (c *cancellableBytesWriter) Write(b []byte) (int, error) {
if c.maxCapacity == 0 {
return c.innerWriter.Write(b)
}
if c.innerWriter.Len()+len(b) > int(c.maxCapacity) {
return 0, errOverCapacity
}
return c.innerWriter.Write(b)
}
func (c *cancellableBytesWriter) Read(p []byte) (int, error) {
return c.innerWriter.Read(p)
}
func (c *cancellableBytesWriter) Reset() {
c.innerWriter.Reset()
}
func (c *cancellableBytesWriter) Close() error {
return nil
}
func (c *cancellableBytesWriter) Len() int {
return c.innerWriter.Len()
}
func (c *cancellableBytesWriter) Empty() bool {
return c.innerWriter.Len() == 0
}
type cancellableGzipWriter struct {
innerBuffer *bytes.Buffer
innerWriter *gzip.Writer
maxCapacity uint
rawLen int
}
func (c *cancellableGzipWriter) Write(b []byte) (int, error) {
if c.maxCapacity == 0 {
c.rawLen += len(b)
return c.innerWriter.Write(b)
}
// if we see that at a 50% compression rate, we'd be over max capacity, start flushing.
if c.rawLen > 0 && (c.rawLen+len(b))/2 > int(c.maxCapacity) {
// we flush so the length of the underlying buffer is accurate.
if err := c.innerWriter.Flush(); err != nil {
return 0, err
}
}
// we find that the new content uncompressed, added to our buffer, would overflow our max capacity.
if c.innerBuffer.Len()+len(b) > int(c.maxCapacity) {
// so we create a copy of our content and add this new data, compressed, to check that it fits.
copyBuf := bytes.NewBuffer(make([]byte, 0, c.maxCapacity+bufCapPadding))
copyBuf.Write(c.innerBuffer.Bytes())
writerCopy := gzip.NewWriter(copyBuf)
writerCopy.Reset(copyBuf)
if _, err := writerCopy.Write(b); err != nil {
return 0, err
}
if err := writerCopy.Flush(); err != nil {
return 0, err
}
// we find that even compressed, the data overflows.
if copyBuf.Len() > int(c.maxCapacity) {
return 0, errOverCapacity
}
}
c.rawLen += len(b)
return c.innerWriter.Write(b)
}
func (c *cancellableGzipWriter) Read(p []byte) (int, error) {
return c.innerBuffer.Read(p)
}
func (c *cancellableGzipWriter) Reset() {
c.innerBuffer.Reset()
c.innerWriter.Reset(c.innerBuffer)
c.rawLen = 0
}
func (c *cancellableGzipWriter) Close() error {
return c.innerWriter.Close()
}
func (c *cancellableGzipWriter) Len() int {
return c.innerBuffer.Len()
}
func (c *cancellableGzipWriter) Empty() bool {
return c.rawLen == 0
}
// bufferPool is a pool of buffer objects.
type bufferPool struct {
pool *sync.Pool
}
func (p bufferPool) get() buffer {
return p.pool.Get().(buffer)
}
func (p bufferPool) put(bf buffer) {
p.pool.Put(bf)
}
func newBufferPool(bufCap uint, compressionEnabled bool) bufferPool {
return bufferPool{
&sync.Pool{
New: func() any {
innerBuffer := &bytes.Buffer{}
if compressionEnabled {
return &cancellableGzipWriter{
innerBuffer: innerBuffer,
innerWriter: gzip.NewWriter(innerBuffer),
maxCapacity: bufCap,
}
}
return &cancellableBytesWriter{
innerWriter: innerBuffer,
maxCapacity: bufCap,
}
},
},
}
}