async.go
func AsyncWithContext(ctx context.Context, check Check, interval time.Duration) Check {
	// create a chan that will buffer the most recent check result
	result := make(chan error, 1)

	// fill it with ErrNoData so we'll start in an initially failing state
	// (we don't want to be ready/live until we've actually executed the check
	// once, but that might be slow).
	result <- ErrNoData

	// make a wrapper that runs the check, and swaps out the current head of
	// the channel with the latest result
	update := func() {
		err := check()
		<-result
		result <- err
	}

	// spawn a background goroutine to run the check
	go func() {
		// call once right away (time.Tick() doesn't always tick immediately
		// but we want an initial result as soon as possible)
		update()

		// loop forever or until the context is canceled
		ticker := time.Tick(interval)
		for {
			select {
			case <-ticker:
				update()
			case <-ctx.Done():
				return
			}
		}
	}()

	// return a Check function that closes over our result and mutex
	return func() error {
		// peek at the head of the channel, then put it back
		err := <-result
		result <- err
		return err
	}
}
func AsyncWithContext(ctx context.Context, check Check, interval time.Duration) Check {
	// create a chan that will buffer the most recent check result
	result := make(chan error, 1)

	// fill it with ErrNoData so we'll start in an initially failing state
	// (we don't want to be ready/live until we've actually executed the check
	// once, but that might be slow).
	result <- ErrNoData

	// make a wrapper that runs the check, and swaps out the current head of
	// the channel with the latest result
	update := func() {
		err := check()
		<-result
		result <- err
	}

	// spawn a background goroutine to run the check
	go func() {
		// call once right away (time.Tick() doesn't always tick immediately
		// but we want an initial result as soon as possible)
		update()

		// loop forever or until the context is canceled
		ticker := time.Tick(interval)
		for {
			select {
			case <-ticker:
				update()
			case <-ctx.Done():
				return
			}
		}
	}()

	// return a Check function that closes over our result and mutex
	return func() error {
		// peek at the head of the channel, then put it back
		err := <-result
		result <- err
		return err
	}
}
func AsyncWithContext ( ctx context . Context , check Check , interval time . Duration ) Check { // create a chan that will buffer the most recent check result result := make ( chan error , 1 ) // fill it with ErrNoData so we'll start in an initially failing state // (we don't want to be ready/live until we've actually executed the check // once, but that might be slow). result <- ErrNoData // make a wrapper that runs the check, and swaps out the current head of // the channel with the latest result update := func ( ) { err := check ( ) <- result result <- err } // spawn a background goroutine to run the check go func ( ) { // call once right away (time.Tick() doesn't always tick immediately // but we want an initial result as soon as possible) update ( ) // loop forever or until the context is canceled ticker := time . Tick ( interval ) for { select { case <- ticker : update ( ) case <- ctx . Done ( ) : return } } } ( ) // return a Check function that closes over our result and mutex return func ( ) error { // peek at the head of the channel, then put it back err := <- result result <- err return err } }
// AsyncWithContext converts a Check into an asynchronous check that runs in a
// background goroutine at a fixed interval. The check is called at a fixed
// rate, not with a fixed delay between invocations. If your check takes longer
// than the interval to execute, the next execution will happen immediately.
//
// Note: if you don't need to cancel execution (because this runs forever), use Async()
AsyncWithContext converts a Check into an asynchronous check that runs in a background goroutine at a fixed interval . The check is called at a fixed rate not with a fixed delay between invocations . If your check takes longer than the interval to execute the next execution will happen immediately . Note : if you don t need to cancel execution ( because this runs forever ) use Async ()