Skip to content

Commit 81279af

Browse files
author
iwysiu
authored
GODRIVER-1642 add unknown server description constructor (#417)
1 parent 8c015ee commit 81279af

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
@@ -306,6 +306,20 @@ func NewServer(addr address.Address, response bsoncore.Document) Server {
306306
return desc
307307
}
308308

309+
// NewDefaultServer creates a new unknown server description with the given address.
310+
func NewDefaultServer(addr address.Address) Server {
311+
return NewServerFromError(addr, nil)
312+
}
313+
314+
// NewServerFromError creates a new unknown server description with the given address and error.
315+
func NewServerFromError(addr address.Address, err error) Server {
316+
return Server{
317+
Addr: addr,
318+
LastError: err,
319+
Kind: Unknown,
320+
}
321+
}
322+
309323
// SetAverageRTT sets the average round trip time for this server description.
310324
func (s Server) SetAverageRTT(rtt time.Duration) Server {
311325
s.AverageRTT = rtt

x/mongo/driver/topology/server.go

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

141141
subscribers: make(map[uint64]chan description.Server),
142142
}
143-
s.desc.Store(description.Server{Addr: addr})
143+
s.desc.Store(description.NewDefaultServer(addr))
144144

145145
callback := func(desc description.Server) { s.updateDescription(desc) }
146146
pc := poolConfig{
@@ -164,7 +164,7 @@ func (s *Server) Connect(updateCallback updateTopologyCallback) error {
164164
if !atomic.CompareAndSwapInt32(&s.connectionstate, disconnected, connected) {
165165
return ErrServerConnected
166166
}
167-
s.desc.Store(description.Server{Addr: s.address})
167+
s.desc.Store(description.NewDefaultServer(s.address))
168168
s.updateTopologyCallback.Store(updateCallback)
169169
go s.update()
170170
s.closewg.Add(1)
@@ -231,10 +231,7 @@ func (s *Server) Connection(ctx context.Context) (driver.Connection, error) {
231231

232232
// Since the only kind of ConnectionError we receive from pool.Get will be an initialization
233233
// error, we should set the description.Server appropriately.
234-
desc := description.Server{
235-
Kind: description.Unknown,
236-
LastError: wrappedConnErr,
237-
}
234+
desc := description.NewServerFromError(s.address, wrappedConnErr)
238235
s.updateDescription(desc)
239236
s.pool.clear()
240237

@@ -299,14 +296,12 @@ func (s *Server) RequestImmediateCheck() {
299296

300297
// ProcessError handles SDAM error handling and implements driver.ErrorProcessor.
301298
func (s *Server) ProcessError(err error) {
299+
desc := s.Description()
302300
// Invalidate server description if not master or node recovering error occurs.
303301
// These errors can be reported as a command error or a write concern error.
304302
if cerr, ok := err.(driver.Error); ok && (cerr.NodeIsRecovering() || cerr.NotMaster()) {
305-
desc := s.Description()
306-
desc.Kind = description.Unknown
307-
desc.LastError = err
308303
// updates description to unknown
309-
s.updateDescription(desc)
304+
s.updateDescription(description.NewServerFromError(s.address, err))
310305
// If the node is shutting down or is older than 4.2, we synchronously clear the pool
311306
if cerr.NodeIsShuttingDown() || desc.WireVersion == nil || desc.WireVersion.Max < 8 {
312307
s.RequestImmediateCheck()
@@ -315,11 +310,8 @@ func (s *Server) ProcessError(err error) {
315310
return
316311
}
317312
if wcerr, ok := err.(driver.WriteConcernError); ok && (wcerr.NodeIsRecovering() || wcerr.NotMaster()) {
318-
desc := s.Description()
319-
desc.Kind = description.Unknown
320-
desc.LastError = err
321313
// updates description to unknown
322-
s.updateDescription(desc)
314+
s.updateDescription(description.NewServerFromError(s.address, err))
323315
// If the node is shutting down or is older than 4.2, we synchronously clear the pool
324316
if wcerr.NodeIsShuttingDown() || desc.WireVersion == nil || desc.WireVersion.Max < 8 {
325317
s.RequestImmediateCheck()
@@ -341,11 +333,8 @@ func (s *Server) ProcessError(err error) {
341333
return
342334
}
343335

344-
desc := s.Description()
345-
desc.Kind = description.Unknown
346-
desc.LastError = err
347336
// updates description to unknown
348-
s.updateDescription(desc)
337+
s.updateDescription(description.NewServerFromError(s.address, err))
349338
s.pool.clear()
350339
}
351340

@@ -547,11 +536,7 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
547536
}
548537

549538
if !set {
550-
desc = description.Server{
551-
Addr: s.address,
552-
LastError: saved,
553-
Kind: description.Unknown,
554-
}
539+
desc = description.NewServerFromError(s.address, saved)
555540
}
556541

557542
return desc, conn

0 commit comments

Comments
 (0)