git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/utils/context_ticker.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  func ContextTick(ctx context.Context, d time.Duration) <-chan time.Time {
     9  	ticker := time.Tick(d)
    10  	c := make(chan time.Time, 1)
    11  	go func() {
    12  		defer close(c)
    13  		for {
    14  			select {
    15  			case <-ctx.Done():
    16  				return
    17  			case tick := <-ticker:
    18  				c <- tick
    19  			}
    20  		}
    21  	}()
    22  	return c
    23  }