Files
nmon/internal/app/background.go

17 lines
460 B
Go

package app
import "context"
// goStop runs run in the background until Stop, which cancels its context and
// waits for it to return. Subsystems stop in reverse start order.
func (a *App) goStop(name string, run func(ctx context.Context)) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { defer close(done); run(ctx) }()
a.onStop(name, func(context.Context) error {
cancel()
<-done
return nil
})
}