/
client.go
413 lines (361 loc) · 9.41 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2020-2023 Zhang Jingcheng <diogin@gmail.com>.
// Copyright (c) 2022-2023 HexInfra Co., Ltd.
// All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE.md file.
// General client implementation.
package internal
import (
"crypto/tls"
"errors"
"net"
"sync"
"sync/atomic"
"time"
)
// client is the interface for outgates and backends.
type client interface {
// Imports
// Methods
Stage() *Stage
TLSMode() bool
WriteTimeout() time.Duration
ReadTimeout() time.Duration
AliveTimeout() time.Duration
nextConnID() int64
}
// qClient is the interface for QUICOutgate, QUDSOutgate, QUICBackend, and QUDSBackend.
type qClient interface {
// Imports
client
streamHolder
// Methods
}
// tClient is the interface for TCPSOutgate, TUDSOutgate, TCPSBackend, and TUDSBackend.
type tClient interface {
// Imports
client
streamHolder
// Methods
}
// uClient is the interface for UDPSOutgate, UUDSOutgate, UDPSBackend, and UUDSBackend.
type uClient interface {
// Imports
client
// Methods
}
// client_ is a mixin for outgates and backends.
type client_ struct {
// Mixins
Component_
// Assocs
stage *Stage // current stage
// States
tlsMode bool // use TLS?
tlsConfig *tls.Config // TLS config if TLS is enabled
dialTimeout time.Duration // dial remote timeout
writeTimeout time.Duration // write operation timeout
readTimeout time.Duration // read operation timeout
aliveTimeout time.Duration // conn alive timeout
connID atomic.Int64 // next conn id
}
func (c *client_) onCreate(name string, stage *Stage) {
c.MakeComp(name)
c.stage = stage
}
func (c *client_) onConfigure() {
// tlsMode
c.ConfigureBool("tlsMode", &c.tlsMode, false)
if c.tlsMode {
c.tlsConfig = new(tls.Config)
}
// dialTimeout
c.ConfigureDuration("dialTimeout", &c.dialTimeout, func(value time.Duration) error {
if value >= time.Second {
return nil
}
return errors.New(".dialTimeout has an invalid value")
}, 10*time.Second)
// writeTimeout
c.ConfigureDuration("writeTimeout", &c.writeTimeout, func(value time.Duration) error {
if value >= time.Second {
return nil
}
return errors.New(".writeTimeout has an invalid value")
}, 30*time.Second)
// readTimeout
c.ConfigureDuration("readTimeout", &c.readTimeout, func(value time.Duration) error {
if value >= time.Second {
return nil
}
return errors.New(".readTimeout has an invalid value")
}, 30*time.Second)
// aliveTimeout
c.ConfigureDuration("aliveTimeout", &c.aliveTimeout, func(value time.Duration) error {
if value > 0 {
return nil
}
return errors.New(".readTimeout has an invalid value")
}, 5*time.Second)
}
func (c *client_) onPrepare() {
// Currently nothing.
}
func (c *client_) OnShutdown() {
close(c.Shut)
}
func (c *client_) Stage() *Stage { return c.stage }
func (c *client_) TLSMode() bool { return c.tlsMode }
func (c *client_) WriteTimeout() time.Duration { return c.writeTimeout }
func (c *client_) ReadTimeout() time.Duration { return c.readTimeout }
func (c *client_) AliveTimeout() time.Duration { return c.aliveTimeout }
func (c *client_) nextConnID() int64 { return c.connID.Add(1) }
// outgate
type outgate interface {
// Methods
servedConns() int64
servedStreams() int64
}
// outgate_ is the mixin for outgates.
type outgate_ struct {
// Mixins
client_
// States
nServedStreams atomic.Int64
nServedExchans atomic.Int64
}
func (o *outgate_) onCreate(name string, stage *Stage) {
o.client_.onCreate(name, stage)
}
func (o *outgate_) onConfigure() {
o.client_.onConfigure()
}
func (o *outgate_) onPrepare() {
o.client_.onPrepare()
}
func (o *outgate_) servedStreams() int64 { return o.nServedStreams.Load() }
func (o *outgate_) incServedStreams() { o.nServedStreams.Add(1) }
func (o *outgate_) servedExchans() int64 { return o.nServedExchans.Load() }
func (o *outgate_) incServedExchans() { o.nServedExchans.Add(1) }
// Backend is a group of nodes.
type Backend interface {
// Imports
Component
client
// Methods
Maintain() // goroutine
}
// Backend_ is the mixin for backends.
type Backend_[N Node] struct {
// Mixins
client_
// Assocs
creator interface {
createNode(id int32) N
} // if Go's generic supports new(N) then this is not needed.
nodes []N // nodes of this backend
// States
}
func (b *Backend_[N]) onCreate(name string, stage *Stage, creator interface{ createNode(id int32) N }) {
b.client_.onCreate(name, stage)
b.creator = creator
}
func (b *Backend_[N]) onConfigure() {
b.client_.onConfigure()
// nodes
v, ok := b.Find("nodes")
if !ok {
UseExitln("nodes is required for backends")
}
vNodes, ok := v.List()
if !ok {
UseExitln("nodes must be a list")
}
for id, elem := range vNodes {
vNode, ok := elem.Dict()
if !ok {
UseExitln("node in nodes must be a dict")
}
node := b.creator.createNode(int32(id))
// address
vAddress, ok := vNode["address"]
if !ok {
UseExitln("address is required in node")
}
if address, ok := vAddress.String(); ok && address != "" {
node.setAddress(address)
}
// weight
vWeight, ok := vNode["weight"]
if !ok {
node.setWeight(1)
} else if weight, ok := vWeight.Int32(); ok && weight > 0 {
node.setWeight(weight)
} else {
UseExitln("bad weight in node")
}
// keepConns
vKeepConns, ok := vNode["keepConns"]
if !ok {
node.setKeepConns(10)
} else if keepConns, ok := vKeepConns.Int32(); ok && keepConns > 0 {
node.setKeepConns(keepConns)
} else {
UseExitln("bad keepConns in node")
}
b.nodes = append(b.nodes, node)
}
}
func (b *Backend_[N]) onPrepare() {
b.client_.onPrepare()
}
func (b *Backend_[N]) Maintain() { // goroutine
for _, node := range b.nodes {
b.IncSub(1)
go node.Maintain()
}
<-b.Shut
// Backend is told to shutdown. Tell its nodes to shutdown too
for _, node := range b.nodes {
node.shut()
}
b.WaitSubs() // nodes
if Debug() >= 2 {
Printf("backend=%s done\n", b.Name())
}
b.stage.SubDone()
}
// Node is a member of backend.
type Node interface {
// Methods
setAddress(address string)
setWeight(weight int32)
setKeepConns(keepConns int32)
Maintain() // goroutine
shut()
}
// Node_ is a mixin for backend nodes.
type Node_ struct {
// Mixins
subsWaiter_ // usually for conns
shutdownable_
// States
id int32 // the node id
address string // hostname:port, /path/to/unix.sock
weight int32 // 1, 22, 333, ...
keepConns int32 // max conns to keep alive
down atomic.Bool // TODO: false-sharing
freeList struct { // free list of conns in this node
sync.Mutex
head Conn // head element
tail Conn // tail element
qnty int // size of the list
}
}
func (n *Node_) init(id int32) {
n.shutdownable_.init()
n.id = id
}
func (n *Node_) setAddress(address string) { n.address = address }
func (n *Node_) setWeight(weight int32) { n.weight = weight }
func (n *Node_) setKeepConns(keepConns int32) { n.keepConns = keepConns }
func (n *Node_) markDown() { n.down.Store(true) }
func (n *Node_) markUp() { n.down.Store(false) }
func (n *Node_) isDown() bool { return n.down.Load() }
func (n *Node_) pullConn() Conn {
list := &n.freeList
list.Lock()
defer list.Unlock()
if list.qnty == 0 {
return nil
}
conn := list.head
list.head = conn.getNext()
conn.setNext(nil)
list.qnty--
return conn
}
func (n *Node_) pushConn(conn Conn) {
list := &n.freeList
list.Lock()
defer list.Unlock()
if list.qnty == 0 {
list.head = conn
list.tail = conn
} else { // >= 1
list.tail.setNext(conn)
list.tail = conn
}
list.qnty++
}
func (n *Node_) closeFree() int {
list := &n.freeList
list.Lock()
defer list.Unlock()
for conn := list.head; conn != nil; conn = conn.getNext() {
conn.closeConn()
}
qnty := list.qnty
list.qnty = 0
list.head, list.tail = nil, nil
return qnty
}
func (n *Node_) shut() {
close(n.Shut)
}
var errNodeDown = errors.New("node is down")
// Conn is the client conns.
type Conn interface {
// Methods
getNext() Conn
setNext(next Conn)
isAlive() bool
closeConn()
}
// Conn_ is the mixin for client conns.
type Conn_ struct {
// Conn states (non-zeros)
next Conn // the linked-list
id int64 // the conn id
client client // associated client
expire time.Time // when the conn is considered expired
// Conn states (zeros)
lastWrite time.Time // deadline of last write operation
lastRead time.Time // deadline of last read operation
}
func (c *Conn_) onGet(id int64, client client) {
c.id = id
c.client = client
c.expire = time.Now().Add(client.AliveTimeout())
}
func (c *Conn_) onPut() {
c.client = nil
c.expire = time.Time{}
c.lastWrite = time.Time{}
c.lastRead = time.Time{}
}
func (c *Conn_) getNext() Conn { return c.next }
func (c *Conn_) setNext(next Conn) { c.next = next }
func (c *Conn_) isAlive() bool { return time.Now().Before(c.expire) }
// wireBackend
type wireBackend interface {
WriteTimeout() time.Duration
ReadTimeout() time.Duration
Dial() (wireConn, error)
FetchConn() (wireConn, error)
StoreConn(conn wireConn)
}
// wireConn
type wireConn interface {
SetWriteDeadline(deadline time.Time) error
SetReadDeadline(deadline time.Time) error
Write(p []byte) (n int, err error)
Writev(vector *net.Buffers) (int64, error)
Read(p []byte) (n int, err error)
ReadFull(p []byte) (n int, err error)
ReadAtLeast(p []byte, min int) (n int, err error)
CloseWrite() error
Close() error
MakeTempName(p []byte, unixTime int64) (from int, edge int)
IsBroken() bool
MarkBroken()
}