Skip to content

Commit

Permalink
Recover from persistent "i/o timeout" or "Closed explicitly" pool err…
Browse files Browse the repository at this point in the history
…ors (#69)
  • Loading branch information
bachue authored and domodwyer committed Dec 27, 2017
1 parent 1ac9b5d commit a104bfb
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4800,13 +4800,13 @@ func (s *Session) acquireSocket(slaveOk bool) (*mongoSocket, error) {
s.m.RLock()
// If there is a slave socket reserved and its use is acceptable, take it as long
// as there isn't a master socket which would be preferred by the read preference mode.
if s.slaveSocket != nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
if s.slaveSocket != nil && s.slaveSocket.dead == nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
socket := s.slaveSocket
socket.Acquire()
s.m.RUnlock()
return socket, nil
}
if s.masterSocket != nil {
if s.masterSocket != nil && s.masterSocket.dead == nil {
socket := s.masterSocket
socket.Acquire()
s.m.RUnlock()
Expand All @@ -4820,12 +4820,20 @@ func (s *Session) acquireSocket(slaveOk bool) (*mongoSocket, error) {
defer s.m.Unlock()

if s.slaveSocket != nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
s.slaveSocket.Acquire()
return s.slaveSocket, nil
if s.slaveSocket.dead == nil {
s.slaveSocket.Acquire()
return s.slaveSocket, nil
} else {
s.unsetSocket()
}
}
if s.masterSocket != nil {
s.masterSocket.Acquire()
return s.masterSocket, nil
if s.masterSocket.dead == nil {
s.masterSocket.Acquire()
return s.masterSocket, nil
} else {
s.unsetSocket()
}
}

// Still not good. We need a new socket.
Expand Down Expand Up @@ -4876,9 +4884,11 @@ func (s *Session) setSocket(socket *mongoSocket) {
// unsetSocket releases any slave and/or master sockets reserved.
func (s *Session) unsetSocket() {
if s.masterSocket != nil {
debugf("unset master socket from session %p", s)
s.masterSocket.Release()
}
if s.slaveSocket != nil {
debugf("unset slave socket from session %p", s)
s.slaveSocket.Release()
}
s.masterSocket = nil
Expand Down

0 comments on commit a104bfb

Please sign in to comment.