1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-04 12:03:41 +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() { func run() {
// Create done and interrupt channels. // Spawn reader loop in another thread.
done := make(chan bool) go readerLoop()
// Create interrupt channel.
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
// Register interrupt channel to receive interrupt messages // Register interrupt channel to receive interrupt messages
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
// Spawn message read loop in another thread. // Wait for interrupt
go func() { <-interrupt
readerLoop() log.Info("Interrupt, closing")
// Reader exited. signal that we are done. if !shClient.IsOpen() {
done <- true log.Info("ship client not connected, exiting...")
}() return
}
// Enter event loop in main thread // Cleanly close the connection by sending a close message.
for { err := shClient.SendCloseMessage()
select { if err != nil {
case <-interrupt: log.WithError(err).Info("failed to send close message to ship server")
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
}
} }
} }