-
Notifications
You must be signed in to change notification settings - Fork 795
/
jsonthrift_codec.go
147 lines (126 loc) · 4.48 KB
/
jsonthrift_codec.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
/*
* 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 generic
import (
"context"
"sync/atomic"
"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/kitex/pkg/generic/descriptor"
"github.com/cloudwego/kitex/pkg/generic/thrift"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/codec"
"github.com/cloudwego/kitex/pkg/remote/codec/perrors"
"github.com/cloudwego/kitex/pkg/serviceinfo"
)
var (
_ remote.PayloadCodec = &jsonThriftCodec{}
_ Closer = &jsonThriftCodec{}
)
// JSONRequest alias of string
type JSONRequest = string
type jsonThriftCodec struct {
svcDsc atomic.Value // *idl
provider DescriptorProvider
codec remote.PayloadCodec
binaryWithBase64 bool
opts *Options
convOpts conv.Options // used for dynamicgo conversion
convOptsWithThriftBase conv.Options // used for dynamicgo conversion with EnableThriftBase turned on
convOptsWithException conv.Options // used for dynamicgo conversion with ConvertException turned on
dynamicgoEnabled bool
}
func newJsonThriftCodec(p DescriptorProvider, codec remote.PayloadCodec, opts *Options) (*jsonThriftCodec, error) {
svc := <-p.Provide()
c := &jsonThriftCodec{codec: codec, provider: p, binaryWithBase64: true, opts: opts, dynamicgoEnabled: false}
if dp, ok := p.(GetProviderOption); ok && dp.Option().DynamicGoEnabled {
c.dynamicgoEnabled = true
convOpts := opts.dynamicgoConvOpts
c.convOpts = convOpts
convOptsWithThriftBase := convOpts
convOptsWithThriftBase.EnableThriftBase = true
c.convOptsWithThriftBase = convOptsWithThriftBase
convOptsWithException := convOpts
convOptsWithException.ConvertException = true
c.convOptsWithException = convOptsWithException
}
c.svcDsc.Store(svc)
go c.update()
return c, nil
}
func (c *jsonThriftCodec) update() {
for {
svc, ok := <-c.provider.Provide()
if !ok {
return
}
c.svcDsc.Store(svc)
}
}
func (c *jsonThriftCodec) Marshal(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error {
method := msg.RPCInfo().Invocation().MethodName()
if method == "" {
return perrors.NewProtocolErrorWithMsg("empty methodName in thrift Marshal")
}
if msg.MessageType() == remote.Exception {
return c.codec.Marshal(ctx, msg, out)
}
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return perrors.NewProtocolErrorWithMsg("get parser ServiceDescriptor failed")
}
wm, err := thrift.NewWriteJSON(svcDsc, method, msg.RPCRole() == remote.Client)
if err != nil {
return err
}
wm.SetBase64Binary(c.binaryWithBase64)
if c.dynamicgoEnabled {
if err = wm.SetDynamicGo(svcDsc, method, &c.convOpts, &c.convOptsWithThriftBase); err != nil {
return err
}
}
msg.Data().(WithCodec).SetCodec(wm)
return c.codec.Marshal(ctx, msg, out)
}
func (c *jsonThriftCodec) Unmarshal(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error {
if err := codec.NewDataIfNeeded(serviceinfo.GenericMethod, msg); err != nil {
return err
}
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return perrors.NewProtocolErrorWithMsg("get parser ServiceDescriptor failed")
}
rm := thrift.NewReadJSON(svcDsc, msg.RPCRole() == remote.Client)
rm.SetBinaryWithBase64(c.binaryWithBase64)
// Transport protocol should be TTHeader, Framed, or TTHeaderFramed to enable dynamicgo
if c.dynamicgoEnabled && msg.PayloadLen() != 0 {
rm.SetDynamicGo(&c.convOpts, &c.convOptsWithException, msg)
}
msg.Data().(WithCodec).SetCodec(rm)
return c.codec.Unmarshal(ctx, msg, in)
}
func (c *jsonThriftCodec) getMethod(req interface{}, method string) (*Method, error) {
fnSvc, err := c.svcDsc.Load().(*descriptor.ServiceDescriptor).LookupFunctionByMethod(method)
if err != nil {
return nil, err
}
return &Method{method, fnSvc.Oneway}, nil
}
func (c *jsonThriftCodec) Name() string {
return "JSONThrift"
}
func (c *jsonThriftCodec) Close() error {
return c.provider.Close()
}