Ska1n

Untitled

Jan 13th, 2021
856
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "context"
  5.     "fmt"
  6.     "os"
  7.     "os/signal"
  8.     "syscall"
  9.     "time"
  10. )
  11.  
  12. var ctxBuffOne context.Context
  13. var cancelBuffOne context.CancelFunc
  14.  
  15. var ctxBuffTwo context.Context
  16. var cancelBuffTwo context.CancelFunc
  17.  
  18. var bufferOneOpen = true
  19. var bufferTwoOpen = true
  20.  
  21. func main() {
  22.     fmt.Printf("Stat application. PID: %d\n", os.Getpid())
  23.  
  24.     ctxBuffOne, cancelBuffOne = context.WithCancel(context.Background())
  25.     ctxBuffTwo, cancelBuffTwo = context.WithCancel(context.Background())
  26.  
  27.     go handleSignal()
  28.     go bufferOne()
  29.     go bufferTwo()
  30.  
  31.     for {
  32.         if !bufferOneOpen && !bufferTwoOpen {
  33.             fmt.Println("Finish application")
  34.             return
  35.         }
  36.     }
  37. }
  38.  
  39. func bufferOne() {
  40.     defer func() {
  41.         //time.Sleep(1 * time.Second)
  42.         fmt.Println("bufferOne defer run")
  43.     }()
  44.  
  45.     for {
  46.         select {
  47.         case <-ctxBuffOne.Done():
  48.             //time.Sleep(3 * time.Second)
  49.             fmt.Println("bufferOne: Context stop!!!")
  50.             bufferOneOpen = false
  51.             return
  52.         default:
  53.             fmt.Printf("Run bufferOne. PID: %d\n", os.Getpid())
  54.             time.Sleep(1 * time.Second)
  55.         }
  56.     }
  57. }
  58.  
  59. func bufferTwo() {
  60.     defer func() {
  61.         fmt.Println("bufferTwo defer run")
  62.     }()
  63.  
  64.     for {
  65.         select {
  66.         case <-ctxBuffTwo.Done():
  67.             fmt.Println("bufferTwo: Context stop!!!")
  68.             bufferTwoOpen = false
  69.             return
  70.         default:
  71.             fmt.Printf("Run bufferTwo. PID: %d\n", os.Getpid())
  72.             time.Sleep(1 * time.Second)
  73.         }
  74.     }
  75. }
  76.  
  77. func handleSignal() {
  78.     sigCh := make(chan os.Signal)
  79.     signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
  80.     for {
  81.         sig := <-sigCh
  82.         switch sig {
  83.         case syscall.SIGHUP:
  84.             stopSignal("1", sigCh)
  85.             return
  86.         case syscall.SIGINT:
  87.             stopSignal("2", sigCh)
  88.             return
  89.         case syscall.SIGQUIT:
  90.             stopSignal("3", sigCh)
  91.             return
  92.         case syscall.SIGTERM:
  93.             stopSignal("15", sigCh)
  94.             return
  95.         }
  96.     }
  97. }
  98.  
  99. func stopSignal(numSignal string, ch chan os.Signal) {
  100.     signal.Stop(ch)
  101.     fmt.Println("Handle signal " + numSignal)
  102.     cancelBuffOne()
  103.     cancelBuffTwo()
  104. }
  105.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×