mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2025-03-01 12:00:44 +01:00
Graceful disconnect upon context cancel
Make sure the underlying library context cancellation happens only after the session has been shutdown. Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
882cecd6a6
commit
4d0f1f26b0
50
context.go
Normal file
50
context.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2021 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithSignal(ctx context.Context, s ...os.Signal) (context.Context, context.CancelFunc) {
|
||||||
|
sigCtx, cancel := context.WithCancel(ctx)
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, s...)
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-c:
|
||||||
|
log.Printf("Received %s, exiting", s)
|
||||||
|
cancel()
|
||||||
|
case <-ctx.Done():
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
signal.Stop(c)
|
||||||
|
}()
|
||||||
|
return sigCtx, cancel
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithWaitGroup(ctx context.Context, wg *sync.WaitGroup) context.Context {
|
||||||
|
wgCtx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
wg.Wait()
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
return wgCtx
|
||||||
|
}
|
21
irc.go
21
irc.go
@ -92,6 +92,7 @@ type IRCNotifier struct {
|
|||||||
sessionUp bool
|
sessionUp bool
|
||||||
sessionUpSignal chan bool
|
sessionUpSignal chan bool
|
||||||
sessionDownSignal chan bool
|
sessionDownSignal chan bool
|
||||||
|
sessionWg sync.WaitGroup
|
||||||
|
|
||||||
channelReconciler *ChannelReconciler
|
channelReconciler *ChannelReconciler
|
||||||
|
|
||||||
@ -212,19 +213,19 @@ func (n *IRCNotifier) SendAlertMsg(ctx context.Context, alertMsg *AlertMsg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *IRCNotifier) ShutdownPhase() {
|
func (n *IRCNotifier) ShutdownPhase() {
|
||||||
if n.Client.Connected() {
|
if n.sessionUp {
|
||||||
log.Printf("IRC client connected, quitting")
|
log.Printf("IRC client connected, quitting")
|
||||||
n.Client.Quit("see ya")
|
n.Client.Quit("see ya")
|
||||||
|
|
||||||
if n.sessionUp {
|
log.Printf("Wait for IRC disconnect to complete")
|
||||||
log.Printf("Session is up, wait for IRC disconnect to complete")
|
select {
|
||||||
select {
|
case <-n.sessionDownSignal:
|
||||||
case <-n.sessionDownSignal:
|
case <-time.After(n.Client.Config().Timeout):
|
||||||
case <-time.After(n.Client.Config().Timeout):
|
log.Printf("Timeout while waiting for IRC disconnect to complete, stopping anyway")
|
||||||
log.Printf("Timeout while waiting for IRC disconnect to complete, stopping anyway")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
n.sessionWg.Done()
|
||||||
}
|
}
|
||||||
|
log.Printf("IRC shutdown complete")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *IRCNotifier) ConnectedPhase(ctx context.Context) {
|
func (n *IRCNotifier) ConnectedPhase(ctx context.Context) {
|
||||||
@ -233,6 +234,7 @@ func (n *IRCNotifier) ConnectedPhase(ctx context.Context) {
|
|||||||
n.SendAlertMsg(ctx, &alertMsg)
|
n.SendAlertMsg(ctx, &alertMsg)
|
||||||
case <-n.sessionDownSignal:
|
case <-n.sessionDownSignal:
|
||||||
n.sessionUp = false
|
n.sessionUp = false
|
||||||
|
n.sessionWg.Done()
|
||||||
n.channelReconciler.Stop()
|
n.channelReconciler.Stop()
|
||||||
n.Client.Quit("see ya")
|
n.Client.Quit("see ya")
|
||||||
ircConnectedGauge.Set(0)
|
ircConnectedGauge.Set(0)
|
||||||
@ -247,7 +249,7 @@ func (n *IRCNotifier) SetupPhase(ctx context.Context) {
|
|||||||
if ok := n.BackoffCounter.DelayContext(ctx); !ok {
|
if ok := n.BackoffCounter.DelayContext(ctx); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := n.Client.ConnectContext(ctx); err != nil {
|
if err := n.Client.ConnectContext(WithWaitGroup(ctx, &n.sessionWg)); err != nil {
|
||||||
log.Printf("Could not connect to IRC: %s", err)
|
log.Printf("Could not connect to IRC: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -256,6 +258,7 @@ func (n *IRCNotifier) SetupPhase(ctx context.Context) {
|
|||||||
select {
|
select {
|
||||||
case <-n.sessionUpSignal:
|
case <-n.sessionUpSignal:
|
||||||
n.sessionUp = true
|
n.sessionUp = true
|
||||||
|
n.sessionWg.Add(1)
|
||||||
n.MaybeIdentifyNick()
|
n.MaybeIdentifyNick()
|
||||||
n.channelReconciler.Start(ctx)
|
n.channelReconciler.Start(ctx)
|
||||||
ircConnectedGauge.Set(1)
|
ircConnectedGauge.Set(1)
|
||||||
|
@ -582,7 +582,6 @@ func TestStopRunningWhenHalfConnected(t *testing.T) {
|
|||||||
expectedCommands := []string{
|
expectedCommands := []string{
|
||||||
"NICK foo",
|
"NICK foo",
|
||||||
"USER foo 12 * :",
|
"USER foo 12 * :",
|
||||||
"QUIT :see ya",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(expectedCommands, server.Log) {
|
if !reflect.DeepEqual(expectedCommands, server.Log) {
|
||||||
|
19
main.go
19
main.go
@ -18,29 +18,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WithSignal(ctx context.Context, s ...os.Signal) (context.Context, context.CancelFunc) {
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
c := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(c, s...)
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-c:
|
|
||||||
log.Printf("Received %s, exiting", s)
|
|
||||||
cancel()
|
|
||||||
case <-ctx.Done():
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
signal.Stop(c)
|
|
||||||
}()
|
|
||||||
return ctx, cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
configFile := flag.String("config", "", "Config file path.")
|
configFile := flag.String("config", "", "Config file path.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user