-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.go
156 lines (131 loc) · 4.99 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2023 Kapeta Inc.
// SPDX-License-Identifier: MIT
package mongo
import (
"context"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/kapetacom/sdk-go-config/providers"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
)
const RESOURCE_TYPE = "kapeta/resource-type-mongodb"
const RESOURCE_PORT = "mongodb"
type MongoDB struct {
*mongo.Client
dbName string
}
func (m *MongoDB) DB() *mongo.Database {
return m.Database(m.dbName)
}
func NewMongoDB(config providers.ConfigProvider, resourceName string) (*MongoDB, error) {
resInfo, err := config.GetResourceInfo(RESOURCE_TYPE, RESOURCE_PORT, resourceName)
if err != nil {
return nil, err
}
url, err := createConnectionString(resInfo, resourceName)
if err != nil {
return nil, err
}
ctx := context.Background()
log.Printf("Connecting to mongodb database: %s\n", resourceName)
options := options.Client().
ApplyURI(url).
SetAppName(config.GetBlockReference()).
// Enable automatic retrying of writes if they fail due to transient errors
SetRetryWrites(true).
// Enable automatic retrying of reads if they fail due to transient errors
SetRetryReads(true)
// Maximum number of connections allowed in the connection pool
// Higher values allow more concurrent operations but consume more resources
maxPoolSize := envGetInt("MONGO_MAX_POOL_SIZE", 50)
options.SetMaxPoolSize(uint64(maxPoolSize))
// Minimum number of connections to maintain in the connection pool
// Helps reduce connection overhead by keeping a baseline of ready connections
minPoolSize := envGetInt("MONGO_MIN_POOL_SIZE", 10)
options.SetMinPoolSize(uint64(minPoolSize))
// Maximum time a connection can remain idle in the pool before being removed
// Helps clean up unused connections to free up resources
maxConnIdleTime := envGetInt("MONGO_MAX_CONN_IDLE_TIME", 120)
options.SetMaxConnIdleTime(time.Duration(maxConnIdleTime) * time.Second)
// Maximum time to wait for a connection to be established with the server
// Prevents hanging indefinitely when the server is unreachable
connectTimeout := envGetInt("MONGO_CONNECT_TIMEOUT", 10)
options.SetConnectTimeout(time.Duration(connectTimeout) * time.Second)
// Maximum time to wait for server selection during operations
// Useful when working with replica sets to prevent long waits for primary selection
serverSelectionTimeout := envGetInt("MONGO_SERVER_SELECTION_TIMEOUT", 5)
options.SetServerSelectionTimeout(time.Duration(serverSelectionTimeout) * time.Second)
// Maximum time to wait for database operations to complete
// Prevents operations from hanging indefinitely and ensures timely failures
timeout := envGetInt("MONGO_TIMEOUT", 30)
options.SetTimeout(time.Duration(timeout) * time.Second)
// Write concern determines the level of acknowledgment requested from MongoDB for write operations
// "majority" ensures writes are acknowledged by a majority of replica set members for strong consistency
writeMajority := "majority"
if os.Getenv("MONGO_WRITE_MAJORITY") != "" {
writeMajority = envGetString("MONGO_WRITE_MAJORITY", writeMajority)
}
options.SetWriteConcern(&writeconcern.WriteConcern{
W: writeMajority,
})
client, err := mongo.Connect(options)
if err != nil {
return nil, err
}
log.Println("Checking connection to mongodb database")
err = client.Ping(ctx, nil)
if err != nil {
return nil, err
}
log.Printf("Connected successfully to mongodb database: %s\n", resourceName)
return &MongoDB{client, getDBName(resInfo, resourceName)}, nil
}
func createConnectionString(resInfo *providers.ResourceInfo, resourceName string) (string, error) {
protocol := getProtocol(resInfo)
dbName := getDBName(resInfo, resourceName)
url := ""
if protocol == "mongodb+srv" {
url = fmt.Sprintf("%s://%s:%s@%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, dbName) + "?authSource=admin"
} else {
url = fmt.Sprintf("%s://%s:%s@%s:%s/%s", protocol, resInfo.Credentials["username"], resInfo.Credentials["password"], resInfo.Host, resInfo.Port, dbName) + "?authSource=admin&directConnection=true"
}
if resInfo.Options["ssl"] != nil {
url += fmt.Sprintf("&ssl=%s", resInfo.Options["ssl"])
}
return url, nil
}
func getProtocol(resInfo *providers.ResourceInfo) string {
if resInfo.Options["protocol"] != nil && resInfo.Options["protocol"] != "" {
return fmt.Sprintf("%v", resInfo.Options["protocol"])
}
return "mongodb"
}
func getDBName(resInfo *providers.ResourceInfo, resourceName string) string {
if resInfo.Options["dbName"] != nil && resInfo.Options["dbName"] != "" {
return fmt.Sprintf("%v", resInfo.Options["dbName"])
}
return resourceName
}
func envGetInt(key string, defaultValue int) int {
val := os.Getenv(key)
if val == "" {
return defaultValue
}
i, err := strconv.Atoi(val)
if err != nil {
return defaultValue
}
return i
}
func envGetString(key string, defaultValue string) string {
val := os.Getenv(key)
if val == "" {
return defaultValue
}
return val
}