Skip to content

Commit f287f96

Browse files
author
Tai Mai Huu
committed
feat: Update MongoDB configuration to include server selection timeout and adjust connection timeout
1 parent c0870d7 commit f287f96

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

component/mongodbc/mongodbc.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ import (
1414
)
1515

1616
type config struct {
17-
url string
18-
username string
19-
password string
20-
authMechanism string
21-
authSource string
22-
maxPoolSize uint64
23-
minPoolSize uint64
24-
timeout time.Duration
25-
connectionTimeout time.Duration
17+
url string
18+
username string
19+
password string
20+
authMechanism string
21+
authSource string
22+
maxPoolSize uint64
23+
minPoolSize uint64
24+
timeout time.Duration
25+
connectionTimeout time.Duration
26+
ServerSelectionTimeout time.Duration
2627
}
2728

2829
type mongoDbComponent struct {
@@ -86,7 +87,8 @@ func (m *mongoDbComponent) InitFlags() {
8687
flag.Uint64Var(&m.maxPoolSize, m.id+"-max-pool-size", 100, "mongodb max pool size. default: 100")
8788

8889
flag.DurationVar(&m.timeout, m.id+"-timeout", 10*time.Second, "mongodb timeout. default: 10s")
89-
flag.DurationVar(&m.connectionTimeout, m.id+"-connection-timeout", 10*time.Second, "mongodb connection timeout. default: 10s")
90+
flag.DurationVar(&m.connectionTimeout, m.id+"-connection-timeout", 30*time.Second, "mongodb connection timeout. default: 30s")
91+
flag.DurationVar(&m.ServerSelectionTimeout, m.id+"-server-selection-timeout", 30*time.Second, "mongodb server selection timeout. default: 30s")
9092

9193
}
9294

@@ -95,20 +97,21 @@ func (m *mongoDbComponent) Activate(ctx sctx.ServiceContext) error {
9597
opts := options.Client()
9698
// set url
9799
opts.ApplyURI(m.url)
98-
// set timeout
99-
opts.SetTimeout(m.timeout)
100100

101101
// set min pool size
102102
opts.SetMinPoolSize(m.minPoolSize)
103103

104104
// set max pool size
105105
opts.SetMaxPoolSize(m.maxPoolSize)
106106

107+
// set timeout
108+
opts.SetTimeout(m.timeout)
109+
107110
// set connection timeout
108-
opts.SetConnectTimeout(m.timeout)
111+
opts.SetConnectTimeout(m.connectionTimeout)
109112

110113
// set ConnectTimeout
111-
opts.SetServerSelectionTimeout(m.connectionTimeout)
114+
opts.SetServerSelectionTimeout(m.ServerSelectionTimeout)
112115

113116
// set auth database
114117
if m.username != "" && m.password != "" {

examples/mongodbcomp/.env.example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ MONGODB_AUTH_MECHANISM="SCRAM-SHA-256"
77
# AuthSource. default: '' (-mongodb-auth-source)
88
MONGODB_AUTH_SOURCE=
99

10-
# mongodb connection timeout. default: 10s (-mongodb-connection-timeout)
11-
MONGODB_CONNECTION_TIMEOUT=10s
10+
# mongodb connection timeout. default: 30s (-mongodb-connection-timeout)
11+
MONGODB_CONNECTION_TIMEOUT=30s
1212

1313
# mongodb max pool size. default: 100 (-mongodb-max-pool-size)
1414
MONGODB_MAX_POOL_SIZE=100
@@ -19,6 +19,9 @@ MONGODB_MIN_POOL_SIZE=10
1919
# mongodb password. default: '' (-mongodb-password)
2020
MONGODB_PASSWORD=
2121

22+
# mongodb server selection timeout. default: 30s (-mongodb-server-selection-timeout)
23+
MONGODB_SERVER_SELECTION_TIMEOUT=30s
24+
2225
# mongodb timeout. default: 10s (-mongodb-timeout)
2326
MONGODB_TIMEOUT=10s
2427

0 commit comments

Comments
 (0)