Skip to content

Commit 29d768e

Browse files
authored
Remove unused ConnectionOption error. (#859)
1 parent 2a4e3db commit 29d768e

File tree

10 files changed

+54
-114
lines changed

10 files changed

+54
-114
lines changed

x/mongo/driver/topology/connection.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ type connection struct {
7272
}
7373

7474
// newConnection handles the creation of a connection. It does not connect the connection.
75-
func newConnection(addr address.Address, opts ...ConnectionOption) (*connection, error) {
76-
cfg, err := newConnectionConfig(opts...)
77-
if err != nil {
78-
return nil, err
79-
}
75+
func newConnection(addr address.Address, opts ...ConnectionOption) *connection {
76+
cfg := newConnectionConfig(opts...)
8077

8178
id := fmt.Sprintf("%s[-%d]", addr, nextConnectionID())
8279

@@ -98,7 +95,7 @@ func newConnection(addr address.Address, opts ...ConnectionOption) (*connection,
9895
}
9996
atomic.StoreInt64(&c.connected, initialized)
10097

101-
return c, nil
98+
return c
10299
}
103100

104101
// setGenerationNumber sets the connection's generation number if a callback has been provided to do so in connection

x/mongo/driver/topology/connection_errors_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ func TestConnectionErrors(t *testing.T) {
2525
t.Run("dial error", func(t *testing.T) {
2626
dialError := errors.New("foo")
2727

28-
conn, err := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
28+
conn := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
2929
return DialerFunc(func(context.Context, string, string) (net.Conn, error) { return nil, dialError })
3030
}))
31-
assert.Nil(t, err, "newConnection error: %v", err)
3231

33-
err = conn.connect(context.Background())
32+
err := conn.connect(context.Background())
3433
assert.True(t, errors.Is(err, dialError), "expected error %v, got %v", dialError, err)
3534
})
3635
t.Run("handshake error", func(t *testing.T) {
37-
conn, err := newConnection(address.Address(""),
36+
conn := newConnection(address.Address(""),
3837
WithHandshaker(func(Handshaker) Handshaker {
3938
return auth.Handshaker(nil, &auth.HandshakeOptions{})
4039
}),
@@ -44,12 +43,11 @@ func TestConnectionErrors(t *testing.T) {
4443
})
4544
}),
4645
)
47-
assert.Nil(t, err, "newConnection error: %v", err)
4846
defer conn.close()
4947

