Learning goroutines and practising with `os.Signal`
This is the small snippet of golang codes that showing how goroutines work perfectly.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func signalHandler(signal os.Signal) {
switch signal {
case syscall.SIGHUP:
fmt.Println("Signal:", signal.String())
case syscall.SIGINT:
fmt.Println("Signal:", signal.String())
case syscall.SIGTERM:
fmt.Println("Signal:", signal.String())
case syscall.SIGQUIT:
fmt.Println("Signal:", signal.String())
default:
fmt.Println("Unhandled/unknown signal")
}
}
func keepDoingStuffs() {
for {
fmt.Printf("Starting job...")
time.Sleep(time.Second * 5)
fmt.Println("completed.")
}
}
func printNumbers() {
for {
time.Sleep(time.Second * 1)
fmt.Printf(".")
}
}
func trapTimeout(done chan bool) {
for {
select {
case <-time.After(time.Second * 20):
fmt.Println("Timeout.")
done <- true
case <-done:
return
}
}
}
func trapSignal(done chan bool) {
sigc := make(chan os.Signal, 1)
defer close(sigc)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT)
for {
select {
case s := <-sigc:
signalHandler(s)
done <- true
case <-done:
return
}
}
}
func main() {
done := make(chan bool)
defer close(done)
go trapSignal(done)
go trapTimeout(done)
go keepDoingStuffs()
go printNumbers()
if <-done {
fmt.Println("Exit gracefully.")
return
}
}
No comments:
Post a Comment