-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmgopool.go
More file actions
87 lines (73 loc) · 1.56 KB
/
mgopool.go
File metadata and controls
87 lines (73 loc) · 1.56 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
package mgopool
import (
"log"
"math/rand"
"time"
"github.com/globalsign/mgo"
)
const (
sessionTimeout = 60 * 60 * 3
poolSize = 128
)
// DB ...
type DB struct {
dbname string
sessionPool [poolSize]*mgo.Session
timestamp [poolSize]int64
}
// DefaultTimeout
var DefaultTimeout = time.Second * 5
var db *DB
// NewDB ...
func NewDB(name string, addr string) (*DB, error) {
if db == nil {
db = &DB{}
db.dbname = name
mongoDBDialInfo, err := mgo.ParseURL(addr)
if err != nil {
return nil, err
}
if mongoDBDialInfo.Timeout == 0 {
mongoDBDialInfo.Timeout = DefaultTimeout
}
session, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
return nil, err
}
session.SetMode(mgo.Monotonic, true)
session.SetPoolLimit(512)
for i := 0; i < poolSize; i++ {
db.sessionPool[i] = session.Copy()
db.timestamp[i] = time.Now().Unix()
}
}
return db, nil
}
// Collection ...
func (b *DB) Collection(c string) *mgo.Collection {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ndx := r.Intn(poolSize)
if ndx == 0 {
ndx = 1
}
defer func() {
if err := recover(); err != nil {
log.Println(err)
b.sessionPool[0].Refresh()
b.sessionPool[ndx] = b.sessionPool[0].Copy()
}
}()
now := time.Now().Unix()
if now-b.timestamp[ndx] > sessionTimeout {
log.Printf("fresh index %d, %d, %d", ndx, b.timestamp[ndx], now)
b.sessionPool[ndx].Refresh()
}
b.timestamp[ndx] = now
return b.sessionPool[ndx].DB(b.dbname).C(c)
}
// Close ...
func (b *DB) Close() {
for i := 0; i < poolSize; i++ {
b.sessionPool[i].Close()
}
}