-
Notifications
You must be signed in to change notification settings - Fork 10
/
pool.go
135 lines (112 loc) · 2.94 KB
/
pool.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
package mgop
import (
"gopkg.in/mgo.v2"
"sync"
"time"
"gopkg.in/mgo.v2/bson"
)
type SessionPool interface {
AcquireSession() *sessionWrapper
Size() int
}
type StrongSessionPool struct {
poolmap map[mgo.Mode]upstreamSessionPool
rw sync.RWMutex
//cache a session for ping
size int
}
var modeMap map[mgo.Mode]int = map[mgo.Mode]int{
mgo.Monotonic:1,
mgo.PrimaryPreferred:1,
mgo.Secondary:1,
mgo.SecondaryPreferred:2,
mgo.Nearest:1,
}
//TODO
func dialMixedPool(url string, fixedSize int) (SessionPool, error) {
s, err := mgo.Dial(url)
if err != nil {
return nil, err
}
s.SetMode(mgo.Strong, true)
s.SetSafe(&mgo.Safe{})
p := &StrongSessionPool{}
p.poolmap = make(map[mgo.Mode]upstreamSessionPool, 7)
for k, v := range modeMap {
p.poolmap[k] = newPollingSessionPool(v)
for i := 0; i < v; i++ {
scp := s.Copy()
scp.SetMode(k, true)
p.poolmap[k].appendSession(newSessionWrapper(p, scp))
}
}
p.poolmap[mgo.Strong] = newPollingSessionPool(fixedSize)
for i := 0; i < fixedSize - 1; i++ {
scp := s.Copy()
scp.SetMode(mgo.Strong, true)
scp.SetSafe(&mgo.Safe{})
p.poolmap[mgo.Strong].appendSession(newSessionWrapper(p, scp))
}
p.poolmap[mgo.Strong].appendSession(newSessionWrapper(p, s))
return p, nil
}
func DialStrongPool(url string, fixedSize int) (SessionPool, error) {
s, err := mgo.Dial(url)
if err != nil {
return nil, err
}
p := &StrongSessionPool{
size: fixedSize,
}
p.poolmap = make(map[mgo.Mode]upstreamSessionPool, 1)
p.poolmap[mgo.Strong] = newPollingSessionPool(fixedSize)
for i := 0; i < fixedSize - 1; i++ {
scp := s.Copy()
scp.SetMode(mgo.Strong, true)
p.poolmap[mgo.Strong].appendSession(newSessionWrapper(p, scp))
}
s.SetMode(mgo.Strong, true)
p.poolmap[mgo.Strong].appendSession(newSessionWrapper(p, s))
go p.pinger()
return p, nil
}
func (p *StrongSessionPool) AcquireSession() *sessionWrapper {
p.rw.RLock()
defer p.rw.RUnlock()
return p.poolmap[mgo.Strong].getBest()
}
func (p *StrongSessionPool) Size() int {
return p.size
}
func (p *StrongSessionPool)pinger() {
for {
time.Sleep(15 * time.Second)
p.poolmap[mgo.Strong].foreach(func(sw *sessionWrapper) {
//if session preserved socket is gone or not master,
//refresh the session
//TODO ismaster is less quick than ping;may a hash for host will work
var result isMasterResult
if err := sw.s.Run("ismaster", &result); err != nil || !result.IsMaster {
sw.refresh()
}
}, true)
}
}
// TODO
func (p *StrongSessionPool) acquireSessionWithMode(mode int) (*sessionWrapper, error) {
return nil, nil
}
// TODO
func (p *StrongSessionPool) acquireSessionWithFraction(i int32) *sessionWrapper {
return nil
}
// isMater cmd result
type isMasterResult struct {
IsMaster bool `bson:"ismaster"`
Secondary bool `bson:"secondary"`
Primary string `bson:"primary"`
Hosts []string `bson:"hosts"`
Me string `bson:"me"`
SetName string `bson:"setName"`
ElectionId bson.ObjectId `bson:"electionId"`
}