Stop sending messages while disconnected

Make sure the session is up before consuming alert messages.
Also, split main run loop for readability.

Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
Luca Bigliardi 2021-02-20 11:17:32 +01:00
parent 82af7c1f69
commit 2471b866f1
2 changed files with 57 additions and 34 deletions

84
irc.go
View File

@ -238,39 +238,7 @@ func (notifier *IRCNotifier) MaybeSendAlertMsg(alertMsg *AlertMsg) {
ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc() ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc()
} }
func (notifier *IRCNotifier) Run() { func (notifier *IRCNotifier) ShutdownPhase() {
defer notifier.stopWg.Done()
for notifier.ctx.Err() != context.Canceled {
if !notifier.Client.Connected() {
log.Printf("Connecting to IRC %s", notifier.Client.Config().Server)
if ok := notifier.BackoffCounter.DelayContext(notifier.ctx); !ok {
continue
}
if err := notifier.Client.Connect(); err != nil {
log.Printf("Could not connect to IRC: %s", err)
continue
}
log.Printf("Connected to IRC server, waiting to establish session")
}
select {
case alertMsg := <-notifier.AlertMsgs:
notifier.MaybeSendAlertMsg(&alertMsg)
case <-notifier.sessionUpSignal:
notifier.sessionUp = true
notifier.MaybeIdentifyNick()
notifier.JoinChannels()
ircConnectedGauge.Set(1)
case <-notifier.sessionDownSignal:
notifier.sessionUp = false
notifier.CleanupChannels()
notifier.Client.Quit("see ya")
ircConnectedGauge.Set(0)
case <-notifier.ctx.Done():
log.Printf("IRC routine asked to terminate")
}
}
if notifier.Client.Connected() { if notifier.Client.Connected() {
log.Printf("IRC client connected, quitting") log.Printf("IRC client connected, quitting")
notifier.Client.Quit("see ya") notifier.Client.Quit("see ya")
@ -285,3 +253,53 @@ func (notifier *IRCNotifier) Run() {
} }
} }
} }
func (notifier *IRCNotifier) ConnectedPhase() {
select {
case alertMsg := <-notifier.AlertMsgs:
notifier.MaybeSendAlertMsg(&alertMsg)
case <-notifier.sessionDownSignal:
notifier.sessionUp = false
notifier.CleanupChannels()
notifier.Client.Quit("see ya")
ircConnectedGauge.Set(0)
case <-notifier.ctx.Done():
log.Printf("IRC routine asked to terminate")
}
}
func (notifier *IRCNotifier) SetupPhase() {
if !notifier.Client.Connected() {
log.Printf("Connecting to IRC %s", notifier.Client.Config().Server)
if ok := notifier.BackoffCounter.DelayContext(notifier.ctx); !ok {
return
}
if err := notifier.Client.Connect(); err != nil {
log.Printf("Could not connect to IRC: %s", err)
return
}
log.Printf("Connected to IRC server, waiting to establish session")
}
select {
case <-notifier.sessionUpSignal:
notifier.sessionUp = true
notifier.MaybeIdentifyNick()
notifier.JoinChannels()
ircConnectedGauge.Set(1)
case <-notifier.ctx.Done():
log.Printf("IRC routine asked to terminate")
}
}
func (notifier *IRCNotifier) Run() {
defer notifier.stopWg.Done()
for notifier.ctx.Err() != context.Canceled {
if !notifier.sessionUp {
notifier.SetupPhase()
} else {
notifier.ConnectedPhase()
}
}
notifier.ShutdownPhase()
}

View File

@ -513,7 +513,12 @@ func TestSendAlertDisconnected(t *testing.T) {
go notifier.Run() go notifier.Run()
alertMsgs <- AlertMsg{Channel: testChannel, Alert: disconnectedTestMessage} // Alert channels is not consumed while disconnected
select {
case alertMsgs <- AlertMsg{Channel: testChannel, Alert: disconnectedTestMessage}:
t.Error("Alert consumed while disconnected")
default:
}
testStep.Done() testStep.Done()
holdUserStep.Wait() holdUserStep.Wait()