stopper := util.NewStopper() defer stopper.Stop() closer := func() { fmt.Println("closing") } stopper.AddCloser(closer)
stopper := util.NewStopper() defer stopper.Stop() task := func(ctx context.Context) { for { select { case <-ctx.Done(): fmt.Println("stopping task") return default: fmt.Println("running task") time.Sleep(time.Second) } } } stopper.RunTask(task)
stopper := util.NewStopper() defer stopper.Stop() for { if stopper.ShouldStop() { fmt.Println("stopping loop") break } fmt.Println("running loop") time.Sleep(time.Second) }In summary, the Stopper package provides a convenient way to manage and coordinate the stopping state of concurrent processes in a Go program. It is part of the CockroachDB library and can be found at github.com.cockroachdb.cockroach.util.