-
Notifications
You must be signed in to change notification settings - Fork 23
/
segment_separator.go
63 lines (54 loc) · 1.41 KB
/
segment_separator.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
package xray
import (
"fmt"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
// segmentDivider is used to divide segments slice into multiple slices.
// PutTraceSegments API has 64kb limitation for segments.
// see: http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
type segmentDivider struct {
totalSize int
list [][]*string
currentIndex int
currentByte int
}
func createSegmentDivider(segments []*Segment) (*segmentDivider, error) {
size := len(segments)
sep := &segmentDivider{
list: [][]*string{make([]*string, 0, size)},
totalSize: size,
}
errList := newErrors()
for _, s := range segments {
if !s.Trace {
continue
}
byt, err := s.ToJSON()
if err != nil {
errList.Add(fmt.Errorf("error on segment.ToJSON(); segment=%+v; error=%s;", s, err.Error()))
continue
}
sep.append(byt)
}
if errList.HasError() {
return sep, errList
}
return sep, nil
}
func (s *segmentDivider) GetResult() [][]*string {
return s.list
}
func (s *segmentDivider) isOverByte() bool {
const maxByte = 61440 // 60kb
return s.currentByte > maxByte
}
func (s *segmentDivider) append(byt []byte) {
bytesize := len(byt)
s.currentByte += bytesize
if s.isOverByte() {
s.currentIndex++
s.currentByte = bytesize
s.list[s.currentIndex] = make([]*string, 0, s.totalSize)
}
s.list[s.currentIndex] = append(s.list[s.currentIndex], pointers.String(string(byt)))
}