/
mongo-connection-pool.go
68 lines (52 loc) · 1.12 KB
/
mongo-connection-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
package mongo_pool_service
import (
"fmt"
"sync"
)
type MongoConnectionPool struct {
wg sync.WaitGroup
conf MongoConfig
serverPool map[int]*MongoDBServer
InputChannel chan MongoLogData
}
func NewMongoConnectionPool(conf MongoConfig) *MongoConnectionPool {
p := new(MongoConnectionPool)
p.conf = conf
p.InputChannel = make(chan MongoLogData)
return p
}
func (s *MongoConnectionPool) Terminate() {
s.wg.Done()
}
func (s *MongoConnectionPool) Run() {
poolCount := s.conf.ConnectionPoolSize
s.serverPool = map[int]*MongoDBServer{}
go func() {
s.wg.Add(1)
for i := 0; i < poolCount; i++ {
// db init
mongoServer := NewMongoDBServer(i, s.conf)
mongoServer.Connect()
defer mongoServer.Disconnect(nil)
s.serverPool[i] = mongoServer
fmt.Println(s.serverPool[i])
}
go func() {
var roundRobinPos = 0
for {
select {
case v := <-s.InputChannel:
{
if roundRobinPos >= poolCount {
roundRobinPos = 0
}
s.serverPool[roundRobinPos].LogDataChannel <- v
roundRobinPos++
}
}
}
}()
fmt.Println(s.serverPool)
s.wg.Wait()
}()
}