-
Notifications
You must be signed in to change notification settings - Fork 792
/
service_inline.go
131 lines (117 loc) · 4.64 KB
/
service_inline.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
/*
* Copyright 2024 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 server
import (
"context"
"reflect"
"unsafe"
"github.com/bytedance/gopkg/cloud/metainfo"
"github.com/cloudwego/kitex/pkg/consts"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/utils"
)
var localAddr = utils.NewNetAddr("tcp", "127.0.0.1")
var invocationType = reflect.TypeOf(rpcinfo.NewServerInvocation()).Elem()
func constructServerCtxWithMetadata(cliCtx context.Context) (serverCtx context.Context) {
serverCtx = context.Background()
// metainfo
// forward transmission
kvs := make(map[string]string, 16)
metainfo.SaveMetaInfoToMap(cliCtx, kvs)
if len(kvs) > 0 {
serverCtx = metainfo.SetMetaInfoFromMap(serverCtx, kvs)
}
serverCtx = metainfo.TransferForward(serverCtx)
// reverse transmission, backward mark
serverCtx = metainfo.WithBackwardValuesToSend(serverCtx)
return serverCtx
}
func (s *server) constructServerRPCInfo(svrCtx context.Context, cliRPCInfo rpcinfo.RPCInfo) (newServerCtx context.Context, svrRPCInfo rpcinfo.RPCInfo) {
rpcStats := rpcinfo.AsMutableRPCStats(rpcinfo.NewRPCStats())
if s.opt.StatsLevel != nil {
rpcStats.SetLevel(*s.opt.StatsLevel)
}
// Export read-only views to external users and keep a mapping for internal users.
svrRPCInfo = rpcinfo.NewRPCInfo(
rpcinfo.EmptyEndpointInfo(),
rpcinfo.FromBasicInfo(s.opt.Svr),
rpcinfo.NewServerInvocation(),
rpcinfo.AsMutableRPCConfig(s.opt.Configs).Clone().ImmutableView(),
rpcStats.ImmutableView(),
)
rpcinfo.AsMutableEndpointInfo(svrRPCInfo.From()).SetAddress(localAddr)
svrCtx = rpcinfo.NewCtxWithRPCInfo(svrCtx, svrRPCInfo)
// handle common rpcinfo
method := cliRPCInfo.To().Method()
if ink, ok := svrRPCInfo.Invocation().(rpcinfo.InvocationSetter); ok {
ink.SetMethodName(method)
ink.SetServiceName(cliRPCInfo.Invocation().Extra(consts.SERVICE_INLINE_SERVICE_NAME).(string))
}
rpcinfo.AsMutableEndpointInfo(svrRPCInfo.To()).SetMethod(method)
svrCtx = context.WithValue(svrCtx, consts.CtxKeyMethod, method)
return svrCtx, svrRPCInfo
}
func (s *server) BuildServiceInlineInvokeChain() endpoint.Endpoint {
svrTraceCtl := s.opt.TracerCtl
if svrTraceCtl == nil {
svrTraceCtl = &rpcinfo.TraceController{}
}
innerHandlerEp := s.invokeHandleEndpoint()
mw := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, resp interface{}) (err error) {
serverCtx := constructServerCtxWithMetadata(ctx)
defer func() {
// backward key
kvs := metainfo.AllBackwardValuesToSend(serverCtx)
if len(kvs) > 0 {
metainfo.SetBackwardValuesFromMap(ctx, kvs)
}
}()
ptr := ctx.Value(consts.SERVICE_INLINE_RPCINFO_KEY).(unsafe.Pointer)
cliRPCInfo := *(*rpcinfo.RPCInfo)(ptr)
serverCtx, svrRPCInfo := s.constructServerRPCInfo(serverCtx, cliRPCInfo)
defer func() {
rpcinfo.PutRPCInfo(svrRPCInfo)
}()
// server trace
serverCtx = svrTraceCtl.DoStart(serverCtx, svrRPCInfo)
serverCtx = context.WithValue(serverCtx, consts.SERVICE_INLINE_DATA_KEY, ctx.Value(consts.SERVICE_INLINE_DATA_KEY))
err = next(serverCtx, req, resp)
bizErr := svrRPCInfo.Invocation().BizStatusErr()
if bizErr != nil {
// In service inline scenario, cliRPCInfo is upstream rpcinfo and the dep is isolated.
// We forcibly converted it to downstream rpcinfo through unsafe.Pointer,
// so the _type in iface still points to the upstream type, which makes
// cliRPCInfo.Invocation().(rpcinfo.InvocationSetter) cannot be asserted.
//
// To solve this problem, we need init a new invocation from reflect.NewAt to
// pass the type assertion
invI := reflect.NewAt(invocationType, unsafe.Pointer(reflect.ValueOf(cliRPCInfo.Invocation()).Pointer())).Interface()
if cliSetter, ok := invI.(rpcinfo.InvocationSetter); ok {
cliSetter.SetBizStatusErr(bizErr)
}
}
// finish server trace
// contextServiceInlineHandler may convert nil err to non nil err, so handle trace here
svrTraceCtl.DoFinish(serverCtx, svrRPCInfo, err)
return
}
}
mws := []endpoint.Middleware{mw}
mws = append(mws, s.mws...)
return endpoint.Chain(mws...)(innerHandlerEp)
}