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:
parent
af0a4d2714
commit
c6a2c49f9a
1 changed files with 15 additions and 37 deletions
36
main.go
36
main.go
|
|
@ -99,25 +99,17 @@ 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()
|
|
||||||
|
|
||||||
// Reader exited. signal that we are done.
|
|
||||||
done <- true
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Enter event loop in main thread
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-interrupt:
|
|
||||||
log.Info("Interrupt, closing")
|
log.Info("Interrupt, closing")
|
||||||
|
|
||||||
if !shClient.IsOpen() {
|
if !shClient.IsOpen() {
|
||||||
|
|
@ -125,25 +117,11 @@ func run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanly close the connection by sending a close message and then
|
// Cleanly close the connection by sending a close message.
|
||||||
// waiting (with timeout) for the server to close the connection.
|
|
||||||
err := shClient.SendCloseMessage()
|
err := shClient.SendCloseMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Info("failed to send close message to ship server")
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue