/
proxy.go
68 lines (56 loc) · 1.37 KB
/
proxy.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
package archer
import (
"fmt"
"net"
"runtime"
"strings"
log "github.com/ngaut/logging"
)
type Proxy struct {
l net.Listener // 监听 Listener
filter Filter // Redis 有效协议检测过滤器
pc *ProxyConfig // 全局配置文件
sm *SessMana // Session 管理
cluster *Cluster // 集群实现
}
func NewProxy(pc *ProxyConfig) *Proxy {
p := &Proxy{
sm: newSessMana(pc.idleTimeout),
cluster: NewCluster(pc),
filter: &StrFilter{},
pc: pc,
}
// listen 放到最后
l, err := net.Listen("tcp4", fmt.Sprintf(":%d", pc.port))
if err != nil {
log.Fatalf("Proxy Listen %d failed %s", pc.port, err.Error())
}
p.l = l
return p
}
func (p *Proxy) Start() {
for {
c, err := p.l.Accept()
if err != nil {
log.Warning("got error when Accept network connect ", err)
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
log.Warningf("NOTICE: temporary Accept() failure - %s", err)
runtime.Gosched()
continue
}
// theres no direct way to detect this error because it is not exposed
if !strings.Contains(err.Error(), "use of closed network connection") {
log.Warningf("ERROR: listener.Accept() - %s", err)
}
break
}
go HandleConn(p, c)
}
}
func HandleConn(p *Proxy, c net.Conn) {
s := NewSession(p, c)
p.sm.Put(c.RemoteAddr().String(), s)
s.Serve()
log.Warning("Close client ", c.RemoteAddr().String())
c.Close()
}