| 
									
										
										
										
											2024-10-29 11:28:47 -05:00
										 |  |  | package context | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"os/signal" | 
					
						
							|  |  |  | 	"syscall" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 15:20:06 -06:00
										 |  |  | func SelfCancelingContextFromBackground() (context.Context, context.CancelFunc) { | 
					
						
							|  |  |  | 	return SelfCancelingContext(context.Background()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SelfCancelingContext(ctx context.Context) (context.Context, context.CancelFunc) { | 
					
						
							| 
									
										
										
										
											2024-10-29 11:28:47 -05:00
										 |  |  | 	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 | 
					
						
							|  |  |  | } |