Turning off IMAP server while no connection

This commit is contained in:
Michal Horejsek
2020-12-21 10:08:03 +01:00
parent a468ce635c
commit 5117672388
4 changed files with 55 additions and 2 deletions

View File

@ -156,7 +156,14 @@ func New( // nolint[funlen]
return nil, err
}
cm := pmapi.NewClientManager(pmapi.GetAPIConfig(configName, constants.Version))
apiConfig := pmapi.GetAPIConfig(configName, constants.Version)
apiConfig.NoConnectionHandler = func() {
eventListener.Emit(events.InternetOffEvent, "")
}
apiConfig.ConnectionHandler = func() {
eventListener.Emit(events.InternetOnEvent, "")
}
cm := pmapi.NewClientManager(apiConfig)
cm.SetRoundTripper(pmapi.GetRoundTripper(cm, listener))
cm.SetCookieJar(jar)
sentryReporter.SetUserAgentProvider(cm)

View File

@ -47,6 +47,8 @@ type imapServer struct {
eventListener listener.Listener
debugClient bool
debugServer bool
on bool
}
// NewIMAPServer constructs a new IMAP server configured with the given options.
@ -106,6 +108,19 @@ func NewIMAPServer(debugClient, debugServer bool, port int, tls *tls.Config, ima
// Starts the server.
func (s *imapServer) ListenAndServe() {
go s.monitorDisconnectedUsers()
go s.monitorInternetConnection()
s.listenAndServe()
}
func (s *imapServer) listenAndServe() {
if s.on {
return
}
s.on = true
defer func() {
s.on = false
}()
log.Info("IMAP server listening at ", s.server.Addr)
l, err := net.Listen("tcp", s.server.Addr)
@ -136,6 +151,24 @@ func (s *imapServer) Close() {
}
}
func (s *imapServer) monitorInternetConnection() {
on := make(chan string)
s.eventListener.Add(events.InternetOnEvent, on)
off := make(chan string)
s.eventListener.Add(events.InternetOffEvent, off)
go func() {
for range on {
s.listenAndServe()
}
}()
go func() {
for range off {
s.Close()
}
}()
}
func (s *imapServer) monitorDisconnectedUsers() {
ch := make(chan string)
s.eventListener.Add(events.CloseConnectionEvent, ch)