func main() { stopper := stop.NewStopper() server := startServer() // Listen for a stop signal and gracefully shut down the server when it's received. <-stopper.ShouldQuiesce() server.Close() stopper.Stop(context.Background()) }
func main() { stopper := stop.NewStopper() worker := startWorker() // Add a custom stopping function to the Stopper that cleans up resources before shutting down. stopper.AddCloser(stop.CloserFn(func() { worker.Stop() })) <-stopper.ShouldQuiesce() stopper.Stop(context.Background()) }This example creates a new `Stopper` instance and starts a worker process. It then adds a custom Closer function to the Stopper, which will be called when the Stopper is stopped. In this case, the Closer function stops the worker process. When the `ShouldQuiesce()` signal is received, the Stopper is stopped, which triggers the Closer function to run. Overall, the Stopper package from CockroachDB's util library provides a flexible way to gracefully shut down processes that rely on background tasks. By using its channel-based signaling and Closer functions, users can customize the shutdown process to fit their specific needs.