21 lines
		
	
	
	
		
			343 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			21 lines
		
	
	
	
		
			343 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package context
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"os"
 | |
| 	"os/signal"
 | |
| 	"syscall"
 | |
| )
 | |
| 
 | |
| func SelfCancelingCotext(ctx context.Context) (context.Context, context.CancelFunc) {
 | |
| 	c, done := context.WithCancel(ctx)
 | |
| 	ch := make(chan os.Signal, 1)
 | |
| 	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
 | |
| 	go func() {
 | |
| 		for range ch {
 | |
| 			done()
 | |
| 		}
 | |
| 	}()
 | |
| 
 | |
| 	return c, done
 | |
| }
 |