-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathrpcconfig.go
228 lines (193 loc) · 5.65 KB
/
rpcconfig.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
/*
* 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 rpcinfo
import (
"sync"
"time"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/serviceinfo"
"github.com/cloudwego/kitex/transport"
)
var (
_ MutableRPCConfig = &rpcConfig{}
_ RPCConfig = &rpcConfig{}
rpcConfigPool sync.Pool
)
// default values.
var (
defaultRPCTimeout = time.Duration(0)
defaultConnectTimeout = time.Millisecond * 50
defaultReadWriteTimeout = time.Second * 5
defaultBufferSize = 4096
defaultInteractionMode = PingPong
)
// Mask bits.
const (
BitRPCTimeout = 1 << iota
BitConnectTimeout
BitReadWriteTimeout
BitIOBufferSize
)
type InteractionMode int32
const (
PingPong InteractionMode = 0
Oneway InteractionMode = 1
Streaming InteractionMode = 2
)
// rpcConfig is a set of configurations used during RPC calls.
type rpcConfig struct {
readOnlyMask int
rpcTimeout time.Duration
connectTimeout time.Duration
readWriteTimeout time.Duration
ioBufferSize int
transportProtocol transport.Protocol
interactionMode InteractionMode
payloadCodec serviceinfo.PayloadCodec
}
func init() {
rpcConfigPool.New = newRPCConfig
}
func newRPCConfig() interface{} {
c := &rpcConfig{}
c.initialize()
return c
}
// LockConfig sets the bits of readonly mask to prevent sequential modification on certain configs.
func (r *rpcConfig) LockConfig(bits int) {
r.readOnlyMask |= bits
}
// SetRPCTimeout implements MutableRPCConfig.
func (r *rpcConfig) SetRPCTimeout(to time.Duration) error {
if !r.IsRPCTimeoutLocked() {
r.rpcTimeout = to
return nil
}
return kerrors.ErrNotSupported
}
// IsRPCTimeoutLocked implements the MutableRPCConfig interface.
func (r *rpcConfig) IsRPCTimeoutLocked() bool {
return r.readOnlyMask&BitRPCTimeout != 0
}
// SetConnectTimeout implements MutableRPCConfig interface.
func (r *rpcConfig) SetConnectTimeout(to time.Duration) error {
if !r.IsConnectTimeoutLocked() {
r.connectTimeout = to
return nil
}
return kerrors.ErrNotSupported
}
// IsConnectTimeoutLocked implements the MutableRPCConfig interface.
func (r *rpcConfig) IsConnectTimeoutLocked() bool {
return r.readOnlyMask&BitConnectTimeout != 0
}
// SetReadWriteTimeout implements MutableRPCConfig interface.
func (r *rpcConfig) SetReadWriteTimeout(to time.Duration) error {
if !r.IsReadWriteTimeoutLocked() {
r.readWriteTimeout = to
return nil
}
return kerrors.ErrNotSupported
}
// IsReadWriteTimeoutLocked implements the MutableRPCConfig interface.
func (r *rpcConfig) IsReadWriteTimeoutLocked() bool {
return r.readOnlyMask&BitReadWriteTimeout != 0
}
// SetIOBufferSize implements MutableRPCConfig interface.
func (r *rpcConfig) SetIOBufferSize(sz int) error {
if (r.readOnlyMask & BitIOBufferSize) == 0 {
r.ioBufferSize = sz
return nil
}
return kerrors.ErrNotSupported
}
// ImmutableView implements MutableRPCConfig interface.
func (r *rpcConfig) ImmutableView() RPCConfig {
return r
}
// RPCTimeout implements RPCConfig interface.
func (r *rpcConfig) RPCTimeout() time.Duration {
return r.rpcTimeout
}
// ConnectTimeout implements RPCConfig interface.
func (r *rpcConfig) ConnectTimeout() time.Duration {
return r.connectTimeout
}
// ReadWriteTimeout implements RPCConfig interface.
func (r *rpcConfig) ReadWriteTimeout() time.Duration {
return r.readWriteTimeout
}
// IOBufferSize implements RPCConfig interface.
func (r *rpcConfig) IOBufferSize() int {
return r.ioBufferSize
}
// TransportProtocol implements RPCConfig interface. It is only useful for client.
func (r *rpcConfig) TransportProtocol() transport.Protocol {
return r.transportProtocol
}
// SetTransportProtocol implements MutableRPCConfig interface.
func (r *rpcConfig) SetTransportProtocol(tp transport.Protocol) error {
// PurePayload would override all the bits set before
// since in previous implementation, r.transport |= transport.PurePayload would not take effect
if tp == transport.PurePayload {
r.transportProtocol = tp
} else {
r.transportProtocol |= tp
}
return nil
}
func (r *rpcConfig) SetInteractionMode(mode InteractionMode) error {
r.interactionMode = mode
return nil
}
func (r *rpcConfig) InteractionMode() InteractionMode {
return r.interactionMode
}
func (r *rpcConfig) SetPayloadCodec(codec serviceinfo.PayloadCodec) {
r.payloadCodec = codec
}
func (r *rpcConfig) PayloadCodec() serviceinfo.PayloadCodec {
return r.payloadCodec
}
// Clone returns a copy of the current rpcConfig.
func (r *rpcConfig) Clone() MutableRPCConfig {
r2 := rpcConfigPool.Get().(*rpcConfig)
*r2 = *r
return r2
}
func (r *rpcConfig) CopyFrom(from RPCConfig) {
f := from.(*rpcConfig)
*r = *f
}
func (r *rpcConfig) initialize() {
r.readOnlyMask = 0
r.rpcTimeout = defaultRPCTimeout
r.connectTimeout = defaultConnectTimeout
r.readWriteTimeout = defaultReadWriteTimeout
r.ioBufferSize = defaultBufferSize
r.transportProtocol = 0
r.interactionMode = defaultInteractionMode
}
// Recycle reuses the rpcConfig.
func (r *rpcConfig) Recycle() {
r.initialize()
rpcConfigPool.Put(r)
}
// NewRPCConfig creates a default RPCConfig.
func NewRPCConfig() RPCConfig {
r := rpcConfigPool.Get().(*rpcConfig)
return r
}