fix: irc network distinguish inactive and error (#76)

This commit is contained in:
Ludvig Lundgren 2022-01-10 22:05:27 +01:00 committed by GitHub
parent 27f902041b
commit d8c37dde2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 17 deletions

View file

@ -41,6 +41,12 @@ func (h *channelHealth) SetMonitoring() {
h.monitoringSince = time.Now()
}
// resetMonitoring remove monitoring and time
func (h *channelHealth) resetMonitoring() {
h.monitoring = false
h.monitoringSince = time.Time{}
}
type Handler struct {
network *domain.IrcNetwork
filterService filter.Service
@ -241,17 +247,15 @@ func (s *Handler) Run() error {
s.client = client
// set connected since now
s.connectedSince = time.Now()
s.connected = true
s.setConnectionStatus()
// Connect
err = client.RunContext(ctx)
if err != nil {
log.Error().Err(err).Msgf("could not connect to %v", addr)
// set connected false if we loose connection or stop
s.connectedSince = time.Time{}
s.connected = false
// reset connection status on handler and channels
s.resetConnectionStatus()
return err
}
@ -259,6 +263,25 @@ func (s *Handler) Run() error {
return nil
}
func (s *Handler) setConnectionStatus() {
// set connected since now
s.connectedSince = time.Now()
s.connected = true
}
func (s *Handler) resetConnectionStatus() {
// set connected false if we loose connection or stop
s.connectedSince = time.Time{}
s.connected = false
// loop over channelHealth and reset each one
for _, h := range s.channelHealth {
if h != nil {
h.resetMonitoring()
}
}
}
func (s *Handler) GetNetwork() *domain.IrcNetwork {
return s.network
}