/
main.go
71 lines (57 loc) · 1.68 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
65
66
67
68
69
70
71
package main
import (
"flag"
"net"
"runtime"
"code.google.com/p/go.net/context"
"github.com/golang/glog"
)
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
laddr, _ := net.ResolveTCPAddr("tcp", ":"+port) // TODO(zog): flag
l, err := net.ListenTCP("tcp", laddr)
if err != nil {
glog.Fatalf("net.ListenTCP: %v", err)
}
ctx := context.Background()
glog.Infof("server start listen on: %s", laddr.String())
for {
tcpconn, err := l.AcceptTCP()
if err != nil {
glog.Errorf("Accept error, server stop (listen on:%s), err: %s",
laddr.String(), err)
break
}
go HandleConnection(ctx, tcpconn)
}
glog.Infof("server stop listen on: %s)", laddr.String())
}
func HandleConnection(ctx context.Context, tcpconn *net.TCPConn) {
ctx, cancel := context.WithCancel(ctx)
defer func() {
tcpconn.Close()
if err := recover(); err != nil {
glog.Errorf("tcpconn panic: %s", err)
}
cancel()
}()
sysfd, err := SysfdByTcpConn(tcpconn)
if err != nil {
glog.Errorf("SysfdByTcpConn err: %s, addr: %s, fd=%d",
err, tcpconn.RemoteAddr().String(), sysfd)
return
}
glog.Infof("Start handle conn, addr: %s, fd=%d", tcpconn.RemoteAddr().String(), sysfd)
errc := make(chan error)
inreqc := make(chan *Request, 10) // TODO(zog): is 10 a good number?
reqc := Recv(ctx, errc, sysfd, tcpconn)
rspc := Send(ctx, errc, sysfd, tcpconn)
Work(ctx, errc, sysfd, reqc, inreqc, rspc)
Update(ctx, errc, sysfd, rspc)
exportInnerReqChan(sysfd, tcpconn, inreqc)
if err := <-errc; err != nil {
glog.Errorf("HandleConnection err: %v, addr: %s", err, tcpconn.RemoteAddr().String())
}
glog.Infof("Stop handle conn, addr: %s, fd=%d", tcpconn.RemoteAddr().String(), sysfd)
}