mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
GODT-1033 Retry starting IMAP server after connection was down
This commit is contained in:
@ -117,10 +117,12 @@ func (s *imapServer) ListenAndServe() {
|
|||||||
go s.monitorDisconnectedUsers()
|
go s.monitorDisconnectedUsers()
|
||||||
go s.monitorInternetConnection()
|
go s.monitorInternetConnection()
|
||||||
|
|
||||||
s.listenAndServe()
|
// When starting the Bridge, we don't want to retry to notify user
|
||||||
|
// quickly about the issue. Very probably retry will not help anyway.
|
||||||
|
s.listenAndServe(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *imapServer) listenAndServe() {
|
func (s *imapServer) listenAndServe(retries int) {
|
||||||
if s.isRunning.Load().(bool) {
|
if s.isRunning.Load().(bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -129,8 +131,16 @@ func (s *imapServer) listenAndServe() {
|
|||||||
log.Info("IMAP server listening at ", s.server.Addr)
|
log.Info("IMAP server listening at ", s.server.Addr)
|
||||||
l, err := net.Listen("tcp", s.server.Addr)
|
l, err := net.Listen("tcp", s.server.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
s.isRunning.Store(false)
|
||||||
|
if retries > 0 {
|
||||||
|
log.WithError(err).WithField("retries", retries).Warn("IMAP listener failed")
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
s.listenAndServe(retries - 1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.WithError(err).Error("IMAP listener failed")
|
||||||
s.eventListener.Emit(events.ErrorEvent, "IMAP failed: "+err.Error())
|
s.eventListener.Emit(events.ErrorEvent, "IMAP failed: "+err.Error())
|
||||||
log.Error("IMAP failed: ", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,8 +152,9 @@ func (s *imapServer) listenAndServe() {
|
|||||||
// User shouldn't be notified about error if server shouldn't be running,
|
// User shouldn't be notified about error if server shouldn't be running,
|
||||||
// but it should in case it was not closed by `s.Close()`.
|
// but it should in case it was not closed by `s.Close()`.
|
||||||
if err != nil && s.isRunning.Load().(bool) {
|
if err != nil && s.isRunning.Load().(bool) {
|
||||||
|
s.isRunning.Store(false)
|
||||||
|
log.WithError(err).Error("IMAP server failed")
|
||||||
s.eventListener.Emit(events.ErrorEvent, "IMAP failed: "+err.Error())
|
s.eventListener.Emit(events.ErrorEvent, "IMAP failed: "+err.Error())
|
||||||
log.Error("IMAP failed: ", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer s.server.Close() //nolint[errcheck]
|
defer s.server.Close() //nolint[errcheck]
|
||||||
@ -176,7 +187,11 @@ func (s *imapServer) monitorInternetConnection() {
|
|||||||
case <-on:
|
case <-on:
|
||||||
go func() {
|
go func() {
|
||||||
defer s.panicHandler.HandlePanic()
|
defer s.panicHandler.HandlePanic()
|
||||||
s.listenAndServe()
|
// We had issues on Mac that from time to time something
|
||||||
|
// blocked our port for a bit after we closed IMAP server
|
||||||
|
// due to connection issues.
|
||||||
|
// Restart always helped, so we do retry to not bother user.
|
||||||
|
s.listenAndServe(10)
|
||||||
}()
|
}()
|
||||||
expectedIsPortFree = false
|
expectedIsPortFree = false
|
||||||
case <-off:
|
case <-off:
|
||||||
|
|||||||
Reference in New Issue
Block a user