-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn_pool.go
More file actions
205 lines (180 loc) · 4.4 KB
/
conn_pool.go
File metadata and controls
205 lines (180 loc) · 4.4 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package pool
import (
"bytes"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
)
type IConnection interface {
Close()
HostAddr() string
}
////////////////////////////////////
const (
retryDuration = time.Second * 10
healthReportTick = time.Second * 30
)
const (
StateConnected = 1
StateConnecting = 2
StateDisconnected = 3
StateDimission = 4
)
var svrDownError = errors.New("backend servers are all down")
var svrDimissionError = errors.New("backend server is dimission")
var healthStateDef map[int]string = map[int]string{StateConnected: "Connected",
StateConnecting: "Connecting",
StateDisconnected: "Disconnected",
StateDimission: "Dimission"}
type Connectionfactory func(host string) (IConnection, error)
type Health struct {
State int
Idle int32
Total int32
}
type ConnectionEntry struct {
health *Health
connList chan IConnection
}
type ConnPool struct {
connMap map[string]*ConnectionEntry
factory Connectionfactory
//for robin
hosts []string
hosts_i uint32
mtx sync.Mutex
//capacity
maxIdle int
}
func NewConnPool(maxIdle int, factory Connectionfactory) *ConnPool {
this := new(ConnPool)
this.connMap = make(map[string]*ConnectionEntry, 0)
this.factory = factory
this.maxIdle = maxIdle
this.hosts_i = 0
return this
}
func (this *ConnPool) Hosts() []string {
return this.hosts
}
func (this *ConnPool) Health(host string) *Health {
if !this.checkHostExist(host) {
this.AddHostEntry(host)
}
return this.connMap[host].health
}
//thread dangerous
func (this *ConnPool) GetByHost(host string) (conn IConnection, err error) {
if !this.checkHostExist(host) {
this.AddHostEntry(host)
}
if this.Health(host).State == StateDisconnected {
return nil, svrDownError
} else if this.Health(host).State == StateDimission {
return nil, svrDimissionError
} else {
select {
case conn = <-this.connMap[host].connList:
atomic.AddInt32(&this.Health(host).Idle, -1)
default:
conn, err = this.factory(host)
if err == nil {
atomic.AddInt32(&this.Health(host).Total, 1)
this.Health(host).State = StateConnected
} else {
this.turnOff(host, err)
}
}
}
return
}
func (this *ConnPool) Get() (conn IConnection, err error) {
for i := 0; i < len(this.hosts); i++ {
host := this.getRobinHost()
conn, err = this.GetByHost(host)
if err == nil {
break
}
}
return
}
//thread dangerous
func (this *ConnPool) Return(conn IConnection) {
if conn != nil {
if this.Health(conn.HostAddr()).State == StateConnected {
//return back to pool
select {
case this.connMap[conn.HostAddr()].connList <- conn:
atomic.AddInt32(&this.Health(conn.HostAddr()).Idle, 1)
default:
atomic.AddInt32(&this.Health(conn.HostAddr()).Total, -1)
conn.Close()
}
} else {
conn.Close()
}
}
}
func (this *ConnPool) AddHostEntry(host string) {
this.mtx.Lock()
if !this.checkHostExist(host) {
connList := make(chan IConnection, this.maxIdle)
this.connMap[host] = &ConnectionEntry{&Health{StateConnecting, 0, 0}, connList}
this.hosts = append(this.hosts, host)
}
this.mtx.Unlock()
this.connMap[host].health.State = StateConnecting
}
func (this *ConnPool) DimissionHostEntry(host string) {
if !this.checkHostExist(host) {
return
}
this.connMap[host].health.State = StateDimission
}
func (this *ConnPool) TurnOff(conn IConnection, err error) {
if conn != nil {
conn.Close()
this.turnOff(conn.HostAddr(), err)
}
}
func (this *ConnPool) HealthMessage() string {
var buf bytes.Buffer
for k, v := range this.connMap {
buf.WriteString(fmt.Sprintf("host=[%s] state=[%s] idle=[%d] total=[%d]\n", k, healthStateDef[v.health.State], v.health.Idle, v.health.Total))
}
return buf.String()
}
func (this *ConnPool) turnOff(host string, err error) {
if this.Health(host).State == StateConnected {
//clean state and idle connections of this addr
this.Health(host).State = StateDisconnected
this.Health(host).Idle = 0
this.Health(host).Total = 0
this.cleanIdle(host)
time.AfterFunc(
retryDuration,
func() {
this.Health(host).State = StateConnecting
})
}
}
func (this *ConnPool) getRobinHost() string {
atomic.AddUint32(&this.hosts_i, 1)
return this.hosts[this.hosts_i%uint32(len(this.hosts))]
}
func (this *ConnPool) cleanIdle(host string) {
for {
select {
case conn := <-this.connMap[host].connList:
conn.Close()
default:
return
}
}
}
func (this *ConnPool) checkHostExist(host string) bool {
_, ok := this.connMap[host]
return ok
}