1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-17 04:30:03 +02:00

main.go: simplify go routines and signal handling.

This commit is contained in:
Henrik Hautakoski 2023-01-20 12:02:32 +01:00
parent af0a4d2714
commit c6a2c49f9a

52
main.go
View file

@ -99,50 +99,28 @@ func readerLoop() {
}
func run() {
// Create done and interrupt channels.
done := make(chan bool)
// Spawn reader loop in another thread.
go readerLoop()
// Create interrupt channel.
interrupt := make(chan os.Signal, 1)
// Register interrupt channel to receive interrupt messages
signal.Notify(interrupt, os.Interrupt)
// Spawn message read loop in another thread.
go func() {
readerLoop()
// Wait for interrupt
<-interrupt
log.Info("Interrupt, closing")
// Reader exited. signal that we are done.
done <- true
}()
if !shClient.IsOpen() {
log.Info("ship client not connected, exiting...")
return
}
// Enter event loop in main thread
for {
select {
case <-interrupt:
log.Info("Interrupt, closing")
if !shClient.IsOpen() {
log.Info("ship client not connected, exiting...")
return
}
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := shClient.SendCloseMessage()
if err != nil {
log.WithError(err).Info("failed to send close message to ship server")
}
select {
case <-done:
log.Info("Closed")
case <-time.After(time.Second * 10):
log.Info("Timeout")
}
return
case <-done:
log.Info("Closed")
return
}
// Cleanly close the connection by sending a close message.
err := shClient.SendCloseMessage()
if err != nil {
log.WithError(err).Info("failed to send close message to ship server")
}
}