Skip to content

Commit a2601d2

Browse files
author
iwysiu
committed
GODRIVER-1642 add unknown server description constructor (#417)
1 parent d89ba21 commit a2601d2

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

x/mongo/driver/description/server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ func NewServer(addr address.Address, response bsoncore.Document) Server {
285285
return desc
286286
}
287287

288+
// NewDefaultServer creates a new unknown server description with the given address.
289+
func NewDefaultServer(addr address.Address) Server {
290+
return NewServerFromError(addr, nil)
291+
}
292+
293+
// NewServerFromError creates a new unknown server description with the given address and error.
294+
func NewServerFromError(addr address.Address, err error) Server {
295+
return Server{
296+
Addr: addr,
297+
LastError: err,
298+
Kind: Unknown,
299+
}
300+
}
301+
288302
// SetAverageRTT sets the average round trip time for this server description.
289303
func (s Server) SetAverageRTT(rtt time.Duration) Server {
290304
s.AverageRTT = rtt

x/mongo/driver/topology/server.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func NewServer(addr address.Address, opts ...ServerOption) (*Server, error) {
135135

136136
subscribers: make(map[uint64]chan description.Server),
137137
}
138-
s.desc.Store(description.Server{Addr: addr})
138+
s.desc.Store(description.NewDefaultServer(addr))
139139

140140
callback := func(desc description.Server) { s.updateDescription(desc) }
141141
pc := poolConfig{
@@ -159,7 +159,7 @@ func (s *Server) Connect(updateCallback func(description.Server)) error {
159159
if !atomic.CompareAndSwapInt32(&s.connectionstate, disconnected, connected) {
160160
return ErrServerConnected
161161
}
162-
s.desc.Store(description.Server{Addr: s.address})
162+
s.desc.Store(description.NewDefaultServer(s.address))
163163
s.updateTopologyCallback.Store(updateCallback)
164164
go s.update()
165165
s.closewg.Add(1)
@@ -226,10 +226,7 @@ func (s *Server) Connection(ctx context.Context) (driver.Connection, error) {
226226

227227
// Since the only kind of ConnectionError we receive from pool.Get will be an initialization
228228
// error, we should set the description.Server appropriately.
229-
desc := description.Server{
230-
Kind: description.Unknown,
231-
LastError: wrappedConnErr,
232-
}
229+
desc := description.NewServerFromError(s.address, wrappedConnErr)
233230
s.updateDescription(desc)
234231
s.pool.clear()
235232

@@ -294,14 +291,12 @@ func (s *Server) RequestImmediateCheck() {
294291

295292
// ProcessError handles SDAM error handling and implements driver.ErrorProcessor.
296293
func (s *Server) ProcessError(err error) {
294+
desc := s.Description()
297295
// Invalidate server description if not master or node recovering error occurs.
298296
// These errors can be reported as a command error or a write concern error.
299297
if cerr, ok := err.(driver.Error); ok && (cerr.NodeIsRecovering() || cerr.NotMaster()) {
300-
desc := s.Description()
301-
desc.Kind = description.Unknown
302-
desc.LastError = err
303298
// updates description to unknown
304-
s.updateDescription(desc)
299+
s.updateDescription(description.NewServerFromError(s.address, err))
305300
// If the node is shutting down or is older than 4.2, we synchronously clear the pool
306301
if cerr.NodeIsShuttingDown() || desc.WireVersion == nil || desc.WireVersion.Max < 8 {
307302
s.RequestImmediateCheck()
@@ -310,11 +305,8 @@ func (s *Server) ProcessError(err error) {
310305
return
311306
}
312307
if wcerr, ok := err.(driver.WriteConcernError); ok && (wcerr.NodeIsRecovering() || wcerr.NotMaster()) {
313-
desc := s.Description()
314-
desc.Kind = description.Unknown
315-
desc.LastError = err
316308
// updates description to unknown
317-
s.updateDescription(desc)
309+
s.updateDescription(description.NewServerFromError(s.address, err))
318310
// If the node is shutting down or is older than 4.2, we synchronously clear the pool
319311
if wcerr.NodeIsShuttingDown() || desc.WireVersion == nil || desc.WireVersion.Max < 8 {
320312
s.RequestImmediateCheck()
@@ -336,11 +328,8 @@ func (s *Server) ProcessError(err error) {
336328
return
337329
}
338330

339-
desc := s.Description()
340-
desc.Kind = description.Unknown
341-
desc.LastError = err
342331
// updates description to unknown
343-
s.updateDescription(desc)
332+
s.updateDescription(description.NewServerFromError(s.address, err))
344333
s.pool.clear()
345334
}
346335

@@ -541,11 +530,7 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
541530
}
542531

543532
if !set {
544-
desc = description.Server{
545-
Addr: s.address,
546-
LastError: saved,
547-
Kind: description.Unknown,
548-
}
533+
desc = description.NewServerFromError(s.address, saved)
549534
}
550535

551536
return desc, conn

0 commit comments

Comments
 (0)