forked from tedcy/fdfs_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_pool.go
143 lines (132 loc) · 2.61 KB
/
conn_pool.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
package fdfs_client
import (
"container/list"
"fmt"
"net"
"sync"
"time"
)
const (
MAXCONNS_LEAST = 5
)
type pConn struct {
net.Conn
pool *connPool
}
func (c pConn) Close() error {
return c.pool.put(c)
}
type connPool struct {
conns *list.List
addr string
maxConns int
count int
lock *sync.RWMutex
finish chan bool
connectTimeOut int
}
func newConnPool(addr string, maxConns, connectTimeOut int) (*connPool, error) {
if maxConns < MAXCONNS_LEAST {
return nil, fmt.Errorf("too little maxConns < %d", MAXCONNS_LEAST)
}
connPool := &connPool{
conns: list.New(),
addr: addr,
maxConns: maxConns,
lock: &sync.RWMutex{},
finish: make(chan bool),
connectTimeOut: connectTimeOut,
}
go func() {
timer := time.NewTimer(time.Second * 20)
for {
select {
case finish := <-connPool.finish:
if finish {
return
}
case <-timer.C:
connPool.CheckConns()
timer.Reset(time.Second * 20)
}
}
}()
connPool.lock.Lock()
defer connPool.lock.Unlock()
for i := 0; i < MAXCONNS_LEAST; i++ {
if err := connPool.makeConn(); err != nil {
return nil, err
}
}
return connPool, nil
}
func (this *connPool) Destory() {
if this == nil {
return
}
this.finish <- true
}
func (this *connPool) CheckConns() error {
this.lock.Lock()
defer this.lock.Unlock()
for e, next := this.conns.Front(), new(list.Element); e != nil; e = next {
next = e.Next()
conn := e.Value.(pConn)
header := &header{
cmd: FDFS_PROTO_CMD_ACTIVE_TEST,
}
if err := header.SendHeader(conn.Conn); err != nil {
this.conns.Remove(e)
this.count--
continue
}
if err := header.RecvHeader(conn.Conn); err != nil {
this.conns.Remove(e)
this.count--
continue
}
if header.cmd != TRACKER_PROTO_CMD_RESP || header.status != 0 {
this.conns.Remove(e)
this.count--
continue
}
}
return nil
}
func (this *connPool) makeConn() error {
conn, err := net.DialTimeout("tcp", this.addr, time.Second*time.Duration(this.connectTimeOut))
if err != nil {
return err
}
this.conns.PushBack(pConn{
Conn: conn,
pool: this,
})
this.count++
return nil
}
func (this *connPool) get() (net.Conn, error) {
this.lock.Lock()
defer this.lock.Unlock()
for {
e := this.conns.Front()
if e == nil {
if this.count >= this.maxConns {
return nil, fmt.Errorf("reach maxConns %d", this.maxConns)
}
this.makeConn()
continue
}
this.conns.Remove(e)
conn := e.Value.(pConn)
return conn, nil
}
//not reach
return nil, nil
}
func (this *connPool) put(pConn pConn) error {
this.lock.Lock()
defer this.lock.Unlock()
pConn.pool.conns.PushBack(pConn)
return nil
}