@@ -10,6 +10,7 @@ import (
10
10
"go.mongodb.org/mongo-driver/mongo"
11
11
"go.mongodb.org/mongo-driver/mongo/options"
12
12
13
+ "github.com/spaceuptech/space-cloud/gateway/config"
13
14
"github.com/spaceuptech/space-cloud/gateway/model"
14
15
"github.com/spaceuptech/space-cloud/gateway/utils"
15
16
)
@@ -21,11 +22,12 @@ type Mongo struct {
21
22
connection string
22
23
dbName string
23
24
client * mongo.Client
25
+ driverConf config.DriverConfig
24
26
}
25
27
26
28
// Init initialises a new mongo instance
27
- func Init (enabled bool , connection , dbName string ) (mongoStub * Mongo , err error ) {
28
- mongoStub = & Mongo {dbName : dbName , enabled : enabled , connection : connection , client : nil }
29
+ func Init (enabled bool , connection , dbName string , driverConf config. DriverConfig ) (mongoStub * Mongo , err error ) {
30
+ mongoStub = & Mongo {dbName : dbName , enabled : enabled , connection : connection , client : nil , driverConf : driverConf }
29
31
30
32
if mongoStub .enabled {
31
33
err = mongoStub .connect ()
@@ -44,8 +46,8 @@ func (m *Mongo) Close() error {
44
46
}
45
47
46
48
// IsSame checks if we've got the same connection string
47
- func (m * Mongo ) IsSame (conn , dbName string ) bool {
48
- return strings .HasPrefix (m .connection , conn ) && dbName == m .dbName
49
+ func (m * Mongo ) IsSame (conn , dbName string , driverConf config. DriverConfig ) bool {
50
+ return strings .HasPrefix (m .connection , conn ) && dbName == m .dbName && driverConf . MaxConn == m . driverConf . MaxConn && driverConf . MaxIdleTimeout == m . driverConf . MaxIdleTimeout && driverConf . MinConn == m . driverConf . MinConn
49
51
}
50
52
51
53
// IsClientSafe checks whether database is enabled and connected
@@ -70,7 +72,29 @@ func (m *Mongo) connect() error {
70
72
ctx , cancel := context .WithTimeout (context .Background (), timeOut )
71
73
defer cancel ()
72
74
73
- client , err := mongo .NewClient (options .Client ().ApplyURI (m .connection ))
75
+ opts := options .Client ().ApplyURI (m .connection )
76
+
77
+ maxConn := m .driverConf .MaxConn
78
+ if maxConn == 0 {
79
+ maxConn = 100
80
+ }
81
+
82
+ maxIdleTimeout := m .driverConf .MaxIdleTimeout
83
+ if maxIdleTimeout == 0 {
84
+ maxIdleTimeout = 60 * 5 * 1000
85
+ }
86
+
87
+ minConn := m .driverConf .MinConn
88
+ if minConn == 0 {
89
+ minConn = 10
90
+ }
91
+
92
+ opts = opts .SetMaxPoolSize ((uint64 )(maxConn ))
93
+ duration := time .Duration (maxIdleTimeout ) * time .Millisecond
94
+ opts = opts .SetMaxConnIdleTime (duration )
95
+ opts = opts .SetMinPoolSize (minConn )
96
+ client , err := mongo .NewClient (opts )
97
+
74
98
if err != nil {
75
99
return err
76
100
}
0 commit comments