...
1
2
3
4
5
6
7 package topology
8
9 import (
10 "time"
11
12 "go.mongodb.org/mongo-driver/bson"
13 "go.mongodb.org/mongo-driver/bson/bsoncodec"
14 "go.mongodb.org/mongo-driver/event"
15 "go.mongodb.org/mongo-driver/internal/logger"
16 "go.mongodb.org/mongo-driver/x/mongo/driver"
17 "go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
18 "go.mongodb.org/mongo-driver/x/mongo/driver/session"
19 )
20
21 var defaultRegistry = bson.NewRegistryBuilder().Build()
22
23 type serverConfig struct {
24 clock *session.ClusterClock
25 compressionOpts []string
26 connectionOpts []ConnectionOption
27 appname string
28 heartbeatInterval time.Duration
29 heartbeatTimeout time.Duration
30 serverMonitoringMode string
31 serverMonitor *event.ServerMonitor
32 registry *bsoncodec.Registry
33 monitoringDisabled bool
34 serverAPI *driver.ServerAPIOptions
35 loadBalanced bool
36
37
38 maxConns uint64
39 minConns uint64
40 maxConnecting uint64
41 poolMonitor *event.PoolMonitor
42 logger *logger.Logger
43 poolMaxIdleTime time.Duration
44 poolMaintainInterval time.Duration
45 }
46
47 func newServerConfig(opts ...ServerOption) *serverConfig {
48 cfg := &serverConfig{
49 heartbeatInterval: 10 * time.Second,
50 heartbeatTimeout: 10 * time.Second,
51 registry: defaultRegistry,
52 }
53
54 for _, opt := range opts {
55 if opt == nil {
56 continue
57 }
58 opt(cfg)
59 }
60
61 return cfg
62 }
63
64
65 type ServerOption func(*serverConfig)
66
67
68
69 func ServerAPIFromServerOptions(opts []ServerOption) *driver.ServerAPIOptions {
70 return newServerConfig(opts...).serverAPI
71 }
72
73 func withMonitoringDisabled(fn func(bool) bool) ServerOption {
74 return func(cfg *serverConfig) {
75 cfg.monitoringDisabled = fn(cfg.monitoringDisabled)
76 }
77 }
78
79
80 func WithConnectionOptions(fn func(...ConnectionOption) []ConnectionOption) ServerOption {
81 return func(cfg *serverConfig) {
82 cfg.connectionOpts = fn(cfg.connectionOpts...)
83 }
84 }
85
86
87 func WithCompressionOptions(fn func(...string) []string) ServerOption {
88 return func(cfg *serverConfig) {
89 cfg.compressionOpts = fn(cfg.compressionOpts...)
90 }
91 }
92
93
94 func WithServerAppName(fn func(string) string) ServerOption {
95 return func(cfg *serverConfig) {
96 cfg.appname = fn(cfg.appname)
97 }
98 }
99
100
101 func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption {
102 return func(cfg *serverConfig) {
103 cfg.heartbeatInterval = fn(cfg.heartbeatInterval)
104 }
105 }
106
107
108
109 func WithHeartbeatTimeout(fn func(time.Duration) time.Duration) ServerOption {
110 return func(cfg *serverConfig) {
111 cfg.heartbeatTimeout = fn(cfg.heartbeatTimeout)
112 }
113 }
114
115
116
117 func WithMaxConnections(fn func(uint64) uint64) ServerOption {
118 return func(cfg *serverConfig) {
119 cfg.maxConns = fn(cfg.maxConns)
120 }
121 }
122
123
124
125
126 func WithMinConnections(fn func(uint64) uint64) ServerOption {
127 return func(cfg *serverConfig) {
128 cfg.minConns = fn(cfg.minConns)
129 }
130 }
131
132
133
134
135 func WithMaxConnecting(fn func(uint64) uint64) ServerOption {
136 return func(cfg *serverConfig) {
137 cfg.maxConnecting = fn(cfg.maxConnecting)
138 }
139 }
140
141
142
143
144 func WithConnectionPoolMaxIdleTime(fn func(time.Duration) time.Duration) ServerOption {
145 return func(cfg *serverConfig) {
146 cfg.poolMaxIdleTime = fn(cfg.poolMaxIdleTime)
147 }
148 }
149
150
151
152 func WithConnectionPoolMaintainInterval(fn func(time.Duration) time.Duration) ServerOption {
153 return func(cfg *serverConfig) {
154 cfg.poolMaintainInterval = fn(cfg.poolMaintainInterval)
155 }
156 }
157
158
159 func WithConnectionPoolMonitor(fn func(*event.PoolMonitor) *event.PoolMonitor) ServerOption {
160 return func(cfg *serverConfig) {
161 cfg.poolMonitor = fn(cfg.poolMonitor)
162 }
163 }
164
165
166 func WithServerMonitor(fn func(*event.ServerMonitor) *event.ServerMonitor) ServerOption {
167 return func(cfg *serverConfig) {
168 cfg.serverMonitor = fn(cfg.serverMonitor)
169 }
170 }
171
172
173 func WithClock(fn func(clock *session.ClusterClock) *session.ClusterClock) ServerOption {
174 return func(cfg *serverConfig) {
175 cfg.clock = fn(cfg.clock)
176 }
177 }
178
179
180
181 func WithRegistry(fn func(*bsoncodec.Registry) *bsoncodec.Registry) ServerOption {
182 return func(cfg *serverConfig) {
183 cfg.registry = fn(cfg.registry)
184 }
185 }
186
187
188 func WithServerAPI(fn func(serverAPI *driver.ServerAPIOptions) *driver.ServerAPIOptions) ServerOption {
189 return func(cfg *serverConfig) {
190 cfg.serverAPI = fn(cfg.serverAPI)
191 }
192 }
193
194
195 func WithServerLoadBalanced(fn func(bool) bool) ServerOption {
196 return func(cfg *serverConfig) {
197 cfg.loadBalanced = fn(cfg.loadBalanced)
198 }
199 }
200
201
202 func withLogger(fn func() *logger.Logger) ServerOption {
203 return func(cfg *serverConfig) {
204 cfg.logger = fn()
205 }
206 }
207
208
209
210 func withServerMonitoringMode(mode *string) ServerOption {
211 return func(cfg *serverConfig) {
212 if mode != nil {
213 cfg.serverMonitoringMode = *mode
214
215 return
216 }
217
218 cfg.serverMonitoringMode = connstring.ServerMonitoringModeAuto
219 }
220 }
221
View as plain text