forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarsprotocol.go
executable file
·184 lines (165 loc) · 5.65 KB
/
tarsprotocol.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
package tars
import (
"bytes"
"context"
"encoding/binary"
"time"
"github.com/TarsCloud/TarsGo/tars/protocol"
"github.com/TarsCloud/TarsGo/tars/protocol/codec"
"github.com/TarsCloud/TarsGo/tars/protocol/res/basef"
"github.com/TarsCloud/TarsGo/tars/protocol/res/requestf"
"github.com/TarsCloud/TarsGo/tars/util/current"
)
type dispatch interface {
Dispatch(context.Context, interface{}, *requestf.RequestPacket, *requestf.ResponsePacket, bool) error
}
// TarsProtocol is struct for dispatch with tars protocol.
type TarsProtocol struct {
dispatcher dispatch
serverImp interface{}
withContext bool
}
// NewTarsProtocol return a TarsProtocol with dipatcher and implement interface.
// withContext explain using context or not.
func NewTarsProtocol(dispatcher dispatch, imp interface{}, withContext bool) *TarsProtocol {
s := &TarsProtocol{dispatcher: dispatcher, serverImp: imp, withContext: withContext}
return s
}
// Invoke puts the request as []byte and call the dispather, and then return the response as []byte.
func (s *TarsProtocol) Invoke(ctx context.Context, req []byte) (rsp []byte) {
defer CheckPanic()
reqPackage := requestf.RequestPacket{}
rspPackage := requestf.ResponsePacket{}
is := codec.NewReader(req[4:])
reqPackage.ReadFrom(is)
if reqPackage.HasMessageType(basef.TARSMESSAGETYPEDYED) {
if dyeingKey, ok := reqPackage.Status[current.STATUS_DYED_KEY]; ok {
if ok := current.SetDyeingKey(ctx, dyeingKey); !ok {
TLOG.Error("dyeing-debug: set dyeing key in current status error, dyeing key:", dyeingKey)
}
}
}
if reqPackage.CPacketType == basef.TARSONEWAY {
defer func() func() {
beginTime := time.Now().UnixNano() / 1e6
return func() {
endTime := time.Now().UnixNano() / 1e6
ReportStatFromServer(reqPackage.SFuncName, "one_way_client", rspPackage.IRet, endTime-beginTime)
}
}()()
}
if reqPackage.SFuncName == "tars_ping" {
rspPackage.IVersion = reqPackage.IVersion
//rspPackage.CPacketType = basef.TARSNORMAL
rspPackage.IRequestId = reqPackage.IRequestId
rspPackage.IRet = 0
} else {
var err error
if s.withContext {
ok := current.SetRequestStatus(ctx, reqPackage.Status)
if !ok {
TLOG.Error("Set reqeust status in context fail!")
}
ok = current.SetRequestContext(ctx, reqPackage.Context)
if !ok {
TLOG.Error("Set request context in context fail!")
}
}
if allFilters.sf != nil {
err = allFilters.sf(ctx, s.dispatcher.Dispatch, s.serverImp, &reqPackage, &rspPackage, s.withContext)
} else if sf := getMiddlewareServerFilter(); sf != nil {
err = sf(ctx, s.dispatcher.Dispatch, s.serverImp, &reqPackage, &rspPackage, s.withContext)
} else {
// execute pre server filters
for i, v := range allFilters.preSfs {
err = v(ctx, s.dispatcher.Dispatch, s.serverImp, &reqPackage, &rspPackage, s.withContext)
if err != nil {
TLOG.Errorf("Pre filter error, No.%v, err: %v", i, err.Error())
}
}
err = s.dispatcher.Dispatch(ctx, s.serverImp, &reqPackage, &rspPackage, s.withContext)
// execute post server filters
for i, v := range allFilters.postSfs {
err = v(ctx, s.dispatcher.Dispatch, s.serverImp, &reqPackage, &rspPackage, s.withContext)
if err != nil {
TLOG.Errorf("Post filter error, No.%v, err: %v", i, err.Error())
}
}
}
if err != nil {
TLOG.Errorf("RequestID:%d, Found err: %v", reqPackage.IRequestId, err)
//rspPackage.IVersion = basef.TARSVERSION
rspPackage.IVersion = reqPackage.IVersion
rspPackage.CPacketType = basef.TARSNORMAL
rspPackage.IRequestId = reqPackage.IRequestId
rspPackage.IRet = 1
rspPackage.SResultDesc = err.Error()
if tarsErr, ok := err.(*Error); ok {
rspPackage.IRet = tarsErr.Code
}
}
}
//return ctype
rspPackage.CPacketType = reqPackage.CPacketType
ok := current.SetPacketTypeFromContext(ctx, rspPackage.CPacketType)
if !ok {
TLOG.Error("SetPacketType in context fail!")
}
return s.rsp2Byte(&rspPackage)
}
func (s *TarsProtocol) req2Byte(rsp *requestf.ResponsePacket) []byte {
req := requestf.RequestPacket{}
req.IVersion = rsp.IVersion
req.IRequestId = rsp.IRequestId
req.IMessageType = rsp.IMessageType
req.CPacketType = rsp.CPacketType
req.Context = rsp.Context
req.Status = rsp.Status
req.SBuffer = rsp.SBuffer
os := codec.NewBuffer()
req.WriteTo(os)
bs := os.ToBytes()
sbuf := bytes.NewBuffer(nil)
sbuf.Write(make([]byte, 4))
sbuf.Write(bs)
len := sbuf.Len()
binary.BigEndian.PutUint32(sbuf.Bytes(), uint32(len))
return sbuf.Bytes()
}
func (s *TarsProtocol) rsp2Byte(rsp *requestf.ResponsePacket) []byte {
if rsp.IVersion == basef.TUPVERSION {
return s.req2Byte(rsp)
}
os := codec.NewBuffer()
rsp.WriteTo(os)
bs := os.ToBytes()
sbuf := bytes.NewBuffer(nil)
sbuf.Write(make([]byte, 4))
sbuf.Write(bs)
len := sbuf.Len()
binary.BigEndian.PutUint32(sbuf.Bytes(), uint32(len))
return sbuf.Bytes()
}
// ParsePackage parse the []byte according to the tars protocol.
// returns header length and package integrity condition (PACKAGE_LESS | PACKAGE_FULL | PACKAGE_ERROR)
func (s *TarsProtocol) ParsePackage(buff []byte) (int, int) {
return protocol.TarsRequest(buff)
}
// InvokeTimeout indicates how to deal with timeout.
func (s *TarsProtocol) InvokeTimeout(pkg []byte) []byte {
rspPackage := requestf.ResponsePacket{}
rspPackage.IRet = 1
rspPackage.SResultDesc = "server invoke timeout"
return s.rsp2Byte(&rspPackage)
}
// GetCloseMsg return a package to close connection
func (s *TarsProtocol) GetCloseMsg() []byte {
rspPackage := requestf.ResponsePacket{}
rspPackage.IRequestId = 0
rspPackage.SResultDesc = reconnectMsg
return s.rsp2Byte(&rspPackage)
}
// DoClose be called when close connection
func (s *TarsProtocol) DoClose(ctx context.Context) {
TLOG.Debug("DoClose!")
}