-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastway.go
113 lines (95 loc) · 2.41 KB
/
fastway.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
package main
import (
"log"
"net"
"time"
"fmt"
fastway "github.com/funny/fastway/go"
"github.com/funny/reuseport"
"github.com/funny/slab"
snet "github.com/funny/snet/go"
)
func startFastway() (*fastway.Gateway, net.Addr) {
var pool slab.Pool
switch gConf.MemPoolType {
case "sync":
pool = slab.NewSyncPool(gConf.MemPoolMinChunk, gConf.MemPoolMaxChunk, gConf.MemPoolFactor)
case "atom":
pool = slab.NewAtomPool(gConf.MemPoolMinChunk, gConf.MemPoolMaxChunk, gConf.MemPoolFactor, gConf.MemPoolPageSize)
case "chan":
pool = slab.NewChanPool(gConf.MemPoolMinChunk, gConf.MemPoolMaxChunk, gConf.MemPoolFactor, gConf.MemPoolPageSize)
default:
println(`unsupported memory pool type, must be "sync", "atom" or "chan"`)
}
gw := fastway.NewGateway(pool, gConf.MaxPacket)
var serverAddr, clientAddr string
if gConf.GateModel == GateModelsClient {
serverAddr = fmt.Sprintf(":%d", gConf.Port)
clientAddr = fmt.Sprintf("%s:0", gConf.Addr)
} else {
clientAddr = fmt.Sprintf(":%d", gConf.Port)
serverAddr = fmt.Sprintf("%s:0", gConf.Addr)
}
clientL := listen("client", clientAddr, gConf.ReusePort,
false,
false,
0,
0,
0,
)
go gw.ServeClients(
clientL,
fastway.GatewayCfg{
MaxConn: gConf.ClientMaxConn,
BufferSize: gConf.ClientBufferSize,
SendChanSize: gConf.ClientSendChanSize,
IdleTimeout: 29 * time.Minute,
},
)
serverL := listen("server", serverAddr, gConf.ReusePort,
false,
false,
0,
0,
0,
)
go gw.ServeServers(
serverL,
fastway.GatewayCfg{
AuthKey: gConf.AuthKey,
BufferSize: 512 * 1024,
SendChanSize: 512,
IdleTimeout: 29 * time.Minute,
},
)
var l net.Listener
if gConf.GateModel == GateModelsClient {
l = clientL
} else {
l = serverL
}
return gw, l.Addr()
}
func listen(who, addr string, reuse, snetEnable, snetEncrypt bool, snetBuffer int, snetInitTimeout, snetWaitTimeout time.Duration) net.Listener {
var lsn net.Listener
var err error
if reuse {
lsn, err = reuseport.NewReusablePortListener("tcp", addr)
} else {
lsn, err = net.Listen("tcp", addr)
}
if err != nil {
log.Fatalf("setup %s listener at %s failed - %s", who, addr, err)
}
if snetEnable {
lsn, _ = snet.Listen(snet.Config{
EnableCrypt: snetEncrypt,
RewriterBufferSize: snetBuffer,
HandshakeTimeout: snetInitTimeout,
ReconnWaitTimeout: snetWaitTimeout,
}, func() (net.Listener, error) {
return lsn, nil
})
}
return lsn
}