-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.go
88 lines (69 loc) · 2.12 KB
/
link.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
package mongohelper
import (
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
)
// Link is a concentrator wrapper for mongodb client
type Link struct {
client *mongo.Client
options Options
}
// insistOnFail returns l.options.reconnectionInsistOnFail value
func (l Link) insistOnFail() bool {
return l.options.reconnectionInsistOnFail
}
// canInsist checks if this engine can retry to connect database, considering the options rules
func (l Link) canInsist() bool {
if l.options.reconnectionAttemptsLimit > 0 && l.options.attempts < l.options.reconnectionAttemptsLimit {
return true
}
if l.options.reconnectionAttemptsLimitMinutes > 0 {
expiration := l.options.lastConnection.Add(time.Duration(l.options.reconnectionAttemptsLimitMinutes) * time.Minute)
if time.Now().After(expiration) {
return true
}
}
return false
}
// wait N seconds before next (9)re)connection attempt
func (l Link) wait() {
timeout := time.Duration(l.options.reconnectionSecondsBetweenAttempts) * time.Second
time.Sleep(timeout)
}
// increment increments in one the connection attempt counter
func (l *Link) increment() {
if l.options.reconnectionAttemptsLimit > 0 {
l.options.attempts++
}
}
// notifyConnection set attempts to zero and lastConnection to NOW
func (l *Link) notifyConnection() {
if l.options.reconnectionAttemptsLimit > 0 {
l.options.attempts = 0
}
if l.options.reconnectionAttemptsLimitMinutes > 0 {
l.options.lastConnection = time.Now()
}
if l.options.printLogMessages {
l.log("link.notifyConnection", "mongodb connected")
}
}
// log print log message if allowed by programmer in options
func (l Link) log(routine, message string) {
if l.options.printLogMessages {
log.Printf("%s - mongohelper %s - %s\n", time.Now().Format(time.RFC3339), routine, message)
}
}
func (l Link) appName() string {
return l.options.appName
}
func (l Link) connectionString() string {
return l.options.connString
}
func (l Link) connTimeout() time.Duration {
return time.Duration(l.options.connTimeoutSeconds) * time.Second
}
func (l Link) execTimeout() time.Duration {
return time.Duration(l.options.execTimeoutSeconds) * time.Second
}