Skip to content

Commit 0dc1a63

Browse files
committed
Return the cancel function from tools.ContextWithQuitSignals
I was annoyed at documenting the weirdness of how to use it with context.WithCancel, this way there's no ordering concern and it's all one line. You can safely ignore the cancel func most of the time as well because usually when this context is cancelled the process ends very soon and the memory will be cleaned up that way.
1 parent af1849c commit 0dc1a63

File tree

5 files changed

+10
-24
lines changed

5 files changed

+10
-24
lines changed

assimilate/embedassimilate/assimilate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Run(ctx context.Context, flags *flag.FlagSet, args []string) {
5656
log.Fatal(err)
5757
}
5858
var wg sync.WaitGroup
59-
ctx = tools.ContextWithQuitSignals(context.Background())
59+
ctx, _ = tools.ContextWithQuitSignals(context.Background())
6060
errCount := 0
6161
for i, requestText := range flags.Args() {
6262
registration := &gate.RegisterRequest{}

host/embedhost/host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func Run(ctx context.Context, flags *flag.FlagSet, args []string) {
9292
}
9393

9494
// Setup graceful stopping
95-
ctx = tools.ContextWithQuitSignals(context.Background())
95+
ctx, _ = tools.ContextWithQuitSignals(context.Background())
9696

9797
// Register the reverse proxy pattern with portal.
9898
// Only blocks until the initial registration is done, then keeps renewing.

portal/gate/example_client_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ var pattern = flag.String("pattern", "/hello/", "The path to register with porta
1818
func Example() {
1919
flag.Parse()
2020

21-
// Setup graceful stopping
22-
// - Call stop() to trigger a full server graceful shutdown
23-
// - When ctrl+C or other OS quit signals are sent ctx is cancelled
24-
// - If you start other background tasks, use wg.Add(1) and wg.Done() and
25-
// exit when ctx.Done() is closed to participate in the graceful shutdown.
26-
ctx, stop := context.WithCancelCause(context.Background())
27-
ctx = tools.ContextWithQuitSignals(ctx)
21+
// Create a context for the server that will be cancelled when the process is
22+
// signalled to exit. You can call stop(nil) to gracefully stop the server.
23+
ctx, stop := tools.ContextWithQuitSignals(context.Background())
2824

2925
// Remove the optional URL prefix from the pattern (http.Handle doesn't understand it)
3026
_, path := gate.ParsePattern(*pattern)

spawn/embedspawn/spawn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func Run(ctx context.Context, flagset *flag.FlagSet, args []string) {
182182

183183
quit := make(chan struct{})
184184
tools.CloseOnQuitSignals(quit)
185-
ctx = tools.ContextWithQuitSignals(context.Background())
185+
ctx, _ = tools.ContextWithQuitSignals(context.Background())
186186

187187
children := newChildren(quit)
188188
go children.MonitorDeaths(quit)

tools/signals.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,13 @@ func CloseOnQuitSignals(quit chan struct{}) {
5353
//
5454
// Catches: SIGINT, SIGKILL, SIGTERM, SIGHUP
5555
//
56-
// To also have a global stop function it's cleanest to do:
57-
//
58-
// ctx, stop := context.WithCancel(context.Background())
59-
// ctx = tools.ContextWithQuitSignals(ctx)
60-
// defer stop()
61-
//
62-
// Since this signals the signal handler goroutine to shutdown and close
63-
// channels. However it's not a problem to let the runtime exit without
64-
// signaling this cleanup and do it in one line:
65-
//
66-
// ctx, stop := context.WithCancel(tools.ContextWithQuitSignals(context.Background()))
67-
// defer stop()
68-
func ContextWithQuitSignals(ctx context.Context) context.Context {
56+
// Canceling this context releases resources associated with it, so code should
57+
// call cancel as soon as the operations running in this Context complete.
58+
func ContextWithQuitSignals(ctx context.Context) (context.Context, context.CancelCauseFunc) {
6959
ret, cancel := context.WithCancelCause(ctx)
7060
go func() {
7161
onQuitSignals(ret.Done(), cancel)
7262
cancel(context.Cause(ret))
7363
}()
74-
return ret
64+
return ret, cancel
7565
}

0 commit comments

Comments
 (0)