import ( "github.com/cockroachdb/cockroach/pkg/util/stop" ) func main() { stopper := stop.NewStopper() defer stopper.Stop() // Start a long-running goroutine. stopper.RunWorker(func() { for { // Do some work... select { case <-stopper.ShouldStop(): // Stop signal received, exit loop. return default: // No stop signal, continue working. } } }) // ...do some other things... // Signal the stopper to stop all workers. stopper.Stop() }
import ( "github.com/cockroachdb/cockroach/pkg/util/stop" ) func main() { stopper := stop.NewStopper() defer stopper.Stop() // Start a large number of goroutines. for i := 0; i < 100; i++ { stopper.RunWorker(func() { for { // Do some work... select { case <-stopper.ShouldStop(): // Stop signal received, exit loop. return default: // No stop signal, continue working. } } }) } // ...do some other things... // Signal the stopper to stop all workers. stopper.Stop() }Overall, the Stopper package is a useful utility provided by the CockroachDB project for managing concurrent processes and ensuring clean shutdowns. It can be used in a variety of contexts and is available as an open-source package library on GitHub.