-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracker.go
208 lines (185 loc) · 5.42 KB
/
tracker.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
package tracker
import (
"context"
"sync"
"github.com/apache/thrift/lib/go/thrift"
"github.com/eleme/thrift-tracker/tracking"
"github.com/google/uuid"
)
type ctxKey string
const (
CtxKeySequenceID ctxKey = "__thrift_tracking_sequence_id"
CtxKeyRequestID ctxKey = "__thrift_tracking_request_id"
CtxKeyRequestMeta ctxKey = "__thrift_tracking_request_meta"
// CtxKeyResponseMeta ctxKey = "__thrift_tracking_response_meta"
TrackingAPIName string = "__thriftpy_tracing_method_name__v2"
)
type HandShaker interface {
Negotiation(curSeqID int32, iprot, oprot thrift.TProtocol) error
TryUpgrade(seqID int32, iprot, oprot thrift.TProtocol) (bool, thrift.TException)
RequestHeaderSupported() bool
// ResponseHeaderSupported() bool
}
type Tracker interface {
HandShaker
RequestSeqIDFromCtx(ctx context.Context) (string, string)
TryReadRequestHeader(iprot thrift.TProtocol) (context.Context, error) // context will pass into service handler
TryWriteRequestHeader(ctx context.Context, oprot thrift.TProtocol) error
// TryReadResponseHeader(iprot thrift.TProtocol) error
// TryWriteResponseHeader(ctx context.Context, oprot thrift.TProtocol) error
}
type NewTrackerFactoryFunc func(name string) func() Tracker
type SimpleTracker struct {
mu *sync.RWMutex
upgraded bool
name string
}
func NewSimpleTrackerFactory(name string) func() Tracker {
return func() Tracker {
return NewSimpleTracker(name)
}
}
func NewSimpleTracker(name string) Tracker {
return &SimpleTracker{
mu: &sync.RWMutex{},
upgraded: false,
name: name,
}
}
func (t *SimpleTracker) Negotiation(curSeqID int32, iprot, oprot thrift.TProtocol) error {
// send
if err := oprot.WriteMessageBegin(TrackingAPIName, thrift.CALL, curSeqID); err != nil {
return err
}
args := tracking.NewUpgradeArgs_()
args.AppID = t.name
if err := args.Write(oprot); err != nil {
return err
}
if err := oprot.WriteMessageEnd(); err != nil {
return err
}
if err := oprot.Flush(); err != nil {
return err
}
// recv
method, mTypeID, seqID, err := iprot.ReadMessageBegin()
if err != nil {
return err
}
if method != TrackingAPIName {
return thrift.NewTApplicationException(thrift.WRONG_METHOD_NAME,
"tracker negotiation failed: wrong method name")
}
if curSeqID != seqID {
return thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID,
"tracker negotiation failed: out of sequence response")
}
if mTypeID == thrift.EXCEPTION {
err0 := thrift.NewTApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION,
"Unknown Exception")
var err1 thrift.TApplicationException
if err1, err = err0.Read(iprot); err != nil {
return err
}
if err = iprot.ReadMessageEnd(); err != nil {
return err
}
if err1.TypeId() == thrift.UNKNOWN_METHOD { // server does not support tracker, ignore
return nil
}
return err1
}
if mTypeID != thrift.REPLY {
return thrift.NewTApplicationException(thrift.INVALID_MESSAGE_TYPE_EXCEPTION,
"tracker negotiation failed: invalid message type")
}
reply := tracking.NewUpgradeReply()
if err := reply.Read(iprot); err != nil {
return err
}
if err := iprot.ReadMessageEnd(); err != nil {
return err
}
t.upgradeProtocol()
return nil
}
func (t *SimpleTracker) TryUpgrade(seqID int32, iprot, oprot thrift.TProtocol) (bool, thrift.TException) {
args := tracking.NewUpgradeArgs_()
if err := args.Read(iprot); err != nil {
iprot.ReadMessageEnd()
x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())
x.Write(oprot)
oprot.WriteMessageEnd()
oprot.Flush()
return false, err
}
iprot.ReadMessageEnd()
result := tracking.NewUpgradeReply()
if err := oprot.WriteMessageBegin(TrackingAPIName, thrift.REPLY, seqID); err != nil {
return false, err
}
if err := result.Write(oprot); err != nil {
return false, err
}
if err := oprot.WriteMessageEnd(); err != nil {
return false, err
}
if err := oprot.Flush(); err != nil {
return false, err
}
t.upgradeProtocol()
return true, nil
}
func (t *SimpleTracker) upgradeProtocol() {
t.mu.Lock()
defer t.mu.Unlock()
t.upgraded = true
}
func (t *SimpleTracker) RequestHeaderSupported() bool {
t.mu.RLock()
defer t.mu.RUnlock()
return t.upgraded
}
func (t *SimpleTracker) RequestSeqIDFromCtx(ctx context.Context) (string, string) {
var reqID, seqID string
if v, ok := ctx.Value(CtxKeyRequestID).(string); ok {
reqID = v
} else {
reqID = uuid.New().String()
}
if v, ok := ctx.Value(CtxKeySequenceID).(string); ok {
seqID = v
} else {
seqID = "1"
}
return reqID, seqID
}
func (t *SimpleTracker) TryReadRequestHeader(iprot thrift.TProtocol) (context.Context, error) {
if !t.RequestHeaderSupported() {
return context.TODO(), nil
}
header := tracking.NewRequestHeader()
if err := header.Read(iprot); err != nil {
return context.TODO(), err
}
ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyRequestID, header.GetRequestID())
ctx = context.WithValue(ctx, CtxKeySequenceID, header.GetSeq())
ctx = context.WithValue(ctx, CtxKeyRequestMeta, header.GetMeta())
return ctx, nil
}
func (t *SimpleTracker) TryWriteRequestHeader(ctx context.Context, oprot thrift.TProtocol) error {
if !t.RequestHeaderSupported() {
return nil
}
header := tracking.NewRequestHeader()
if meta, ok := ctx.Value(CtxKeyRequestMeta).(map[string]string); ok {
header.Meta = make(map[string]string, len(meta))
for k, v := range meta {
header.Meta[k] = v
}
}
header.RequestID, header.Seq = t.RequestSeqIDFromCtx(ctx)
return header.Write(oprot)
}