-
Notifications
You must be signed in to change notification settings - Fork 37
/
mux.go
514 lines (481 loc) · 12 KB
/
mux.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package nps_mux
import (
"errors"
"io"
"log"
"math"
"net"
"os"
"sync/atomic"
"time"
)
const (
muxPingFlag uint8 = iota
muxNewConnOk
muxNewConnFail
muxNewMsg
muxNewMsgPart
muxMsgSendOk
muxNewConn
muxConnClose
muxPingReturn
muxPing int32 = -1
maximumSegmentSize = poolSizeWindow
maximumWindowSize = 1 << 27 // 1<<31-1 TCP slide window size is very large,
// we use 128M, reduce memory usage
)
type Mux struct {
latency uint64 // we store latency in bits, but it's float64
net.Listener
conn net.Conn
connMap *connMap
newConnCh chan *conn
id int32
closeChan chan struct{}
IsClose bool
counter *latencyCounter
bw *bandwidth
pingCh chan []byte
pingCheckTime uint32 // we check the ping per 5s
pingCheckThreshold uint32
connType string
writeQueue priorityQueue
newConnQueue connQueue
}
func NewMux(c net.Conn, connType string, pingCheckThreshold int) *Mux {
//c.(*net.TCPConn).SetReadBuffer(0)
//c.(*net.TCPConn).SetWriteBuffer(0)
fd, err := getConnFd(c)
if err != nil {
log.Println(err)
}
var checkThreshold uint32
if pingCheckThreshold <= 0 {
if connType == "kcp" {
checkThreshold = 20
} else {
checkThreshold = 60
}
} else {
checkThreshold = uint32(pingCheckThreshold)
}
m := &Mux{
conn: c,
connMap: NewConnMap(),
id: 0,
closeChan: make(chan struct{}, 1),
newConnCh: make(chan *conn),
bw: NewBandwidth(fd),
IsClose: false,
connType: connType,
pingCh: make(chan []byte),
pingCheckThreshold: checkThreshold,
counter: newLatencyCounter(),
}
m.writeQueue.New()
m.newConnQueue.New()
//read session by flag
m.readSession()
//ping
m.ping()
m.writeSession()
return m
}
func (s *Mux) NewConn() (*conn, error) {
if s.IsClose {
return nil, errors.New("the mux has closed")
}
conn := NewConn(s.getId(), s)
//it must be Set before send
s.connMap.Set(conn.connId, conn)
s.sendInfo(muxNewConn, conn.connId, nil)
//Set a timer timeout 120 second
timer := time.NewTimer(time.Minute * 2)
defer timer.Stop()
select {
case <-conn.connStatusOkCh:
return conn, nil
case <-timer.C:
}
return nil, errors.New("create connection fail,the server refused the connection")
}
func (s *Mux) Accept() (net.Conn, error) {
if s.IsClose {
return nil, errors.New("accpet error,the mux has closed")
}
conn := <-s.newConnCh
if conn == nil {
return nil, errors.New("accpet error,the conn has closed")
}
return conn, nil
}
func (s *Mux) Addr() net.Addr {
return s.conn.LocalAddr()
}
func (s *Mux) sendInfo(flag uint8, id int32, data interface{}) {
if s.IsClose {
return
}
var err error
pack := muxPack.Get()
err = pack.Set(flag, id, data)
if err != nil {
muxPack.Put(pack)
log.Println("mux: New Pack err", err)
_ = s.Close()
return
}
s.writeQueue.Push(pack)
return
}
func (s *Mux) writeSession() {
go func() {
for {
if s.IsClose {
break
}
pack := s.writeQueue.Pop()
if s.IsClose {
break
}
//if pack.flag == muxNewMsg || pack.flag == muxNewMsgPart {
// if pack.length >= 100 {
// log.Println("write session id", pack.id, "\n", string(pack.content[:100]))
// } else {
// log.Println("write session id", pack.id, "\n", string(pack.content[:pack.length]))
// }
//}
err := pack.Pack(s.conn)
muxPack.Put(pack)
if err != nil {
log.Println("mux: Pack err", err)
_ = s.Close()
break
}
}
}()
}
func (s *Mux) ping() {
go func() {
now, _ := time.Now().UTC().MarshalText()
s.sendInfo(muxPingFlag, muxPing, now)
// send the ping flag and Get the latency first
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
for {
if s.IsClose {
break
}
select {
case <-ticker.C:
}
if atomic.LoadUint32(&s.pingCheckTime) > s.pingCheckThreshold {
log.Println("mux: ping time out, checktime", s.pingCheckTime, "threshold", s.pingCheckThreshold)
_ = s.Close()
// more than limit times not receive the ping return package,
// mux conn is damaged, maybe a packet drop, close it
break
}
now, _ = time.Now().UTC().MarshalText()
s.sendInfo(muxPingFlag, muxPing, now)
atomic.AddUint32(&s.pingCheckTime, 1)
}
return
}()
go func() {
var now time.Time
var data []byte
for {
if s.IsClose {
break
}
select {
case data = <-s.pingCh:
atomic.StoreUint32(&s.pingCheckTime, 0)
case <-s.closeChan:
break
}
_ = now.UnmarshalText(data)
latency := time.Now().UTC().Sub(now).Seconds()
if latency > 0 {
atomic.StoreUint64(&s.latency, math.Float64bits(s.counter.Latency(latency)))
// convert float64 to bits, store it atomic
//log.Println("ping", math.Float64frombits(atomic.LoadUint64(&s.latency)))
}
if cap(data) > 0 && !s.IsClose {
windowBuff.Put(data)
}
}
}()
}
func (s *Mux) readSession() {
go func() {
var connection *conn
for {
if s.IsClose {
break
}
connection = s.newConnQueue.Pop()
if s.IsClose {
break // make sure that is closed
}
s.connMap.Set(connection.connId, connection) //it has been Set before send ok
s.newConnCh <- connection
s.sendInfo(muxNewConnOk, connection.connId, nil)
}
}()
go func() {
var pack *muxPackager
var l uint16
var err error
for {
if s.IsClose {
return
}
pack = muxPack.Get()
s.bw.StartRead()
if l, err = pack.UnPack(s.conn); err != nil {
log.Println("mux: read session unpack from connection err", err)
_ = s.Close()
break
}
s.bw.SetCopySize(l)
//if pack.flag == muxNewMsg || pack.flag == muxNewMsgPart {
// if pack.length >= 100 {
// log.Printf("read session id %d pointer %p\n%v", pack.id, pack.content, string(pack.content[:100]))
// } else {
// log.Printf("read session id %d pointer %p\n%v", pack.id, pack.content, string(pack.content[:pack.length]))
// }
//}
switch pack.flag {
case muxNewConn: //New connection
connection := NewConn(pack.id, s)
s.newConnQueue.Push(connection)
continue
case muxPingFlag: //ping
s.sendInfo(muxPingReturn, muxPing, pack.content)
windowBuff.Put(pack.content)
continue
case muxPingReturn:
s.pingCh <- pack.content
continue
}
if connection, ok := s.connMap.Get(pack.id); ok && !connection.isClose {
switch pack.flag {
case muxNewMsg, muxNewMsgPart: //New msg from remote connection
err = s.newMsg(connection, pack)
if err != nil {
log.Println("mux: read session connection New msg err", err)
_ = connection.Close()
}
continue
case muxNewConnOk: //connection ok
connection.connStatusOkCh <- struct{}{}
continue
case muxNewConnFail:
connection.connStatusFailCh <- struct{}{}
continue
case muxMsgSendOk:
if connection.isClose {
continue
}
connection.sendWindow.SetSize(pack.window)
continue
case muxConnClose: //close the connection
connection.closingFlag = true
connection.receiveWindow.Stop() // close signal to receive window
continue
}
} else if pack.flag == muxConnClose {
continue
}
muxPack.Put(pack)
}
}()
}
func (s *Mux) newMsg(connection *conn, pack *muxPackager) (err error) {
if connection.isClose {
err = io.ErrClosedPipe
return
}
//insert into queue
if pack.flag == muxNewMsgPart {
err = connection.receiveWindow.Write(pack.content, pack.length, true, pack.id)
}
if pack.flag == muxNewMsg {
err = connection.receiveWindow.Write(pack.content, pack.length, false, pack.id)
}
return
}
func (s *Mux) Close() (err error) {
if s.IsClose {
return errors.New("the mux has closed")
}
s.IsClose = true
log.Println("close mux")
s.connMap.Close()
//s.connMap = nil
s.closeChan <- struct{}{}
close(s.newConnCh)
// while target host close socket without finish steps, conn.Close method maybe blocked
// and tcp status change to CLOSE WAIT or TIME WAIT, so we close it in other goroutine
_ = s.conn.SetDeadline(time.Now().Add(time.Second * 5))
go s.conn.Close()
s.release()
return
}
func (s *Mux) release() {
for {
pack := s.writeQueue.TryPop()
if pack == nil {
break
}
if pack.basePackager.content != nil {
windowBuff.Put(pack.basePackager.content)
}
muxPack.Put(pack)
}
for {
connection := s.newConnQueue.TryPop()
if connection == nil {
break
}
connection = nil
}
s.writeQueue.Stop()
s.newConnQueue.Stop()
}
//Get New connId as unique flag
func (s *Mux) getId() (id int32) {
//Avoid going beyond the scope
if (math.MaxInt32 - s.id) < 10000 {
atomic.StoreInt32(&s.id, 0)
}
id = atomic.AddInt32(&s.id, 1)
if _, ok := s.connMap.Get(id); ok {
return s.getId()
}
return
}
type bandwidth struct {
readBandwidth uint64 // store in bits, but it's float64
readStart time.Time
lastReadStart time.Time
bufLength uint32
fd *os.File
calcThreshold uint32
}
func NewBandwidth(fd *os.File) *bandwidth {
return &bandwidth{fd: fd}
}
func (Self *bandwidth) StartRead() {
if Self.readStart.IsZero() {
Self.readStart = time.Now()
}
if Self.bufLength >= Self.calcThreshold {
Self.lastReadStart, Self.readStart = Self.readStart, time.Now()
Self.calcBandWidth()
}
}
func (Self *bandwidth) SetCopySize(n uint16) {
Self.bufLength += uint32(n)
}
func (Self *bandwidth) calcBandWidth() {
t := Self.readStart.Sub(Self.lastReadStart)
bufferSize, err := sysGetSock(Self.fd)
if err != nil {
log.Println(err)
Self.bufLength = 0
return
}
if Self.bufLength >= uint32(bufferSize) {
atomic.StoreUint64(&Self.readBandwidth, math.Float64bits(float64(Self.bufLength)/t.Seconds()))
// calculate the whole socket buffer, the time meaning to fill the buffer
} else {
Self.calcThreshold = uint32(bufferSize)
}
// socket buffer size is bigger than bufLength, so we don't calculate it
Self.bufLength = 0
}
func (Self *bandwidth) Get() (bw float64) {
// The zero value, 0 for numeric types
bw = math.Float64frombits(atomic.LoadUint64(&Self.readBandwidth))
if bw <= 0 {
bw = 0
}
return
}
const counterBits = 4
const counterMask = 1<<counterBits - 1
func newLatencyCounter() *latencyCounter {
return &latencyCounter{
buf: make([]float64, 1<<counterBits, 1<<counterBits),
headMin: 0,
}
}
type latencyCounter struct {
buf []float64 //buf is a fixed length ring buffer,
// if buffer is full, New value will replace the oldest one.
headMin uint8 //head indicate the head in ring buffer,
// in meaning, slot in list will be replaced;
// min indicate this slot value is minimal in list.
// we delineate the effective range with three times the minimum latency
// average of effective latency for all current data as a mux latency
}
func (Self *latencyCounter) unpack(idxs uint8) (head, min uint8) {
head = (idxs >> counterBits) & counterMask
// we Set head is 4 bits
min = idxs & counterMask
return
}
func (Self *latencyCounter) pack(head, min uint8) uint8 {
return head<<counterBits |
min&counterMask
}
func (Self *latencyCounter) add(value float64) {
head, min := Self.unpack(Self.headMin)
Self.buf[head] = value
if head == min {
min = Self.minimal()
//if head equals min, means the min slot already be replaced,
// so we need to find another minimal value in the list,
// and change the min indicator
}
if Self.buf[min] > value {
min = head
}
head++
Self.headMin = Self.pack(head, min)
}
func (Self *latencyCounter) minimal() (min uint8) {
var val float64
var i uint8
for i = 0; i < counterMask; i++ {
if Self.buf[i] > 0 {
if val > Self.buf[i] {
val = Self.buf[i]
min = i
}
}
}
return
}
func (Self *latencyCounter) Latency(value float64) (latency float64) {
Self.add(value)
latency = Self.countSuccess()
return
}
const lossRatio = 3
func (Self *latencyCounter) countSuccess() (successRate float64) {
var i, success uint8
_, min := Self.unpack(Self.headMin)
for i = 0; i < counterMask; i++ {
if Self.buf[i] <= lossRatio*Self.buf[min] && Self.buf[i] > 0 {
success++
successRate += Self.buf[i]
}
}
// counting all the data in the ring buf, except zero
successRate = successRate / float64(success)
return
}