/
mongo.go
57 lines (44 loc) · 1.35 KB
/
mongo.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
package db
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/Nexters/myply/infrastructure/configs"
)
// MongoInstance contains the Mongo client and database objects
type MongoInstance struct {
Client *mongo.Client
Db *mongo.Database // connection
}
func NewMongoDB(config *configs.Config) (*MongoInstance, error) {
mongoURI := config.MongoURI + "/" + config.MongoDBName
clientOptions := options.Client().ApplyURI(mongoURI)
switch config.Phase {
case configs.Production:
clientOptions.SetHeartbeatInterval(15 * time.Second)
clientOptions.SetMaxPoolSize(100)
clientOptions.SetMinPoolSize(1)
clientOptions.SetMaxConnIdleTime(10 * time.Second)
default:
clientOptions.SetHeartbeatInterval(10 * time.Second) // default 10s
clientOptions.SetMaxPoolSize(100) // default 100
clientOptions.SetMinPoolSize(0) // default 0
clientOptions.SetMaxConnIdleTime(0) // default 0s
}
client, err := mongo.NewClient(clientOptions)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), config.MongoTTL)
defer cancel()
err = client.Connect(ctx)
db := client.Database(config.MongoDBName)
if err != nil {
return nil, err
}
return &MongoInstance{
Client: client,
Db: db,
}, nil
}