4
4
"context"
5
5
"time"
6
6
7
+ wErrors "github.com/pkg/errors"
7
8
"go.mongodb.org/mongo-driver/mongo"
8
9
"go.mongodb.org/mongo-driver/mongo/options"
9
10
"go.mongodb.org/mongo-driver/mongo/readpref"
@@ -19,9 +20,6 @@ const (
19
20
type Config struct {
20
21
// DSN is the MongoDB instance connection DSN. Required.
21
22
DSN string
22
- // LogFunc is the callback function for errors logging during check.
23
- // If not set logging is skipped.
24
- LogFunc func (err error , details string , extra ... interface {})
25
23
26
24
// TimeoutConnect defines timeout for establishing mongo connection, if not set - default value is used
27
25
TimeoutConnect time.Duration
@@ -35,10 +33,6 @@ type Config struct {
35
33
// - connection establishing
36
34
// - doing the ping command
37
35
func New (config Config ) func () error {
38
- if config .LogFunc == nil {
39
- config .LogFunc = func (err error , details string , extra ... interface {}) {}
40
- }
41
-
42
36
if config .TimeoutConnect == 0 {
43
37
config .TimeoutConnect = defaultTimeoutConnect
44
38
}
@@ -51,42 +45,44 @@ func New(config Config) func() error {
51
45
config .TimeoutPing = defaultTimeoutPing
52
46
}
53
47
54
- return func () error {
48
+ return func () ( checkErr error ) {
55
49
var ctx context.Context
56
50
var cancel context.CancelFunc
57
51
58
52
client , err := mongo .NewClient (options .Client ().ApplyURI (config .DSN ))
59
53
if err != nil {
60
- config . LogFunc (err , "MongoDB health check failed on client creation" )
61
- return err
54
+ checkErr = wErrors . Wrap (err , "mongoDB health check failed on client creation" )
55
+ return
62
56
}
63
57
64
58
ctx , cancel = context .WithTimeout (context .Background (), config .TimeoutConnect )
65
59
defer cancel ()
66
- err = client .Connect (ctx )
67
60
61
+ err = client .Connect (ctx )
68
62
if err != nil {
69
- config . LogFunc (err , "MongoDB health check failed on connect" )
70
- return err
63
+ checkErr = wErrors . Wrap (err , "mongoDB health check failed on connect" )
64
+ return
71
65
}
72
66
73
67
defer func () {
74
68
ctx , cancel = context .WithTimeout (context .Background (), config .TimeoutDisconnect )
75
69
defer cancel ()
76
- if err := client .Disconnect (ctx ); err != nil {
77
- config .LogFunc (err , "MongoDB health check failed on closing connection" )
70
+
71
+ // override checkErr only if there were no other errors
72
+ if err := client .Disconnect (ctx ); err != nil && checkErr == nil {
73
+ checkErr = wErrors .Wrap (err , "mongoDB health check failed on closing connection" )
78
74
}
79
75
}()
80
76
81
77
ctx , cancel = context .WithTimeout (context .Background (), config .TimeoutPing )
82
78
defer cancel ()
83
- err = client .Ping (ctx , readpref .Primary ())
84
79
80
+ err = client .Ping (ctx , readpref .Primary ())
85
81
if err != nil {
86
- config . LogFunc (err , "MongoDB health check failed during ping" )
87
- return err
82
+ checkErr = wErrors . Wrap (err , "mongoDB health check failed on ping" )
83
+ return
88
84
}
89
85
90
- return nil
86
+ return
91
87
}
92
88
}
0 commit comments