5048
ctx, cancel := context.WithCancel(context.Background())
5149
cancel()
52-
err = conn.connect(ctx)
50+
err := conn.connect(ctx)
5351
assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
5452
})
5553
t.Run("write error", func(t *testing.T) {

x/mongo/driver/topology/connection_options.go

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,156 +58,137 @@ type connectionConfig struct {
5858
getGenerationFn generationNumberFn
5959
}
6060

61-
func newConnectionConfig(opts ...ConnectionOption) (*connectionConfig, error) {
61+
func newConnectionConfig(opts ...ConnectionOption) *connectionConfig {
6262
cfg := &connectionConfig{
6363
connectTimeout: 30 * time.Second,
6464
dialer: nil,
6565
tlsConnectionSource: defaultTLSConnectionSource,
6666
}
6767

6868
for _, opt := range opts {
69-
err := opt(cfg)
70-
if err != nil {
71-
return nil, err
72-
}
69+
opt(cfg)
7370
}
7471

7572
if cfg.dialer == nil {
7673
cfg.dialer = &net.Dialer{}
7774
}
7875

79-
return cfg, nil
76+
return cfg
8077
}
8178

8279
// ConnectionOption is used to configure a connection.
83-
type ConnectionOption func(*connectionConfig) error
80+
type ConnectionOption func(*connectionConfig)
8481

8582
func withTLSConnectionSource(fn func(tlsConnectionSource) tlsConnectionSource) ConnectionOption {
86-
return func(c *connectionConfig) error {
83+
return func(c *connectionConfig) {
8784
c.tlsConnectionSource = fn(c.tlsConnectionSource)
88-
return nil
8985
}
9086
}
9187

9288
// WithCompressors sets the compressors that can be used for communication.
9389
func WithCompressors(fn func([]string) []string) ConnectionOption {
94-
return func(c *connectionConfig) error {
90+
return func(c *connectionConfig) {
9591
c.compressors = fn(c.compressors)
96-
return nil
9792
}
9893
}
9994

10095
// WithConnectTimeout configures the maximum amount of time a dial will wait for a
10196
// Connect to complete. The default is 30 seconds.
10297
func WithConnectTimeout(fn func(time.Duration) time.Duration) ConnectionOption {
103-
return func(c *connectionConfig) error {
98+
return func(c *connectionConfig) {
10499
c.connectTimeout = fn(c.connectTimeout)
105-
return nil
106100
}
107101
}
108102

109103
// WithDialer configures the Dialer to use when making a new connection to MongoDB.
110104
func WithDialer(fn func(Dialer) Dialer) ConnectionOption {
111-
return func(c *connectionConfig) error {
105+
return func(c *connectionConfig) {
112106
c.dialer = fn(c.dialer)
113-
return nil
114107
}
115108
}
116109

117110
// WithHandshaker configures the Handshaker that wll be used to initialize newly
118111
// dialed connections.
119112
func WithHandshaker(fn func(Handshaker) Handshaker) ConnectionOption {
120-
return func(c *connectionConfig) error {
113+
return func(c *connectionConfig) {
121114
c.handshaker = fn(c.handshaker)
122-
return nil
123115
}
124116
}
125117

126118
// WithIdleTimeout configures the maximum idle time to allow for a connection.
127119
func WithIdleTimeout(fn func(time.Duration) time.Duration) ConnectionOption {
128-
return func(c *connectionConfig) error {
120+
return func(c *connectionConfig) {
129121
c.idleTimeout = fn(c.idleTimeout)
130-
return nil
131122
}
132123
}
133124

134125
// WithReadTimeout configures the maximum read time for a connection.
135126
func WithReadTimeout(fn func(time.Duration) time.Duration) ConnectionOption {
136-
return func(c *connectionConfig) error {
127+
return func(c *connectionConfig) {
137128
c.readTimeout = fn(c.readTimeout)
138-
return nil
139129
}
140130
}
141131

142132
// WithWriteTimeout configures the maximum write time for a connection.
143133
func WithWriteTimeout(fn func(time.Duration) time.Duration) ConnectionOption {
144-
return func(c *connectionConfig) error {
134+
return func(c *connectionConfig) {
145135
c.writeTimeout = fn(c.writeTimeout)
146-
return nil
147136
}
148137
}
149138

150139
// WithTLSConfig configures the TLS options for a connection.
151140
func WithTLSConfig(fn func(*tls.Config) *tls.Config) ConnectionOption {
152-
return func(c *connectionConfig) error {
141+
return func(c *connectionConfig) {
153142
c.tlsConfig = fn(c.tlsConfig)
154-
return nil
155143
}
156144
}
157145

158146
// WithMonitor configures a event for command monitoring.
159147
func WithMonitor(fn func(*event.CommandMonitor) *event.CommandMonitor) ConnectionOption {
160-
return func(c *connectionConfig) error {
148+
return func(c *connectionConfig) {
161149
c.cmdMonitor = fn(c.cmdMonitor)
162-
return nil
163150
}
164151
}
165152

166153
// WithZlibLevel sets the zLib compression level.
167154
func WithZlibLevel(fn func(*int) *int) ConnectionOption {
168-
return func(c *connectionConfig) error {
155+
return func(c *connectionConfig) {
169156
c.zlibLevel = fn(c.zlibLevel)
170-
return nil
171157
}
172158
}
173159

174160
// WithZstdLevel sets the zstd compression level.
175161
func WithZstdLevel(fn func(*int) *int) ConnectionOption {
176-
return func(c *connectionConfig) error {
162+
return func(c *connectionConfig) {
177163
c.zstdLevel = fn(c.zstdLevel)
178-
return nil
179164
}
180165
}
181166

182167
// WithOCSPCache specifies a cache to use for OCSP verification.
183168
func WithOCSPCache(fn func(ocsp.Cache) ocsp.Cache) ConnectionOption {
184-
return func(c *connectionConfig) error {
169+
return func(c *connectionConfig) {
185170
c.ocspCache = fn(c.ocspCache)
186-
return nil
187171
}
188172
}
189173

190174
// WithDisableOCSPEndpointCheck specifies whether or the driver should perform non-stapled OCSP verification. If set
191175
// to true, the driver will only check stapled responses and will continue the connection without reaching out to
192176
// OCSP responders.
193177
func WithDisableOCSPEndpointCheck(fn func(bool) bool) ConnectionOption {
194-
return func(c *connectionConfig) error {
178+
return func(c *connectionConfig) {
195179
c.disableOCSPEndpointCheck = fn(c.disableOCSPEndpointCheck)
196-
return nil
197180
}
198181
}
199182

200183
// WithConnectionLoadBalanced specifies whether or not the connection is to a server behind a load balancer.
201184
func WithConnectionLoadBalanced(fn func(bool) bool) ConnectionOption {
202-
return func(c *connectionConfig) error {
185+
return func(c *connectionConfig) {
203186
c.loadBalanced = fn(c.loadBalanced)
204-
return nil
205187
}
206188
}
207189

208190
func withGenerationNumberFn(fn func(generationNumberFn) generationNumberFn) ConnectionOption {
209-
return func(c *connectionConfig) error {
191+
return func(c *connectionConfig) {
210192
c.getGenerationFn = fn(c.getGenerationFn)
211-
return nil
212193
}
213194
}

x/mongo/driver/topology/connection_test.go

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,8 @@ var _ driver.Handshaker = &testHandshaker{}
5050
func TestConnection(t *testing.T) {
5151
t.Run("connection", func(t *testing.T) {
5252
t.Run("newConnection", func(t *testing.T) {
53-
t.Run("config error", func(t *testing.T) {
54-
want := errors.New("config error")
55-
_, got := newConnection(address.Address(""), ConnectionOption(func(*connectionConfig) error { return want }))
56-
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
57-
t.Errorf("errors do not match. got %v; want %v", got, want)
58-
}
59-
})
6053
t.Run("no default idle timeout", func(t *testing.T) {
61-
conn, err := newConnection(address.Address(""))
62-
assert.Nil(t, err, "newConnection error: %v", err)
54+
conn := newConnection(address.Address(""))
6355
wantTimeout := time.Duration(0)
6456
assert.Equal(t, wantTimeout, conn.idleTimeout, "expected idle timeout %v, got %v", wantTimeout,
6557
conn.idleTimeout)
@@ -69,13 +61,10 @@ func TestConnection(t *testing.T) {
6961
t.Run("dialer error", func(t *testing.T) {
7062
err := errors.New("dialer error")
7163
var want error = ConnectionError{Wrapped: err, init: true}
72-
conn, got := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
64+
conn := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
7365
return DialerFunc(func(context.Context, string, string) (net.Conn, error) { return nil, err })
7466
}))
75-
if got != nil {
76-
t.Errorf("newConnection shouldn't error. got %v; want nil", got)
77-
}
78-
got = conn.connect(context.Background())
67+
got := conn.connect(context.Background())
7968
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
8069
t.Errorf("errors do not match. got %v; want %v", got, want)
8170
}
@@ -85,7 +74,7 @@ func TestConnection(t *testing.T) {
8574
t.Run("handshaker error", func(t *testing.T) {
8675
err := errors.New("handshaker error")
8776
var want error = ConnectionError{Wrapped: err, init: true}
88-
conn, got := newConnection(address.Address(""),
77+
conn := newConnection(address.Address(""),
8978
WithHandshaker(func(Handshaker) Handshaker {
9079
return &testHandshaker{
9180
finishHandshake: func(context.Context, driver.Connection) error {
@@ -99,10 +88,7 @@ func TestConnection(t *testing.T) {
9988
})
10089
}),
10190
)
102-
if got != nil {
103-
t.Errorf("newConnection shouldn't error. got %v; want nil", got)
104-
}
105-
got = conn.connect(context.Background())
91+
got := conn.connect(context.Background())
10692
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
10793
t.Errorf("errors do not match. got %v; want %v", got, want)
10894
}
@@ -117,7 +103,7 @@ func TestConnection(t *testing.T) {
117103
t.Run("connect succeeds", func(t *testing.T) {
118104
// In the case where connect finishes successfully, it unpins the CancelFunc.
119105

120-
conn, err := newConnection(address.Address(""),
106+
conn := newConnection(address.Address(""),
121107
WithDialer(func(Dialer) Dialer {
122108
return DialerFunc(func(context.Context, string, string) (net.Conn, error) {
123109
return &net.TCPConn{}, nil
@@ -127,9 +113,8 @@ func TestConnection(t *testing.T) {
127113
return &testHandshaker{}
128114
}),
129115
)
130-
assert.Nil(t, err, "newConnection error: %v", err)
131116

132-
err = conn.connect(context.Background())
117+
err := conn.connect(context.Background())
133118
assert.Nil(t, err, "error establishing connection: %v", err)
134119
assert.Nil(t, conn.cancelConnectContext, "cancellation function was not cleared")
135120
})
@@ -140,7 +125,7 @@ func TestConnection(t *testing.T) {
140125
// Create a connection that will block in connect until doneChan is closed. This prevents
141126
// connect from succeeding and unpinning the CancelFunc.
142127
doneChan := make(chan struct{})
143-
conn, err := newConnection(address.Address(""),
128+
conn := newConnection(address.Address(""),
144129
WithDialer(func(Dialer) Dialer {
145130
return DialerFunc(func(context.Context, string, string) (net.Conn, error) {
146131
<-doneChan
@@ -151,7 +136,6 @@ func TestConnection(t *testing.T) {
151136
return &testHandshaker{}
152137
}),
153138
)
154-
assert.Nil(t, err, "newConnection error: %v", err)
155139

156140
// Call connect in a goroutine because it will block.
157141
var wg sync.WaitGroup
@@ -170,8 +154,7 @@ func TestConnection(t *testing.T) {
170154
})
171155
t.Run("tls", func(t *testing.T) {
172156
t.Run("connection source is set to default if unspecified", func(t *testing.T) {
173-
conn, err := newConnection(address.Address(""))
174-
assert.Nil(t, err, "newConnection error: %v", err)
157+
conn := newConnection(address.Address(""))
175158
assert.NotNil(t, conn.config.tlsConnectionSource, "expected tlsConnectionSource to be set but was not")
176159
})
177160
t.Run("server name", func(t *testing.T) {
@@ -208,8 +191,7 @@ func TestConnection(t *testing.T) {
208191
return testTLSConnectionSource
209192
}),
210193
}
211-
conn, err := newConnection(tc.addr, connOpts...)
212-
assert.Nil(t, err, "newConnection error: %v", err)
194+
conn := newConnection(tc.addr, connOpts...)
213195

214196
_ = conn.connect(context.Background())
215197
assert.NotNil(t, sentCfg, "expected TLS config to be set, but was not")
@@ -250,8 +232,7 @@ func TestConnection(t *testing.T) {
250232
return tc.connectTimeout
251233
}),
252234
}
253-
conn, err := newConnection("", connOpts...)
254-
assert.Nil(t, err, "newConnection error: %v", err)
235+
conn := newConnection("", connOpts...)
255236

256237
ctx, cancel := context.WithTimeout(context.Background(), tc.contextTimeout)
257238
defer cancel()
@@ -291,8 +272,7 @@ func TestConnection(t *testing.T) {
291272
return hangingTLSConnectionSource
292273
}),
293274
}
294-
conn, err := newConnection("", connOpts...)
295-
assert.Nil(t, err, "newConnection error: %v", err)
275+
conn := newConnection("", connOpts...)
296276

297277
ctx, cancel := context.WithTimeout(context.Background(), tc.contextTimeout)
298278
defer cancel()
@@ -336,11 +316,10 @@ func TestConnection(t *testing.T) {
336316
return handshaker
337317
}),
338318
}
339-
conn, err := newConnection("", connOpts...)
340-
assert.Nil(t, err, "newConnection error: %v", err)
319+
conn := newConnection("", connOpts...)
341320

342321
bgCtx := context.Background()
343-
err = conn.connect(bgCtx)
322+
err := conn.connect(bgCtx)
344323
assert.Nil(t, err, "connect error: %v", err)
345324

346325
assertNoContextTimeout := func(t *testing.T, ctx context.Context) {
@@ -697,7 +676,7 @@ func TestConnection(t *testing.T) {
697676
})
698677
t.Run("close", func(t *testing.T) {
699678
t.Run("can close a connection that failed handshaking", func(t *testing.T) {
700-
conn, err := newConnection(address.Address(""),
679+
conn := newConnection(address.Address(""),
701680
WithHandshaker(func(Handshaker) Handshaker {
702681
return &testHandshaker{
703682
finishHandshake: func(context.Context, driver.Connection) error {
@@ -711,9 +690,8 @@ func TestConnection(t *testing.T) {
711690
})
712691
}),
713692
)
714-
assert.Nil(t, err, "newConnection error: %v", err)
715693

716-
err = conn.connect(context.Background())
694+
err := conn.connect(context.Background())
717695
assert.NotNil(t, err, "expected handshake error from connect, got nil")
718696
connState := atomic.LoadInt64(&conn.connected)
719697
assert.Equal(t, disconnected, connState, "expected connection state %v, got %v", disconnected, connState)

0 commit comments

Comments
 (0)