Skip to content
Snippets Groups Projects
  • ale's avatar
    098c74b3
    Initial refactoring for v2 · 098c74b3
    ale authored
    Includes a merge of radiod and redirectord, updates to more modern Go
    idioms, adoption of the higher level etcd concurrency primitives, and
    a lot of other minor implementation changes.
    
    The refactor is still incomplete: the daemons won't compile and the
    code to start the RPC servers is missing.
    098c74b3
    History
    Initial refactoring for v2
    ale authored
    Includes a merge of radiod and redirectord, updates to more modern Go
    idioms, adoption of the higher level etcd concurrency primitives, and
    a lot of other minor implementation changes.
    
    The refactor is still incomplete: the daemons won't compile and the
    code to start the RPC servers is missing.
eager_ticker.go 557 B
package util

import (
	"context"
	"time"
)

// RunCron invokes fn periodically, until the context is canceled. It
// is a simple wrapper for time.Ticker, but fn is also called
// immediately as soon as the timer starts.
func RunCron(ctx context.Context, d time.Duration, fn func(context.Context)) {
	// Call fn right away, unless the Context is already invalid.
	select {
	case <-ctx.Done():
		return
	default:
	}
	fn(ctx)

	tick := time.NewTicker(d)
	defer tick.Stop()
	for {
		select {
		case <-ctx.Done():
			return
		case <-tick.C:
			fn(ctx)
		}
	}
}