/
proxy.go
347 lines (287 loc) · 9.58 KB
/
proxy.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 proxy
import (
"container/list"
"context"
"runtime"
"sync"
jsoniter "github.com/json-iterator/go"
"mosn.io/api"
v2 "mosn.io/mosn/pkg/config/v2"
"mosn.io/mosn/pkg/configmanager"
"mosn.io/mosn/pkg/log"
"mosn.io/mosn/pkg/mtls"
"mosn.io/mosn/pkg/protocol"
"mosn.io/mosn/pkg/router"
"mosn.io/mosn/pkg/stream"
"mosn.io/mosn/pkg/streamfilter"
mosnsync "mosn.io/mosn/pkg/sync"
"mosn.io/mosn/pkg/types"
"mosn.io/mosn/pkg/upstream/cluster"
"mosn.io/pkg/buffer"
"mosn.io/pkg/variable"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var (
globalStats *Stats
currProxyID uint32
pool mosnsync.WorkerPool
)
func init() {
// register init function with interest of P number
configmanager.RegisterConfigParsedListener(configmanager.ParseCallbackKeyProcessor, initWorkerPool)
}
func initWorkerPool(data interface{}, endParsing bool) error {
initGlobalStats()
poolSize := runtime.NumCPU() * 256
// set poolSize equal to processor if it was specified
if pNum, ok := data.(int); ok && pNum > 0 {
poolSize = pNum * 256
}
pool = mosnsync.NewWorkerPool(poolSize)
return nil
}
func GetWorkerPool() mosnsync.WorkerPool {
return pool
}
func initGlobalStats() {
globalStats = newProxyStats(types.GlobalProxyName)
}
// types.ReadFilter
// types.ServerStreamConnectionEventListener
type proxy struct {
config *v2.Proxy
clusterManager types.ClusterManager
readCallbacks api.ReadFilterCallbacks
downstreamListener api.ConnectionEventListener
routersWrapper types.RouterWrapper // wrapper used to point to the routers instance
fallback bool
serverStreamConn types.ServerStreamConnection
context context.Context
activeStreams *list.List // downstream requests
asMux sync.RWMutex
stats *Stats
listenerStats *Stats
accessLogs []api.AccessLog
streamFilterFactory streamfilter.StreamFilterFactory
routeHandlerFactory router.MakeHandlerFunc
protocols []api.ProtocolName
// configure the proxy level worker pool
// eg. if we want the requests on one connection to keep serial,
// we only start one worker(goroutine) on this connection
// request will blocking wait for the previous to finish sending
workerpool mosnsync.WorkerPool
}
// NewProxy create proxy instance for given v2.Proxy config
func NewProxy(ctx context.Context, config *v2.Proxy) Proxy {
aclog, _ := variable.Get(ctx, types.VariableAccessLogs)
proxy := &proxy{
config: config,
clusterManager: cluster.GetClusterMngAdapterInstance().ClusterManager,
activeStreams: list.New(),
stats: globalStats,
context: ctx,
accessLogs: aclog.([]api.AccessLog),
}
if pi, err := variable.Get(ctx, types.VarProtocolConfig); err == nil {
if protos, ok := pi.([]api.ProtocolName); ok {
proxy.protocols = protos
}
}
// proxy level worker pool config
if config.ConcurrencyNum > 0 {
proxy.workerpool = mosnsync.NewWorkerPool(config.ConcurrencyNum)
}
// proxy level worker pool config end
lv, _ := variable.Get(ctx, types.VariableListenerName)
listenerName := lv.(string)
proxy.listenerStats = newListenerStats(listenerName)
if routersWrapper := router.GetRoutersMangerInstance().GetRouterWrapperByName(proxy.config.RouterConfigName); routersWrapper != nil {
proxy.routersWrapper = routersWrapper
} else {
log.DefaultLogger.Alertf("proxy.config", "[proxy] RouterConfigName:%s doesn't exit", proxy.config.RouterConfigName)
}
proxy.downstreamListener = &downstreamCallbacks{
proxy: proxy,
}
proxy.streamFilterFactory = streamfilter.GetStreamFilterManager().GetStreamFilterFactory(listenerName)
proxy.routeHandlerFactory = router.GetMakeHandlerFunc(proxy.config.RouterHandlerName)
return proxy
}
func (p *proxy) OnData(buf buffer.IoBuffer) api.FilterStatus {
if p.fallback {
return api.Continue
}
if p.serverStreamConn == nil {
var prot string
if conn, ok := p.readCallbacks.Connection().RawConn().(*mtls.TLSConn); ok {
prot = conn.ConnectionState().NegotiatedProtocol
}
scopes := p.protocols
// if protocols is Auto only, match all registered protocols
if len(scopes) == 1 && scopes[0] == protocol.Auto {
scopes = nil
}
proto, err := stream.SelectStreamFactoryProtocol(p.context, prot, buf.Bytes(), scopes)
if err == stream.EAGAIN {
return api.Stop
}
if err == stream.FAILED {
if p.config.FallbackForUnknownProtocol {
p.fallback = true
return api.Continue
}
var size int
if buf.Len() > 10 {
size = 10
} else {
size = buf.Len()
}
log.DefaultLogger.Alertf("proxy.auto", "[proxy] Protocol Auto error magic :%v", buf.Bytes()[:size])
p.readCallbacks.Connection().Close(api.NoFlush, api.OnReadErrClose)
return api.Stop
}
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("[proxy] Protoctol Auto: %v", proto)
}
p.serverStreamConn = stream.CreateServerStreamConnection(p.context, proto, p.readCallbacks.Connection(), p)
}
p.serverStreamConn.Dispatch(buf)
return api.Stop
}
// rpc realize upstream on event
func (p *proxy) onDownstreamEvent(event api.ConnectionEvent) {
if event.IsClose() {
p.stats.DownstreamConnectionDestroy.Inc(1)
p.stats.DownstreamConnectionActive.Dec(1)
p.listenerStats.DownstreamConnectionDestroy.Inc(1)
p.listenerStats.DownstreamConnectionActive.Dec(1)
var urEleNext *list.Element
p.asMux.RLock()
defer p.asMux.RUnlock()
for urEle := p.activeStreams.Front(); urEle != nil; urEle = urEleNext {
urEleNext = urEle.Next()
ds := urEle.Value.(*downStream)
// don't need to resetStream when upstream already response
// so that the stream can be reused.
if ds.upstreamProcessDone.Load() {
continue
}
ds.OnResetStream(types.StreamConnectionTermination)
}
return
}
if event == api.OnReadTimeout {
if p.shouldFallback() {
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("[proxy] wait for fallback timeout, do fallback")
}
p.fallback = true
p.readCallbacks.ContinueReading()
}
return
}
if event == api.OnShutdown && p.serverStreamConn != nil {
p.serverStreamConn.GoAway()
return
}
}
func (p *proxy) shouldFallback() bool {
if !p.config.FallbackForUnknownProtocol || p.serverStreamConn != nil {
return false
}
if p.fallback == true {
return false
}
// not yet receive any data, and then timeout happens, should keep waiting for data and not fallback
if p.readCallbacks.Connection().GetReadBuffer().Len() == 0 {
return false
}
return true
}
func (p *proxy) ReadDisableUpstream(disable bool) {
// TODO
}
func (p *proxy) ReadDisableDownstream(disable bool) {
// TODO
}
func (p *proxy) ActiveStreamSize() int {
if p.activeStreams == nil {
return 0
}
return p.activeStreams.Len()
}
func (p *proxy) InitializeReadFilterCallbacks(cb api.ReadFilterCallbacks) {
p.readCallbacks = cb
// bytes total adds all connections data together
cb.Connection().SetCollector(p.stats.DownstreamBytesReadTotal, p.stats.DownstreamBytesWriteTotal)
p.stats.DownstreamConnectionTotal.Inc(1)
p.stats.DownstreamConnectionActive.Inc(1)
p.listenerStats.DownstreamConnectionTotal.Inc(1)
p.listenerStats.DownstreamConnectionActive.Inc(1)
p.readCallbacks.Connection().AddConnectionEventListener(p.downstreamListener)
if len(p.protocols) == 1 && p.protocols[0] != protocol.Auto {
p.serverStreamConn = stream.CreateServerStreamConnection(p.context, api.ProtocolName(p.protocols[0]), p.readCallbacks.Connection(), p)
}
}
func (p *proxy) OnGoAway() {}
func (p *proxy) NewStreamDetect(ctx context.Context, responseSender types.StreamSender, span api.Span) types.StreamReceiveListener {
stream := newActiveStream(ctx, p, responseSender, span)
if p.streamFilterFactory != nil {
p.streamFilterFactory.CreateFilterChain(ctx, stream.getStreamFilterChainRegisterCallback())
}
p.asMux.Lock()
stream.element = p.activeStreams.PushBack(stream)
p.asMux.Unlock()
return stream
}
func (p *proxy) OnNewConnection() api.FilterStatus {
return api.Continue
}
func (p *proxy) streamResetReasonToResponseFlag(reason types.StreamResetReason) api.ResponseFlag {
switch reason {
case types.StreamConnectionFailed:
return api.UpstreamConnectionFailure
case types.StreamConnectionTermination:
return api.UpstreamConnectionTermination
case types.StreamLocalReset:
return api.UpstreamLocalReset
case types.StreamOverflow:
return api.UpstreamOverflow
case types.StreamRemoteReset:
return api.UpstreamRemoteReset
case types.UpstreamGlobalTimeout, types.UpstreamPerTryTimeout:
return api.UpstreamRequestTimeout
}
return 0
}
func (p *proxy) deleteActiveStream(s *downStream) {
if s.element != nil {
p.asMux.Lock()
p.activeStreams.Remove(s.element)
p.asMux.Unlock()
s.element = nil
}
}
// ConnectionEventListener
type downstreamCallbacks struct {
proxy *proxy
}
func (dc *downstreamCallbacks) OnEvent(event api.ConnectionEvent) {
dc.proxy.onDownstreamEvent(event)
}