forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
119 lines (103 loc) · 3.01 KB
/
options.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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package connection
import (
"time"
)
type config struct {
appName string
connectTimeout time.Duration
dialer Dialer
handshaker Handshaker
idleTimeout time.Duration
lifeTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
tlsConfig *TLSConfig
}
func newConfig(opts ...Option) (*config, error) {
cfg := &config{
connectTimeout: 30 * time.Second,
dialer: DefaultDialer,
idleTimeout: 10 * time.Minute,
lifeTimeout: 30 * time.Minute,
}
for _, opt := range opts {
err := opt(cfg)
if err != nil {
return nil, err
}
}
return cfg, nil
}
// Option is used to configure a connection.
type Option func(*config) error
// WithAppName sets the application name which gets sent to MongoDB when it
// first connects.
func WithAppName(fn func(string) string) Option {
return func(c *config) error {
c.appName = fn(c.appName)
return nil
}
}
// WithConnectTimeout configures the maximum amount of time a dial will wait for a
// connect to complete. The default is 30 seconds.
func WithConnectTimeout(fn func(time.Duration) time.Duration) Option {
return func(c *config) error {
c.connectTimeout = fn(c.connectTimeout)
return nil
}
}
// WithDialer configures the Dialer to use when making a new connection to MongoDB.
func WithDialer(fn func(Dialer) Dialer) Option {
return func(c *config) error {
c.dialer = fn(c.dialer)
return nil
}
}
// WithHandshaker configures the Handshaker that wll be used to initialize newly
// dialed connections.
func WithHandshaker(fn func(Handshaker) Handshaker) Option {
return func(c *config) error {
c.handshaker = fn(c.handshaker)
return nil
}
}
// WithIdleTimeout configures the maximum idle time to allow for a connection.
func WithIdleTimeout(fn func(time.Duration) time.Duration) Option {
return func(c *config) error {
c.idleTimeout = fn(c.idleTimeout)
return nil
}
}
// WithLifeTimeout configures the maximum life of a connection.
func WithLifeTimeout(fn func(time.Duration) time.Duration) Option {
return func(c *config) error {
c.lifeTimeout = fn(c.lifeTimeout)
return nil
}
}
// WithReadTimeout configures the maximum read time for a connection.
func WithReadTimeout(fn func(time.Duration) time.Duration) Option {
return func(c *config) error {
c.readTimeout = fn(c.readTimeout)
return nil
}
}
// WithWriteTimeout configures the maximum write time for a connection.
func WithWriteTimeout(fn func(time.Duration) time.Duration) Option {
return func(c *config) error {
c.writeTimeout = fn(c.writeTimeout)
return nil
}
}
// WithTLSConfig configures the TLS options for a connection.
func WithTLSConfig(fn func(*TLSConfig) *TLSConfig) Option {
return func(c *config) error {
c.tlsConfig = fn(c.tlsConfig)
return nil
}
}