-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmgo.go
120 lines (107 loc) · 2.31 KB
/
xmgo.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
package xmgo
import (
"errors"
"fmt"
"sync"
"time"
mgo "gopkg.in/mgo.v2"
)
type MgoPool struct {
sync.RWMutex
sessions chan *mgo.Session
poolSize int
factory Factory
}
type Factory func() (*mgo.Session, error)
func adaptor(action string, addr []string, timeout time.Duration, username string, passwd string) Factory {
ff := func() (*mgo.Session, error) {
var (
session *mgo.Session
err error
)
if len(addr) == 0 {
return nil, errors.New("mongodb address is empty")
}
switch action {
case "single":
if timeout == 0 {
session, err = mgo.Dial(addr[0])
} else {
session, err = mgo.DialWithTimeout(addr[0], timeout)
}
case "cluster":
dialinfo := mgo.DialInfo{
Addrs: addr,
Timeout: timeout,
Username: username,
Password: passwd,
}
session, err = mgo.DialWithInfo(&dialinfo)
}
return session, err
}
return ff
}
func InitMgoPool(mode string, addr []string, timeout time.Duration, username string, passwd string, pool_size int) *MgoPool {
mgo_pool := new(MgoPool)
mgo_pool.poolSize = pool_size
mgo_pool.sessions = make(chan *mgo.Session, pool_size)
mgo_pool.factory = adaptor(mode, addr, timeout, username, passwd)
return mgo_pool
}
func (mp *MgoPool) getSessions() chan *mgo.Session {
mp.RLock()
defer mp.RUnlock()
return mp.sessions
}
func (mp *MgoPool) Get() (*mgo.Session, error) {
sess := mp.getSessions()
if sess == nil {
return nil, errors.New("mongo session pool is nil")
}
select {
case session := <-sess:
if session == nil {
return nil, errors.New("get nil session")
}
session.Refresh()
return session, nil
default:
session, err := mp.factory()
return session, err
}
}
func (mp *MgoPool) Put(session *mgo.Session) {
sess := mp.getSessions()
if sess == nil {
sess = make(chan *mgo.Session, mp.poolSize)
mp.sessions = sess
}
mp.Lock()
defer mp.Unlock()
if len(sess) >= mp.poolSize {
session.Close()
} else {
sess <- session
}
return
}
func (mp *MgoPool) Dump() {
fmt.Printf("mgo pool length: %d", mp.poolSize)
}
func (mp *MgoPool) CloseSession(session *mgo.Session) {
session.Close()
}
func (mp *MgoPool) CloseAll() {
mp.Lock()
defer mp.Unlock()
for sess := range mp.sessions {
sess.Close()
}
close(mp.sessions)
}
func (mp *MgoPool) Len() int {
mp.RLock()
defer mp.RUnlock()
return len(mp.sessions)
}