Skip to content

Commit 215b687

Browse files
committed
Remove unused ServerOption error.
1 parent f4a087a commit 215b687

File tree

7 files changed

+44
-88
lines changed

7 files changed

+44
-88
lines changed

x/mongo/driver/topology/CMAP_spec_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ func runCMAPTest(t *testing.T, testFileName string) {
209209
}))
210210
}
211211

212-
s, err := NewServer(address.Address(l.Addr().String()), primitive.NewObjectID(), sOpts...)
213-
testHelpers.RequireNil(t, err, "error creating server: %v", err)
212+
s := NewServer(address.Address(l.Addr().String()), primitive.NewObjectID(), sOpts...)
214213
s.state = serverConnected
215214
testHelpers.RequireNil(t, err, "error connecting connection pool: %v", err)
216215
defer s.pool.close(context.Background())

x/mongo/driver/topology/server.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,8 @@ type updateTopologyCallback func(description.Server) description.Server
130130
// ConnectServer creates a new Server and then initializes it using the
131131
// Connect method.
132132
func ConnectServer(addr address.Address, updateCallback updateTopologyCallback, topologyID primitive.ObjectID, opts ...ServerOption) (*Server, error) {
133-
srvr, err := NewServer(addr, topologyID, opts...)
134-
if err != nil {
135-
return nil, err
136-
}
137-
err = srvr.Connect(updateCallback)
133+
srvr := NewServer(addr, topologyID, opts...)
134+
err := srvr.Connect(updateCallback)
138135
if err != nil {
139136
return nil, err
140137
}
@@ -143,12 +140,8 @@ func ConnectServer(addr address.Address, updateCallback updateTopologyCallback,
143140

144141
// NewServer creates a new server. The mongodb server at the address will be monitored
145142
// on an internal monitoring goroutine.
146-
func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...ServerOption) (*Server, error) {
147-
cfg, err := newServerConfig(opts...)
148-
if err != nil {
149-
return nil, err
150-
}
151-
143+
func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...ServerOption) *Server {
144+
cfg := newServerConfig(opts...)
152145
globalCtx, globalCtxCancel := context.WithCancel(context.Background())
153146
s := &Server{
154147
state: serverDisconnected,
@@ -190,7 +183,7 @@ func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...Serv
190183
s.pool = newPool(pc, connectionOpts...)
191184
s.publishServerOpeningEvent(s.address)
192185

193-
return s, nil
186+
return s
194187
}
195188

196189
// Connect initializes the Server by starting background monitoring goroutines.

x/mongo/driver/topology/server_options.go

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type serverConfig struct {
4040
poolMaintainInterval time.Duration
4141
}
4242

43-
func newServerConfig(opts ...ServerOption) (*serverConfig, error) {
43+
func newServerConfig(opts ...ServerOption) *serverConfig {
4444
cfg := &serverConfig{
4545
heartbeatInterval: 10 * time.Second,
4646
heartbeatTimeout: 10 * time.Second,
@@ -49,159 +49,139 @@ func newServerConfig(opts ...ServerOption) (*serverConfig, error) {
4949
}
5050

5151
for _, opt := range opts {
52-
err := opt(cfg)
53-
if err != nil {
54-
return nil, err
55-
}
52+
opt(cfg)
5653
}
5754

58-
return cfg, nil
55+
return cfg
5956
}
6057

6158
// ServerOption configures a server.
62-
type ServerOption func(*serverConfig) error
59+
type ServerOption func(*serverConfig)
6360

6461
func withMonitoringDisabled(fn func(bool) bool) ServerOption {
65-
return func(cfg *serverConfig) error {
62+
return func(cfg *serverConfig) {
6663
cfg.monitoringDisabled = fn(cfg.monitoringDisabled)
67-
return nil
6864
}
6965
}
7066

7167
// WithConnectionOptions configures the server's connections.
7268
func WithConnectionOptions(fn func(...ConnectionOption) []ConnectionOption) ServerOption {
73-
return func(cfg *serverConfig) error {
69+
return func(cfg *serverConfig) {
7470
cfg.connectionOpts = fn(cfg.connectionOpts...)
75-
return nil
7671
}
7772
}
7873

7974
// WithCompressionOptions configures the server's compressors.
8075
func WithCompressionOptions(fn func(...string) []string) ServerOption {
81-
return func(cfg *serverConfig) error {
76+
return func(cfg *serverConfig) {
8277
cfg.compressionOpts = fn(cfg.compressionOpts...)
83-
return nil
8478
}
8579
}
8680

8781
// WithServerAppName configures the server's application name.
8882
func WithServerAppName(fn func(string) string) ServerOption {
89-
return func(cfg *serverConfig) error {
83+
return func(cfg *serverConfig) {
9084
cfg.appname = fn(cfg.appname)
91-
return nil
9285
}
9386
}
9487

9588
// WithHeartbeatInterval configures a server's heartbeat interval.
9689
func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption {
97-
return func(cfg *serverConfig) error {
90+
return func(cfg *serverConfig) {
9891
cfg.heartbeatInterval = fn(cfg.heartbeatInterval)
99-
return nil
10092
}
10193
}
10294

10395
// WithHeartbeatTimeout configures how long to wait for a heartbeat socket to
10496
// connection.
10597
func WithHeartbeatTimeout(fn func(time.Duration) time.Duration) ServerOption {
106-
return func(cfg *serverConfig) error {
98+
return func(cfg *serverConfig) {
10799
cfg.heartbeatTimeout = fn(cfg.heartbeatTimeout)
108-
return nil
109100
}
110101
}
111102

112103
// WithMaxConnections configures the maximum number of connections to allow for
113104
// a given server. If max is 0, then maximum connection pool size is not limited.
114105
func WithMaxConnections(fn func(uint64) uint64) ServerOption {
115-
return func(cfg *serverConfig) error {
106+
return func(cfg *serverConfig) {
116107
cfg.maxConns = fn(cfg.maxConns)
117-
return nil
118108
}
119109
}
120110

121111
// WithMinConnections configures the minimum number of connections to allow for
122112
// a given server. If min is 0, then there is no lower limit to the number of
123113
// connections.
124114
func WithMinConnections(fn func(uint64) uint64) ServerOption {
125-
return func(cfg *serverConfig) error {
115+
return func(cfg *serverConfig) {
126116
cfg.minConns = fn(cfg.minConns)
127-
return nil
128117
}
129118
}
130119

131120
// WithMaxConnecting configures the maximum number of connections a connection
132121
// pool may establish simultaneously. If maxConnecting is 0, the default value
133122
// of 2 is used.
134123
func WithMaxConnecting(fn func(uint64) uint64) ServerOption {
135-
return func(cfg *serverConfig) error {
124+
return func(cfg *serverConfig) {
136125
cfg.maxConnecting = fn(cfg.maxConnecting)
137-
return nil
138126
}
139127
}
140128

141129
// WithConnectionPoolMaxIdleTime configures the maximum time that a connection can remain idle in the connection pool
142130
// before being removed. If connectionPoolMaxIdleTime is 0, then no idle time is set and connections will not be removed
143131
// because of their age
144132
func WithConnectionPoolMaxIdleTime(fn func(time.Duration) time.Duration) ServerOption {
145-
return func(cfg *serverConfig) error {
133+
return func(cfg *serverConfig) {
146134
cfg.poolMaxIdleTime = fn(cfg.poolMaxIdleTime)
147-
return nil
148135
}
149136
}
150137

151138
// WithConnectionPoolMaintainInterval configures the interval that the background connection pool
152139
// maintenance goroutine runs.
153140
func WithConnectionPoolMaintainInterval(fn func(time.Duration) time.Duration) ServerOption {
154-
return func(cfg *serverConfig) error {
141+
return func(cfg *serverConfig) {
155142
cfg.poolMaintainInterval = fn(cfg.poolMaintainInterval)
156-
return nil
157143
}
158144
}
159145

160146
// WithConnectionPoolMonitor configures the monitor for all connection pool actions
161147
func WithConnectionPoolMonitor(fn func(*event.PoolMonitor) *event.PoolMonitor) ServerOption {
162-
return func(cfg *serverConfig) error {
148+
return func(cfg *serverConfig) {
163149
cfg.poolMonitor = fn(cfg.poolMonitor)
164-
return nil
165150
}
166151
}
167152

168153
// WithServerMonitor configures the monitor for all SDAM events for a server
169154
func WithServerMonitor(fn func(*event.ServerMonitor) *event.ServerMonitor) ServerOption {
170-
return func(cfg *serverConfig) error {
155+
return func(cfg *serverConfig) {
171156
cfg.serverMonitor = fn(cfg.serverMonitor)
172-
return nil
173157
}
174158
}
175159

176160
// WithClock configures the ClusterClock for the server to use.
177161
func WithClock(fn func(clock *session.ClusterClock) *session.ClusterClock) ServerOption {
178-
return func(cfg *serverConfig) error {
162+
return func(cfg *serverConfig) {
179163
cfg.clock = fn(cfg.clock)
180-
return nil
181164
}
182165
}
183166

184167
// WithRegistry configures the registry for the server to use when creating
185168
// cursors.
186169
func WithRegistry(fn func(*bsoncodec.Registry) *bsoncodec.Registry) ServerOption {
187-
return func(cfg *serverConfig) error {
170+
return func(cfg *serverConfig) {
188171
cfg.registry = fn(cfg.registry)
189-
return nil
190172
}
191173
}
192174

193175
// WithServerAPI configures the server API options for the server to use.
194176
func WithServerAPI(fn func(serverAPI *driver.ServerAPIOptions) *driver.ServerAPIOptions) ServerOption {
195-
return func(cfg *serverConfig) error {
177+
return func(cfg *serverConfig) {
196178
cfg.serverAPI = fn(cfg.serverAPI)
197-
return nil
198179
}
199180
}
200181

201182
// WithServerLoadBalanced specifies whether or not the server is behind a load balancer.
202183
func WithServerLoadBalanced(fn func(bool) bool) ServerOption {
203-
return func(cfg *serverConfig) error {
184+
return func(cfg *serverConfig) {
204185
cfg.loadBalanced = fn(cfg.loadBalanced)
205-
return nil
206186
}
207187
}

x/mongo/driver/topology/server_test.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestServerConnectionTimeout(t *testing.T) {
146146
// event monitor to send events to the channel.
147147
eventsCh := make(chan *event.PoolEvent, 100)
148148

149-
server, err := NewServer(
149+
server := NewServer(
150150
address.Address(l.Addr().String()),
151151
primitive.NewObjectID(),
152152
// Create a connection pool event monitor that sends all events to an events channel
@@ -176,7 +176,6 @@ func TestServerConnectionTimeout(t *testing.T) {
176176
// heartbeats from unexpectedly clearing the connection pool.
177177
withMonitoringDisabled(func(bool) bool { return true }),
178178
)
179-
require.NoError(t, err)
180179
require.NoError(t, server.Connect(nil))
181180

182181
// Create a context with the operation timeout if one is specified in the test case.
@@ -246,7 +245,7 @@ func TestServer(t *testing.T) {
246245
for _, tt := range serverTestTable {
247246
t.Run(tt.name, func(t *testing.T) {
248247
var returnConnectionError bool
249-
s, err := NewServer(
248+
s := NewServer(
250249
address.Address("localhost"),
251250
primitive.NewObjectID(),
252251
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
@@ -274,15 +273,14 @@ func TestServer(t *testing.T) {
274273
)
275274
}),
276275
)
277-
require.NoError(t, err)
278276

279277
var desc *description.Server
280278
descript := s.Description()
281279
if tt.hasDesc {
282280
desc = &descript
283281
require.Nil(t, desc.LastError)
284282
}
285-
err = s.pool.ready()
283+
err := s.pool.ready()
286284
require.NoError(t, err, "pool.ready() error")
287285
defer s.pool.close(context.Background())
288286

@@ -463,17 +461,16 @@ func TestServer(t *testing.T) {
463461
_ = nc.Close()
464462
})
465463
d := newdialer(&net.Dialer{})
466-
s, err := NewServer(address.Address(addr.String()),
464+
s := NewServer(address.Address(addr.String()),
467465
primitive.NewObjectID(),
468466
WithConnectionOptions(func(option ...ConnectionOption) []ConnectionOption {
469467
return []ConnectionOption{WithDialer(func(_ Dialer) Dialer { return d })}
470468
}),
471469
WithMaxConnections(func(u uint64) uint64 {
472470
return 1
473471
}))
474-
noerr(t, err)
475472
s.state = serverConnected
476-
err = s.pool.ready()
473+
err := s.pool.ready()
477474
noerr(t, err)
478475
defer s.pool.close(context.Background())
479476

@@ -603,11 +600,9 @@ func TestServer(t *testing.T) {
603600

604601
for _, tc := range testCases {
605602
t.Run(tc.name, func(t *testing.T) {
606-
server, err := NewServer(address.Address("localhost"), primitive.NewObjectID())
607-
assert.Nil(t, err, "NewServer error: %v", err)
608-
603+
server := NewServer(address.Address("localhost"), primitive.NewObjectID())
609604
server.state = serverConnected
610-
err = server.pool.ready()
605+
err := server.pool.ready()
611606
assert.Nil(t, err, "pool.ready() error: %v", err)
612607

613608
originalDesc := description.Server{
@@ -678,13 +673,10 @@ func TestServer(t *testing.T) {
678673
return append(connOpts, dialerOpt)
679674
})
680675

681-
s, err := NewServer(address.Address("localhost:27017"), primitive.NewObjectID(), serverOpt)
682-
if err != nil {
683-
t.Fatalf("error from NewServer: %v", err)
684-
}
676+
s := NewServer(address.Address("localhost:27017"), primitive.NewObjectID(), serverOpt)
685677

686678
// do a heartbeat with a nil connection so a new one will be dialed
687-
_, err = s.check()
679+
_, err := s.check()
688680
assert.Nil(t, err, "check error: %v", err)
689681
assert.NotNil(t, s.conn, "no connection dialed in check")
690682

@@ -745,13 +737,10 @@ func TestServer(t *testing.T) {
745737
WithServerMonitor(func(*event.ServerMonitor) *event.ServerMonitor { return sdam }),
746738
}
747739

748-
s, err := NewServer(address.Address("localhost:27017"), primitive.NewObjectID(), serverOpts...)
749-
if err != nil {
750-
t.Fatalf("error from NewServer: %v", err)
751-
}
740+
s := NewServer(address.Address("localhost:27017"), primitive.NewObjectID(), serverOpts...)
752741

753742
// set up heartbeat connection, which doesn't send events
754-
_, err = s.check()
743+
_, err := s.check()
755744
assert.Nil(t, err, "check error: %v", err)
756745

757746
channelConn := s.conn.nc.(*drivertest.ChannelNetConn)
@@ -806,16 +795,15 @@ func TestServer(t *testing.T) {
806795
t.Run("WithServerAppName", func(t *testing.T) {
807796
name := "test"
808797

809-
s, err := NewServer(address.Address("localhost"),
798+
s := NewServer(address.Address("localhost"),
810799
primitive.NewObjectID(),
811800
WithServerAppName(func(string) string { return name }))
812-
require.Nil(t, err, "error from NewServer: %v", err)
813801
require.Equal(t, name, s.cfg.appname, "expected appname to be: %v, got: %v", name, s.cfg.appname)
814802
})
815803
t.Run("createConnection overwrites WithSocketTimeout", func(t *testing.T) {
816804
socketTimeout := 40 * time.Second
817805

818-
s, err := NewServer(
806+
s := NewServer(
819807
address.Address("localhost"),
820808
primitive.NewObjectID(),
821809
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
@@ -826,7 +814,6 @@ func TestServer(t *testing.T) {
826814
)
827815
}),
828816
)
829-
assert.Nil(t, err, "NewServer error: %v", err)
830817

831818
conn := s.createConnection()
832819
assert.Equal(t, s.cfg.heartbeatTimeout, 10*time.Second, "expected heartbeatTimeout to be: %v, got: %v", 10*time.Second, s.cfg.heartbeatTimeout)

x/mongo/driver/topology/topology.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func (t *Topology) selectServerFromDescription(desc description.Topology,
556556
func (t *Topology) pollSRVRecords() {
557557
defer t.pollingwg.Done()
558558

559-
serverConfig, _ := newServerConfig(t.cfg.serverOpts...)
559+
serverConfig := newServerConfig(t.cfg.serverOpts...)
560560
heartbeatInterval := serverConfig.heartbeatInterval
561561

562562
pollTicker := time.NewTicker(t.rescanSRVInterval)

0 commit comments

Comments
 (0)