-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathdefault_server_handler.go
357 lines (329 loc) · 12 KB
/
default_server_handler.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package trans
import (
"context"
"errors"
"fmt"
"net"
"runtime/debug"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/serviceinfo"
"github.com/cloudwego/kitex/pkg/stats"
)
// NewDefaultSvrTransHandler to provide default impl of svrTransHandler, it can be reused in netpoll, shm-ipc, framework-sdk extensions
func NewDefaultSvrTransHandler(opt *remote.ServerOption, ext Extension) (remote.ServerTransHandler, error) {
svrHdlr := &svrTransHandler{
opt: opt,
codec: opt.Codec,
svcSearchMap: opt.SvcSearchMap,
targetSvcInfo: opt.TargetSvcInfo,
ext: ext,
}
if svrHdlr.opt.TracerCtl == nil {
// init TraceCtl when it is nil, or it will lead some unit tests panic
svrHdlr.opt.TracerCtl = &rpcinfo.TraceController{}
}
return svrHdlr, nil
}
type svrTransHandler struct {
opt *remote.ServerOption
svcSearchMap map[string]*serviceinfo.ServiceInfo
targetSvcInfo *serviceinfo.ServiceInfo
inkHdlFunc endpoint.Endpoint
codec remote.Codec
transPipe *remote.TransPipeline
ext Extension
}
// Write implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) Write(ctx context.Context, conn net.Conn, sendMsg remote.Message) (nctx context.Context, err error) {
var bufWriter remote.ByteBuffer
ri := sendMsg.RPCInfo()
rpcinfo.Record(ctx, ri, stats.WriteStart, nil)
defer func() {
t.ext.ReleaseBuffer(bufWriter, err)
rpcinfo.Record(ctx, ri, stats.WriteFinish, err)
}()
svcInfo := sendMsg.ServiceInfo()
if svcInfo != nil {
if methodInfo, _ := GetMethodInfo(ri, svcInfo); methodInfo != nil {
if methodInfo.OneWay() {
return ctx, nil
}
}
}
bufWriter = t.ext.NewWriteByteBuffer(ctx, conn, sendMsg)
err = t.codec.Encode(ctx, sendMsg, bufWriter)
if err != nil {
return ctx, err
}
return ctx, bufWriter.Flush()
}
// Read implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) Read(ctx context.Context, conn net.Conn, recvMsg remote.Message) (nctx context.Context, err error) {
var bufReader remote.ByteBuffer
defer func() {
t.ext.ReleaseBuffer(bufReader, err)
rpcinfo.Record(ctx, recvMsg.RPCInfo(), stats.ReadFinish, err)
}()
rpcinfo.Record(ctx, recvMsg.RPCInfo(), stats.ReadStart, nil)
bufReader = t.ext.NewReadByteBuffer(ctx, conn, recvMsg)
if codec, ok := t.codec.(remote.MetaDecoder); ok {
if err = codec.DecodeMeta(ctx, recvMsg, bufReader); err == nil {
if t.opt.Profiler != nil && t.opt.ProfilerTransInfoTagging != nil && recvMsg.TransInfo() != nil {
var tags []string
ctx, tags = t.opt.ProfilerTransInfoTagging(ctx, recvMsg)
ctx = t.opt.Profiler.Tag(ctx, tags...)
}
err = codec.DecodePayload(ctx, recvMsg, bufReader)
}
} else {
err = t.codec.Decode(ctx, recvMsg, bufReader)
}
if err != nil {
recvMsg.Tags()[remote.ReadFailed] = true
return ctx, err
}
return ctx, nil
}
func (t *svrTransHandler) newCtxWithRPCInfo(ctx context.Context, conn net.Conn) (context.Context, rpcinfo.RPCInfo) {
if rpcinfo.PoolEnabled() { // reuse per-connection rpcinfo
return ctx, rpcinfo.GetRPCInfo(ctx)
// delayed reinitialize for faster response
}
// new rpcinfo if reuse is disabled
ri := t.opt.InitOrResetRPCInfoFunc(nil, conn.RemoteAddr())
return rpcinfo.NewCtxWithRPCInfo(ctx, ri), ri
}
// OnRead implements the remote.ServerTransHandler interface.
// The connection should be closed after returning error.
func (t *svrTransHandler) OnRead(ctx context.Context, conn net.Conn) (err error) {
ctx, ri := t.newCtxWithRPCInfo(ctx, conn)
t.ext.SetReadTimeout(ctx, conn, ri.Config(), remote.Server)
var recvMsg remote.Message
var sendMsg remote.Message
closeConnOutsideIfErr := true
defer func() {
panicErr := recover()
var wrapErr error
if panicErr != nil {
stack := string(debug.Stack())
if conn != nil {
ri := rpcinfo.GetRPCInfo(ctx)
rService, rAddr := getRemoteInfo(ri, conn)
klog.CtxErrorf(ctx, "KITEX: panic happened, remoteAddress=%s, remoteService=%s, error=%v\nstack=%s", rAddr, rService, panicErr, stack)
} else {
klog.CtxErrorf(ctx, "KITEX: panic happened, error=%v\nstack=%s", panicErr, stack)
}
if err != nil {
wrapErr = kerrors.ErrPanic.WithCauseAndStack(fmt.Errorf("[happened in OnRead] %s, last error=%s", panicErr, err.Error()), stack)
} else {
wrapErr = kerrors.ErrPanic.WithCauseAndStack(fmt.Errorf("[happened in OnRead] %s", panicErr), stack)
}
}
t.finishTracer(ctx, ri, err, panicErr)
t.finishProfiler(ctx)
remote.RecycleMessage(recvMsg)
remote.RecycleMessage(sendMsg)
// reset rpcinfo for reuse
if rpcinfo.PoolEnabled() {
t.opt.InitOrResetRPCInfoFunc(ri, conn.RemoteAddr())
}
if wrapErr != nil {
err = wrapErr
}
if err != nil && !closeConnOutsideIfErr {
err = nil
}
}()
ctx = t.startTracer(ctx, ri)
ctx = t.startProfiler(ctx)
recvMsg = remote.NewMessageWithNewer(t.targetSvcInfo, t.svcSearchMap, ri, remote.Call, remote.Server, t.opt.RefuseTrafficWithoutServiceName)
recvMsg.SetPayloadCodec(t.opt.PayloadCodec)
ctx, err = t.transPipe.Read(ctx, conn, recvMsg)
if err != nil {
t.writeErrorReplyIfNeeded(ctx, recvMsg, conn, err, ri, true)
// t.OnError(ctx, err, conn) will be executed at outer function when transServer close the conn
return err
}
svcInfo := recvMsg.ServiceInfo()
// heartbeat processing
// recvMsg.MessageType would be set to remote.Heartbeat in previous Read procedure
// if specified codec support heartbeat
if recvMsg.MessageType() == remote.Heartbeat {
sendMsg = remote.NewMessage(nil, svcInfo, ri, remote.Heartbeat, remote.Server)
} else {
// reply processing
var methodInfo serviceinfo.MethodInfo
if methodInfo, err = GetMethodInfo(ri, svcInfo); err != nil {
// it won't be err, because the method has been checked in decode, err check here just do defensive inspection
t.writeErrorReplyIfNeeded(ctx, recvMsg, conn, err, ri, true)
// for proxy case, need read actual remoteAddr, error print must exec after writeErrorReplyIfNeeded,
// t.OnError(ctx, err, conn) will be executed at outer function when transServer close the conn
return err
}
if methodInfo.OneWay() {
sendMsg = remote.NewMessage(nil, svcInfo, ri, remote.Reply, remote.Server)
} else {
sendMsg = remote.NewMessage(methodInfo.NewResult(), svcInfo, ri, remote.Reply, remote.Server)
}
ctx, err = t.transPipe.OnMessage(ctx, recvMsg, sendMsg)
if err != nil {
// error cannot be wrapped to print here, so it must exec before NewTransError
t.OnError(ctx, err, conn)
err = remote.NewTransError(remote.InternalError, err)
if closeConn := t.writeErrorReplyIfNeeded(ctx, recvMsg, conn, err, ri, false); closeConn {
return err
}
// connection don't need to be closed when the error is return by the server handler
closeConnOutsideIfErr = false
return
}
}
remote.FillSendMsgFromRecvMsg(recvMsg, sendMsg)
if ctx, err = t.transPipe.Write(ctx, conn, sendMsg); err != nil {
// t.OnError(ctx, err, conn) will be executed at outer function when transServer close the conn
return err
}
return
}
// OnMessage implements the remote.ServerTransHandler interface.
// msg is the decoded instance, such as Arg and Result.
func (t *svrTransHandler) OnMessage(ctx context.Context, args, result remote.Message) (context.Context, error) {
err := t.inkHdlFunc(ctx, args.Data(), result.Data())
return ctx, err
}
// OnActive implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) OnActive(ctx context.Context, conn net.Conn) (context.Context, error) {
// init rpcinfo
ri := t.opt.InitOrResetRPCInfoFunc(nil, conn.RemoteAddr())
return rpcinfo.NewCtxWithRPCInfo(ctx, ri), nil
}
// OnInactive implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) OnInactive(ctx context.Context, conn net.Conn) {
// recycle rpcinfo
rpcinfo.PutRPCInfo(rpcinfo.GetRPCInfo(ctx))
}
// OnError implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) OnError(ctx context.Context, err error, conn net.Conn) {
ri := rpcinfo.GetRPCInfo(ctx)
rService, rAddr := getRemoteInfo(ri, conn)
if t.ext.IsRemoteClosedErr(err) {
// it should not regard error which cause by remote connection closed as server error
if ri == nil {
return
}
remote := rpcinfo.AsMutableEndpointInfo(ri.From())
remote.SetTag(rpcinfo.RemoteClosedTag, "1")
} else {
var de *kerrors.DetailedError
if ok := errors.As(err, &de); ok && de.Stack() != "" {
klog.CtxErrorf(ctx, "KITEX: processing request error, remoteService=%s, remoteAddr=%v, error=%s\nstack=%s", rService, rAddr, err.Error(), de.Stack())
} else {
klog.CtxErrorf(ctx, "KITEX: processing request error, remoteService=%s, remoteAddr=%v, error=%s", rService, rAddr, err.Error())
}
}
}
// SetInvokeHandleFunc implements the remote.InvokeHandleFuncSetter interface.
func (t *svrTransHandler) SetInvokeHandleFunc(inkHdlFunc endpoint.Endpoint) {
t.inkHdlFunc = inkHdlFunc
}
// SetPipeline implements the remote.ServerTransHandler interface.
func (t *svrTransHandler) SetPipeline(p *remote.TransPipeline) {
t.transPipe = p
}
func (t *svrTransHandler) writeErrorReplyIfNeeded(
ctx context.Context, recvMsg remote.Message, conn net.Conn, err error, ri rpcinfo.RPCInfo, doOnMessage bool,
) (shouldCloseConn bool) {
if cn, ok := conn.(remote.IsActive); ok && !cn.IsActive() {
// conn is closed, no need reply
return
}
svcInfo := recvMsg.ServiceInfo()
if svcInfo != nil {
if methodInfo, _ := GetMethodInfo(ri, svcInfo); methodInfo != nil {
if methodInfo.OneWay() {
return
}
}
}
transErr, isTransErr := err.(*remote.TransError)
if !isTransErr {
return
}
errMsg := remote.NewMessage(transErr, svcInfo, ri, remote.Exception, remote.Server)
remote.FillSendMsgFromRecvMsg(recvMsg, errMsg)
if doOnMessage {
// if error happen before normal OnMessage, exec it to transfer header trans info into rpcinfo
t.transPipe.OnMessage(ctx, recvMsg, errMsg)
}
ctx, err = t.transPipe.Write(ctx, conn, errMsg)
if err != nil {
klog.CtxErrorf(ctx, "KITEX: write error reply failed, remote=%s, error=%s", conn.RemoteAddr(), err.Error())
return true
}
return
}
func (t *svrTransHandler) startTracer(ctx context.Context, ri rpcinfo.RPCInfo) context.Context {
c := t.opt.TracerCtl.DoStart(ctx, ri)
return c
}
func (t *svrTransHandler) finishTracer(ctx context.Context, ri rpcinfo.RPCInfo, err error, panicErr interface{}) {
rpcStats := rpcinfo.AsMutableRPCStats(ri.Stats())
if rpcStats == nil {
return
}
if panicErr != nil {
rpcStats.SetPanicked(panicErr)
}
if err != nil && t.ext.IsRemoteClosedErr(err) {
// it should not regard the error which caused by remote connection closed as server error
err = nil
}
t.opt.TracerCtl.DoFinish(ctx, ri, err)
// for server side, rpcinfo is reused on connection, clear the rpc stats info but keep the level config
sl := ri.Stats().Level()
rpcStats.Reset()
rpcStats.SetLevel(sl)
}
func (t *svrTransHandler) startProfiler(ctx context.Context) context.Context {
if t.opt.Profiler == nil {
return ctx
}
return t.opt.Profiler.Prepare(ctx)
}
func (t *svrTransHandler) finishProfiler(ctx context.Context) {
if t.opt.Profiler == nil {
return
}
t.opt.Profiler.Untag(ctx)
}
func getRemoteInfo(ri rpcinfo.RPCInfo, conn net.Conn) (string, net.Addr) {
rAddr := conn.RemoteAddr()
if ri == nil {
return "", rAddr
}
if rAddr != nil && rAddr.Network() == "unix" {
if ri.From().Address() != nil {
rAddr = ri.From().Address()
}
}
return ri.From().ServiceName(), rAddr
}