github.com/ianic/xnet/aio@v0.0.0-20230924160527-cee7f41ab201/signal/signal.go (about)

     1  package signal
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  )
     9  
    10  func WaitForInterupt() {
    11  	quit := make(chan os.Signal, 1)
    12  	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
    13  	<-quit
    14  }
    15  
    16  // InterruptContext returns context which will be closed on application interrupt
    17  func InterruptContext() context.Context {
    18  	ctx, cancel := context.WithCancel(context.Background())
    19  	go func() {
    20  		WaitForInterupt()
    21  		cancel()
    22  	}()
    23  	return ctx
    24  }