-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconn_pool.go
More file actions
169 lines (135 loc) · 2.84 KB
/
Copy pathconn_pool.go
File metadata and controls
169 lines (135 loc) · 2.84 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package connpool
import (
"fmt"
"io"
"sync"
"time"
)
//TODO: 保存所有的连接, 而不是只保存连接计数
var ErrMaxConn = fmt.Errorf("maximum connections reached")
//
type NConn interface {
io.Closer
Name() string
Closed() bool
}
type ConnPool struct {
sync.RWMutex
Name string
Address string
MaxConns int
MaxIdle int
Cnt int64
Username string
Password string
Database string
Precision string
New func(name string) (NConn, error)
active int
free []NConn
all map[string]NConn
}
func NewConnPool(name string, address string, maxConns int, maxIdle int) *ConnPool {
return &ConnPool{Name: name, Address: address, MaxConns: maxConns, MaxIdle: maxIdle, Cnt: 0, all: make(map[string]NConn)}
}
func (this *ConnPool) Proc() string {
this.RLock()
defer this.RUnlock()
return fmt.Sprintf("Name:%s,Cnt:%d,active:%d,all:%d,free:%d",
this.Name, this.Cnt, this.active, len(this.all), len(this.free))
}
func (this *ConnPool) Fetch() (NConn, error) {
this.Lock()
defer this.Unlock()
// get from free
conn := this.fetchFree()
if conn != nil {
return conn, nil
}
if this.overMax() {
return nil, ErrMaxConn
}
// create new conn
conn, err := this.newConn()
if err != nil {
return nil, err
}
this.increActive()
return conn, nil
}
func (this *ConnPool) Release(conn NConn) {
this.Lock()
defer this.Unlock()
if this.overMaxIdle() {
this.deleteConn(conn)
this.decreActive()
} else {
this.addFree(conn)
}
}
func (this *ConnPool) ForceClose(conn NConn) {
this.Lock()
defer this.Unlock()
this.deleteConn(conn)
this.decreActive()
}
func (this *ConnPool) Destroy() {
this.Lock()
defer this.Unlock()
for _, conn := range this.free {
if conn != nil && !conn.Closed() {
conn.Close()
}
}
for _, conn := range this.all {
if conn != nil && !conn.Closed() {
conn.Close()
}
}
this.active = 0
this.free = []NConn{}
this.all = map[string]NConn{}
}
// internal, concurrently unsafe
func (this *ConnPool) newConn() (NConn, error) {
name := fmt.Sprintf("%s_%d_%d", this.Name, this.Cnt, time.Now().Unix())
conn, err := this.New(name)
if err != nil {
if conn != nil {
conn.Close()
}
return nil, err
}
this.Cnt++
this.all[conn.Name()] = conn
return conn, nil
}
func (this *ConnPool) deleteConn(conn NConn) {
if conn != nil {
conn.Close()
}
delete(this.all, conn.Name())
}
func (this *ConnPool) addFree(conn NConn) {
this.free = append(this.free, conn)
}
func (this *ConnPool) fetchFree() NConn {
if len(this.free) == 0 {
return nil
}
conn := this.free[0]
this.free = this.free[1:]
return conn
}
func (this *ConnPool) increActive() {
this.active += 1
}
func (this *ConnPool) decreActive() {
this.active -= 1
}
func (this *ConnPool) overMax() bool {
return this.active >= this.MaxConns
}
func (this *ConnPool) overMaxIdle() bool {
return len(this.free) >= this.MaxIdle
}