/
protocol.go
213 lines (182 loc) · 4.42 KB
/
protocol.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cocaine12
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"reflect"
)
const (
traceId = 80
spanId = 81
parentId = 82
)
var (
traceIdName = []byte("trace_id")
spanIdName = []byte("span_id")
parentIdName = []byte("parent_id")
traceValueMap = map[uint64]struct{}{
traceId: struct{}{},
spanId: struct{}{},
parentId: struct{}{},
}
)
var (
ErrInvalidHeaderLength = errors.New("invalid header size")
ErrInvalidHeaderType = errors.New("invalid header type")
ErrInvalidTraceType = errors.New("invalid trace header number type")
ErrInvalidTraceNumber = errors.New("invalid trace header number")
ErrInvalidTraceValueType = errors.New("invalid trace value type")
ErrNotAllTracesPresent = errors.New("not all trace values present")
)
// CommonMessageInfo consists of a session number and a message type
type CommonMessageInfo struct {
// Session id
Session uint64
// Message type number
MsgType uint64
}
func getTrace(header interface{}) (uint64, []byte, error) {
switch t := header.(type) {
case uint:
return uint64(t), nil, nil
case uint32:
return uint64(t), nil, nil
case uint64:
return t, nil, nil
case int:
return uint64(t), nil, nil
case int32:
return uint64(t), nil, nil
case int64:
return uint64(t), nil, nil
case []interface{}:
if len(t) != 3 {
return 0, nil, ErrInvalidHeaderLength
}
var (
traceNum uint64
traceVal []byte
)
switch num := t[1].(type) {
case uint:
traceNum = uint64(num)
case uint32:
traceNum = uint64(num)
case uint64:
traceNum = num
case int:
traceNum = uint64(num)
case int32:
traceNum = uint64(num)
case int64:
traceNum = uint64(num)
case []byte:
decodedTraceNum, err := decodeRawTraceName(num)
if err != nil {
return 0, nil, err
}
traceNum = decodedTraceNum
default:
fmt.Println(reflect.TypeOf(t[1]))
return 0, nil, ErrInvalidTraceType
}
if _, ok := traceValueMap[traceNum]; !ok {
return 0, nil, ErrInvalidTraceNumber
}
switch val := t[2].(type) {
case []byte:
traceVal = val
case string:
traceVal = []byte(val)
default:
return 0, nil, ErrInvalidTraceValueType
}
return traceNum, traceVal, nil
default:
fmt.Printf("%v\n", reflect.TypeOf(t))
}
return 0, nil, ErrInvalidHeaderType
}
func decodeRawTraceName(name []byte) (uint64, error) {
if bytes.Equal(name, traceIdName) {
return traceId, nil
} else if bytes.Equal(name, spanIdName) {
return spanId, nil
} else if bytes.Equal(name, parentIdName) {
return parentId, nil
}
return 0, ErrInvalidTraceType
}
type CocaineHeaders []interface{}
func (h CocaineHeaders) getTraceData() (traceInfo TraceInfo, err error) {
var i = 0
for _, header := range h {
number, buffer, zerr := getTrace(header)
if zerr != nil {
continue
}
switch number {
case traceId:
if traceInfo.Trace, err = decodeTracingId(buffer); err != nil {
return
}
case spanId:
if traceInfo.Span, err = decodeTracingId(buffer); err != nil {
return
}
case parentId:
if buffer == nil {
traceInfo.Parent = 0
} else {
if traceInfo.Parent, err = decodeTracingId(buffer); err != nil {
return
}
}
default:
continue
}
i++
if i == 3 {
return
}
}
return traceInfo, ErrNotAllTracesPresent
}
func decodeTracingId(b []byte) (uint64, error) {
var tracingId uint64
err := binary.Read(bytes.NewReader(b), binary.LittleEndian, &tracingId)
return tracingId, err
}
func traceInfoToHeaders(info *TraceInfo) (CocaineHeaders, error) {
var (
offset = 0
buff = new(bytes.Buffer)
headers = make(CocaineHeaders, 0, 3)
)
if err := binary.Write(buff, binary.LittleEndian, info.Trace); err != nil {
return headers, err
}
headers = append(headers, []interface{}{false, traceId, buff.Bytes()[offset:]})
offset = buff.Len()
if err := binary.Write(buff, binary.LittleEndian, info.Span); err != nil {
return headers, err
}
headers = append(headers, []interface{}{false, spanId, buff.Bytes()[offset:]})
offset = buff.Len()
if err := binary.Write(buff, binary.LittleEndian, info.Parent); err != nil {
return headers, err
}
headers = append(headers, []interface{}{false, parentId, buff.Bytes()[offset:]})
offset = buff.Len()
return headers, nil
}
type Message struct {
// _struct bool `codec:",toarray"`
CommonMessageInfo
Payload []interface{}
Headers CocaineHeaders
}
func (m *Message) String() string {
return fmt.Sprintf("message %v %v payload %v", m.MsgType, m.Session, m.Payload)
}