-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
64 lines (49 loc) · 1.12 KB
/
main.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
package main
import (
"fmt"
"mysql-proxy/lib"
"net"
"runtime"
"strconv"
"sync"
)
func main() {
mysqlUrl := ":3306"
listen := ":3366"
connCount := runtime.NumCPU()
server, _ := net.Listen("tcp", listen)
connList := make([]lib.ProxyConn, connCount)
for i := 0; i < connCount; i++ {
proxy := lib.ProxyConn{ID: i}
connList[i] = proxy
}
wg := sync.WaitGroup{}
wg.Add(1)
for i := 0; i < connCount; i++ {
go func(proxy lib.ProxyConn) {
proxy.NewClientConn(server)
proxy.NewMysqlConn(mysqlUrl)
fmt.Println("Connection " + strconv.Itoa(proxy.ID) + " established")
err := proxy.Handshake()
if err != nil {
proxy.Close()
}
go proxy.PipeMysql2Client()
go proxy.PipeClient2Mysql()
for {
if !proxy.IsClientClose() {
continue
}
proxy.NewClientConn(server)
fmt.Println("Connection " + strconv.Itoa(proxy.ID) + " working")
err := proxy.FakeHandshake()
if err != nil {
proxy.CloseClient()
}
go proxy.PipeClient2Mysql()
}
}(connList[i])
}
fmt.Println("Start at " + listen + " , with max " + strconv.Itoa(connCount) + " connection")
wg.Wait()
}