-
Notifications
You must be signed in to change notification settings - Fork 795
/
client.go
177 lines (144 loc) · 5.2 KB
/
client.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
/*
* 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 stream
import (
"context"
metrics "github.com/rcrowley/go-metrics"
"mosn.io/mosn/pkg/log"
"mosn.io/mosn/pkg/types"
)
// stream.Client
// types.ReadFilter
// types.StreamConnectionEventListener
type client struct {
Protocol types.Protocol
Connection types.ClientConnection
Host types.HostInfo
ClientStreamConnection types.ClientStreamConnection
StreamConnectionEventListener types.StreamConnectionEventListener
ConnectedFlag bool
}
// NewStreamClient
// Create a codecclient used as a client to send/receive stream in a connection
func NewStreamClient(ctx context.Context, prot types.Protocol, connection types.ClientConnection, host types.HostInfo) Client {
client := &client{
Protocol: prot,
Connection: connection,
Host: host,
}
if factory, ok := streamFactories[prot]; ok {
client.ClientStreamConnection = factory.CreateClientStream(ctx, connection, client, client)
} else {
return nil
}
connection.AddConnectionEventListener(client)
connection.FilterManager().AddReadFilter(client)
connection.SetNoDelay(true)
return client
}
// NewBiDirectStreamClient
// Create a bidirectional client used to realize bidirectional communication
func NewBiDirectStreamClient(ctx context.Context, prot types.Protocol, connection types.ClientConnection, host types.HostInfo,
serverCallbacks types.ServerStreamConnectionEventListener) Client {
client := &client{
Protocol: prot,
Connection: connection,
Host: host,
}
if factory, ok := streamFactories[prot]; ok {
client.ClientStreamConnection = factory.CreateBiDirectStream(ctx, connection, client, serverCallbacks)
} else {
return nil
}
connection.AddConnectionEventListener(client)
connection.FilterManager().AddReadFilter(client)
connection.SetNoDelay(true)
return client
}
// Client
func (c *client) ConnID() uint64 {
return c.Connection.ID()
}
func (c *client) Connect() error {
return c.Connection.Connect()
}
func (c *client) AddConnectionEventListener(listener types.ConnectionEventListener) {
c.Connection.AddConnectionEventListener(listener)
}
func (c *client) ActiveRequestsNum() int {
return c.ClientStreamConnection.ActiveStreamsNum()
}
func (c *client) SetConnectionCollector(read, write metrics.Counter) {
c.Connection.SetCollector(read, write)
}
func (c *client) SetStreamConnectionEventListener(listener types.StreamConnectionEventListener) {
c.StreamConnectionEventListener = listener
}
func (c *client) NewStream(context context.Context, respReceiver types.StreamReceiveListener) types.StreamSender {
// oneway
if respReceiver == nil {
log.DefaultLogger.Debugf("oneway client NewStream")
return c.ClientStreamConnection.NewStream(context, nil)
}
wrapper := &clientStreamReceiverWrapper{
streamReceiver: respReceiver,
}
streamSender := c.ClientStreamConnection.NewStream(context, wrapper)
wrapper.stream = streamSender.GetStream()
return streamSender
}
func (c *client) Close() {
c.Connection.Close(types.NoFlush, types.LocalClose)
}
// types.StreamConnectionEventListener
func (c *client) OnGoAway() {
c.StreamConnectionEventListener.OnGoAway()
}
// types.ConnectionEventListener
// conn callbacks
func (c *client) OnEvent(event types.ConnectionEvent) {
log.DefaultLogger.Debugf("client OnEvent %v, connected %v", event, c.ConnectedFlag)
switch event {
case types.Connected:
c.ConnectedFlag = true
}
if reason, ok := c.ClientStreamConnection.CheckReasonError(c.ConnectedFlag, event); !ok {
c.ClientStreamConnection.Reset(reason)
}
}
// types.ReadFilter
// read filter, recv upstream data
func (c *client) OnData(buffer types.IoBuffer) types.FilterStatus {
c.ClientStreamConnection.Dispatch(buffer)
return types.Stop
}
func (c *client) OnNewConnection() types.FilterStatus {
return types.Continue
}
func (c *client) InitializeReadFilterCallbacks(cb types.ReadFilterCallbacks) {}
// uniform wrapper to destroy stream at client side
type clientStreamReceiverWrapper struct {
stream types.Stream
streamReceiver types.StreamReceiveListener
}
func (w *clientStreamReceiverWrapper) OnReceive(ctx context.Context, headers types.HeaderMap, data types.IoBuffer, trailers types.HeaderMap) {
w.stream.DestroyStream()
w.streamReceiver.OnReceive(ctx, headers, data, trailers)
}
func (w *clientStreamReceiverWrapper) OnDecodeError(ctx context.Context, err error, headers types.HeaderMap) {
w.streamReceiver.OnDecodeError(ctx, err, headers)
}