forked from ibmdb/go_ibm_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooling.go
203 lines (191 loc) · 4.14 KB
/
pooling.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
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
package go_ibm_db
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
)
//DBP struct type contains the timeout, dbinstance and connection string
type DBP struct {
sql.DB
con string
n time.Duration
}
//Pool struct contais the about the pool like size, used and available connections
type Pool struct {
availablePool map[string][]*DBP
usedPool map[string][]*DBP
PoolSize int
}
var b *Pool
var ConnMaxLifetime, PoolSize int
//Pconnect will return the pool instance
func Pconnect(PoolSize string) *Pool {
var Size int
count := len(PoolSize)
if count > 0 {
opt := strings.Split(PoolSize, "=")
if opt[0] == "PoolSize" {
Size, _ = strconv.Atoi(opt[1])
} else {
fmt.Println("Not a valid parameter")
}
} else {
Size = 100
}
p := &Pool{
availablePool: make(map[string][]*DBP),
usedPool: make(map[string][]*DBP),
PoolSize: Size,
}
b = p
return p
}
//Psize sets the size of the pool idf value is passed
var Psize int
//Open will check for the connection in the pool
//If not opens a new connection and stores in the pool
func (p *Pool) Open(Connstr string, options ...string) *DBP {
var Time time.Duration
count := len(options)
if count > 0 {
for i := 0; i < count; i++ {
opt := strings.Split(options[i], "=")
if opt[0] == "SetConnMaxLifetime" {
ConnMaxLifetime, _ = strconv.Atoi(opt[1])
Time = time.Duration(ConnMaxLifetime) * time.Second
} else {
fmt.Println("not a valid parameter")
}
}
} else {
Time = 30 * time.Second
}
if Psize < p.PoolSize {
Psize = Psize + 1
if val, ok := p.availablePool[Connstr]; ok {
if len(val) > 1 {
dbpo := val[0]
copy(val[0:], val[1:])
val[len(val)-1] = nil
val = val[:len(val)-1]
p.availablePool[Connstr] = val
p.usedPool[Connstr] = append(p.usedPool[Connstr], dbpo)
dbpo.SetConnMaxLifetime(Time)
return dbpo
} else {
dbpo := val[0]
p.usedPool[Connstr] = append(p.usedPool[Connstr], dbpo)
delete(p.availablePool, Connstr)
dbpo.SetConnMaxLifetime(Time)
return dbpo
}
} else {
db, err := sql.Open("go_ibm_db", Connstr)
if err != nil {
return nil
}
dbi := &DBP{
DB: *db,
con: Connstr,
n: Time,
}
p.usedPool[Connstr] = append(p.usedPool[Connstr], dbi)
dbi.SetConnMaxLifetime(Time)
return dbi
}
} else {
db, err := sql.Open("go_ibm_db", Connstr)
if err != nil {
return nil
}
dbi := &DBP{
DB: *db,
con: Connstr,
}
return dbi
}
}
//Close will make the connection available for the next release
func (d *DBP) Close() {
Psize = Psize - 1
var pos int
i := -1
if valc, okc := b.usedPool[d.con]; okc {
if len(valc) > 1 {
for _, b := range valc {
i = i + 1
if b == d {
pos = i
}
}
dbpc := valc[pos]
copy(valc[pos:], valc[pos+1:])
valc[len(valc)-1] = nil
valc = valc[:len(valc)-1]
b.usedPool[d.con] = valc
b.availablePool[d.con] = append(b.availablePool[d.con], dbpc)
} else {
dbpc := valc[0]
b.availablePool[d.con] = append(b.availablePool[d.con], dbpc)
delete(b.usedPool, d.con)
}
go d.Timeout()
} else {
d.DB.Close()
}
}
//Timeout for closing the connection in pool
func (d *DBP) Timeout() {
var pos int
i := -1
select {
case <-time.After(d.n):
if valt, okt := b.availablePool[d.con]; okt {
if len(valt) > 1 {
for _, b := range valt {
i = i + 1
if b == d {
pos = i
}
}
dbpt := valt[pos]
copy(valt[pos:], valt[pos+1:])
valt[len(valt)-1] = nil
valt = valt[:len(valt)-1]
b.availablePool[d.con] = valt
dbpt.DB.Close()
} else {
dbpt := valt[0]
dbpt.DB.Close()
delete(b.availablePool, d.con)
}
}
}
}
//Release will close all the connections in the pool
func (p *Pool) Release() {
if p.availablePool != nil {
for _, vala := range p.availablePool {
for _, dbpr := range vala {
dbpr.DB.Close()
}
}
p.availablePool = nil
}
if p.usedPool != nil {
for _, valu := range p.usedPool {
for _, dbpr := range valu {
dbpr.DB.Close()
}
}
p.usedPool = nil
}
}
// Display will print the values in the map
func (p *Pool) Display() {
fmt.Println(p.availablePool)
fmt.Println(p.usedPool)
fmt.Println(p.PoolSize)
}