-
Notifications
You must be signed in to change notification settings - Fork 6
/
dbinit.go
116 lines (104 loc) · 2.89 KB
/
dbinit.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
// Copyright 2019 The fsn-go-sdk Authors
// This file is part of the fsn-go-sdk library.
//
// The fsn-go-sdk library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The fsn-go-sdk library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the fsn-go-sdk library. If not, see <http://www.gnu.org/licenses/>.
package mongodb
import (
"fmt"
"time"
"github.com/fsn-dev/fsn-go-sdk/efsn/log"
"gopkg.in/mgo.v2"
)
var (
database *mgo.Database
session *mgo.Session
dialInfo *mgo.DialInfo
)
// MongoServerInit int mongodb server session
func MongoServerInit(addrs []string, dbname, user, pass string) {
initDialInfo(addrs, dbname, user, pass)
mongoConnect()
initCollections()
go checkMongoSession()
}
func initDialInfo(addrs []string, db, user, pass string) {
dialInfo = &mgo.DialInfo{
Addrs: addrs,
Database: db,
Username: user,
Password: pass,
}
}
func mongoConnect() {
if session != nil { // when reconnect
session.Close()
}
log.Info("[mongodb] connect database start.", "addrs", dialInfo.Addrs, "dbName", dialInfo.Database)
var err error
for {
session, err = mgo.DialWithInfo(dialInfo)
if err == nil {
break
}
log.Warn("[mongodb] dial error", "err", err)
time.Sleep(1 * time.Second)
}
session.SetMode(mgo.Monotonic, true)
session.SetSafe(&mgo.Safe{FSync: true})
database = session.DB(dialInfo.Database)
deinintCollections()
log.Info("[mongodb] connect database finished.", "dbName", dialInfo.Database)
}
// fix 'read tcp 127.0.0.1:43502->127.0.0.1:27917: i/o timeout'
func checkMongoSession() {
for {
time.Sleep(60 * time.Second)
if err := ensureMongoConnected(); err != nil {
log.Info("[mongodb] check session error", "err", err)
log.Info("[mongodb] reconnect database", "dbName", dialInfo.Database)
mongoConnect()
}
}
}
func sessionPing() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recover from error %v", r)
}
}()
for i := 0; i < 6; i++ {
err = session.Ping()
if err == nil {
break
}
time.Sleep(10 * time.Second)
}
return err
}
func ensureMongoConnected() (err error) {
err = sessionPing()
if err != nil {
log.Error("[mongodb] session ping error", "err", err)
log.Info("[mongodb] refresh session.", "dbName", dialInfo.Database)
session.Refresh()
database = session.DB(dialInfo.Database)
deinintCollections()
err = sessionPing()
}
return err
}
// Fsync flush memory to db
func Fsync(async bool) error {
return session.Fsync(async)
}