Skip to content

Commit a26cf64

Browse files
authored
Remove unused ServerOption error. (#888)
1 parent a6626e6 commit a26cf64

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,
@@ -52,159 +52,139 @@ func newServerConfig(opts ...ServerOption) (*serverConfig, error) {
5252
if opt == nil {
5353
continue
5454
}
55-
err := opt(cfg)
56-
if err != nil {
57-
return nil, err
58-
}
55+
opt(cfg)
5956
}
6057

61-
return cfg, nil
58+
return cfg
6259
}
6360

6461
// ServerOption configures a server.
65-
type ServerOption func(*serverConfig) error
62+
type ServerOption func(*serverConfig)
6663

6764
func withMonitoringDisabled(fn func(bool) bool) ServerOption {
68-
return func(cfg *serverConfig) error {
65+
return func(cfg *serverConfig) {
6966
cfg.monitoringDisabled = fn(cfg.monitoringDisabled)
70-
return nil
7167
}
7268
}
7369

7470
// WithConnectionOptions configures the server's connections.
7571
func WithConnectionOptions(fn func(...ConnectionOption) []ConnectionOption) ServerOption {
76-
return func(cfg *serverConfig) error {
72+
return func(cfg *serverConfig) {
7773
cfg.connectionOpts = fn(cfg.connectionOpts...)
78-
return nil
7974
}
8075
}
8176

8277
// WithCompressionOptions configures the server's compressors.
8378
func WithCompressionOptions(fn func(...string) []string) ServerOption {
84-
return func(cfg *serverConfig) error {
79+
return func(cfg *serverConfig) {
8580
cfg.compressionOpts = fn(cfg.compressionOpts...)
86-
return nil
8781
}
8882
}
8983

9084
// WithServerAppName configures the server's application name.
9185
func WithServerAppName(fn func(string) string) ServerOption {
92-
return func(cfg *serverConfig) error {
86+
return func(cfg *serverConfig) {
9387
cfg.appname = fn(cfg.appname)
94-
return nil
9588
}
9689
}
9790

9891
// WithHeartbeatInterval configures a server's heartbeat interval.
9992
func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption {
100-
return func(cfg *serverConfig) error {
93+
return func(cfg *serverConfig) {
10194
cfg.heartbeatInterval = fn(cfg.heartbeatInterval)
102-
return nil
10395
}
10496
}
10597

10698
// WithHeartbeatTimeout configures how long to wait for a heartbeat socket to
10799
// connection.
108100
func WithHeartbeatTimeout(fn func(time.Duration) time.Duration) ServerOption {
109-
return func(cfg *serverConfig) error {
101+
return func(cfg *serverConfig) {
110102
cfg.heartbeatTimeout = fn(cfg.heartbeatTimeout)
111-
return nil
112103
}
113104
}
114105

115106
// WithMaxConnections configures the maximum number of connections to allow for
116107
// a given server. If max is 0, then maximum connection pool size is not limited.
117108
func WithMaxConnections(fn func(uint64) uint64) ServerOption {
118-
return func(cfg *serverConfig) error {
109+
return func(cfg *serverConfig) {
119110
cfg.maxConns = fn(cfg.maxConns)
120-
return nil
121111
}
122112
}
123113

124114
// WithMinConnections configures the minimum number of connections to allow for
125115
// a given server. If min is 0, then there is no lower limit to the number of
126116
// connections.
127117
func WithMinConnections(fn func(uint64) uint64) ServerOption {
128-
return func(cfg *serverConfig) error {
118+
return func(cfg *serverConfig) {
129119
cfg.minConns = fn(cfg.minConns)
130-
return nil
131120
}
132121
}
133122

134123
// WithMaxConnecting configures the maximum number of connections a connection
135124
// pool may establish simultaneously. If maxConnecting is 0, the default value
136125
// of 2 is used.
137126
func WithMaxConnecting(fn func(uint64) uint64) ServerOption {
138-
return func(cfg *serverConfig) error {
127+
return func(cfg *serverConfig) {
139128
cfg.maxConnecting = fn(cfg.maxConnecting)
140-
return nil
141129
}
142130
}
143131

144132
// WithConnectionPoolMaxIdleTime configures the maximum time that a connection can remain idle in the connection pool
145133
// before being removed. If connectionPoolMaxIdleTime is 0, then no idle time is set and connections will not be removed
146134
// because of their age
147135
func WithConnectionPoolMaxIdleTime(fn func(time.Duration) time.Duration) ServerOption {
148-
return func(cfg *serverConfig) error {
136+
return func(cfg *serverConfig) {
149137
cfg.poolMaxIdleTime = fn(cfg.poolMaxIdleTime)
150-
return nil
151138
}
152139
}
153140

154141
// WithConnectionPoolMaintainInterval configures the interval that the background connection pool
155142
// maintenance goroutine runs.
156143
func WithConnectionPoolMaintainInterval(fn func(time.Duration) time.Duration) ServerOption {
157-
return func(cfg *serverConfig) error {
144+
return func(cfg *serverConfig) {
158145
cfg.poolMaintainInterval = fn(cfg.poolMaintainInterval)
159-
return nil
160146
}
161147
}
162148

163149
// WithConnectionPoolMonitor configures the monitor for all connection pool actions
164150
func WithConnectionPoolMonitor(fn func(*event.PoolMonitor) *event.PoolMonitor) ServerOption {
165-
return func(cfg *serverConfig) error {
151+
return func(cfg *serverConfig) {
166152
cfg.poolMonitor = fn(cfg.poolMonitor)
167-
return nil
168153
}
169154
}
170155

171156
// WithServerMonitor configures the monitor for all SDAM events for a server
172157
func WithServerMonitor(fn func(*event.ServerMonitor) *event.ServerMonitor) ServerOption {
173-
return func(cfg *serverConfig) error {
158+
return func(cfg *serverConfig) {
174159
cfg.serverMonitor = fn(cfg.serverMonitor)
175-
return nil
176160
}
177161
}
178162

179163
// WithClock configures the ClusterClock for the server to use.
180164
func WithClock(fn func(clock *session.ClusterClock) *session.ClusterClock) ServerOption {
181-
return func(cfg *serverConfig) error {
165+
return func(cfg *serverConfig) {
182166
cfg.clock = fn(cfg.clock)
183-
return nil
184167
}
185168
}
186169

187170
// WithRegistry configures the registry for the server to use when creating
188171
// cursors.
189172
func WithRegistry(fn func(*bsoncodec.Registry) *bsoncodec.Registry) ServerOption {
190-
return func(cfg *serverConfig) error {
173+
return func(cfg *serverConfig) {
191174
cfg.registry = fn(cfg.registry)
192-
return nil
193175
}
194176
}
195177

196178
// WithServerAPI configures the server API options for the server to use.
197179
func WithServerAPI(fn func(serverAPI *driver.ServerAPIOptions) *driver.ServerAPIOptions) ServerOption {
198-
return func(cfg *serverConfig) error {
180+
return func(cfg *serverConfig) {
199181
cfg.serverAPI = fn(cfg.serverAPI)
200-
return nil
201182
}
202183
}
203184

204185
// WithServerLoadBalanced specifies whether or not the server is behind a load balancer.
205186
func WithServerLoadBalanced(fn func(bool) bool) ServerOption {
206-
return func(cfg *serverConfig) error {
187+
return func(cfg *serverConfig) {
207188
cfg.loadBalanced = fn(cfg.loadBalanced)
208-
return nil
209189
}
210190
}

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)