Example #1
0
// Tick constructs a ticker with interval, and calls the given ProcessFunc every
// time the ticker fires.
// This is sequentially rate limited, only one call will be in-flight at a time.
//
//  p := periodicproc.Tick(time.Second, func(proc goprocess.Process) {
//  	fmt.Println("fire!")
//  })
//
//  <-time.After(3 * time.Second)
//  p.Close()
//
//  // Output:
//  // fire!
//  // fire!
//  // fire!
func Tick(interval time.Duration, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(func(proc gp.Process) {
		ticker := time.NewTicker(interval)
		callOnTicker(ticker.C, procfunc)(proc)
		ticker.Stop()
	})
}
Example #2
0
func ExampleGo() {
	p := goprocess.Go(func(p goprocess.Process) {
		ticker := time.Tick(200 * time.Millisecond)
		for {
			select {
			case <-ticker:
				fmt.Println("tick")
			case <-p.Closing():
				fmt.Println("closing")
				return
			}
		}
	})

	<-time.After(1100 * time.Millisecond)
	p.Close()
	fmt.Println("closed")
	<-time.After(100 * time.Millisecond)

	// Output:
	// tick
	// tick
	// tick
	// tick
	// tick
	// closing
	// closed
}
Example #3
0
// EveryGo calls the given ProcessFunc at periodic intervals. Internally, it uses
// <-time.After(interval)
// This is not rate limited, multiple calls could be in-flight at the same time.
func EveryGo(interval time.Duration, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(func(proc gp.Process) {
		for {
			select {
			case <-time.After(interval):
				proc.Go(procfunc)
			case <-proc.Closing(): // we're told to close
				return
			}
		}
	})
}
Example #4
0
// OnSignalGo calls the given ProcessFunc every time the signal fires.
// This is not rate limited, multiple calls could be in-flight at the same time.
//
//  sig := make(chan struct{})
//  p := periodicproc.OnSignalGo(sig, func(proc goprocess.Process) {
//  	fmt.Println("fire!")
//  	<-time.After(time.Second) // wont block execution
//  })
//
//  sig<- struct{}
//  sig<- struct{}
//  sig<- struct{}
//
//  // Output:
//  // fire!
//  // fire!
//  // fire!
func OnSignalGo(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(func(proc gp.Process) {
		for {
			select {
			case <-sig:
				proc.Go(procfunc)
			case <-proc.Closing(): // we're told to close
				return
			}
		}
	})
}
Example #5
0
// Every calls the given ProcessFunc at periodic intervals. Internally, it uses
// <-time.After(interval), so it will have the behavior of waiting _at least_
// interval in between calls. If you'd prefer the time.Ticker behavior, use
// periodicproc.Tick instead.
// This is sequentially rate limited, only one call will be in-flight at a time.
func Every(interval time.Duration, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(func(proc gp.Process) {
		for {
			select {
			case <-time.After(interval):
				select {
				case <-proc.Go(procfunc).Closed(): // spin it out as a child, and wait till it's done.
				case <-proc.Closing(): // we're told to close
					return
				}
			case <-proc.Closing(): // we're told to close
				return
			}
		}
	})
}
Example #6
0
// OnSignal calls the given ProcessFunc every time the signal fires, and waits for it to exit.
// This is sequentially rate limited, only one call will be in-flight at a time.
//
//  sig := make(chan struct{})
//  p := periodicproc.OnSignal(sig, func(proc goprocess.Process) {
//  	fmt.Println("fire!")
//  	<-time.After(time.Second) // delays sequential execution by 1 second
//  })
//
//  sig<- struct{}
//  sig<- struct{}
//  sig<- struct{}
//
//  // Output:
//  // fire!
//  // fire!
//  // fire!
func OnSignal(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(func(proc gp.Process) {
		for {
			select {
			case <-sig:
				select {
				case <-proc.Go(procfunc).Closed(): // spin it out as a child, and wait till it's done.
				case <-proc.Closing(): // we're told to close
					return
				}
			case <-proc.Closing(): // we're told to close
				return
			}
		}
	})
}
Example #7
0
// TickerGo calls the given ProcessFunc every time the ticker fires.
// This is not rate limited, multiple calls could be in-flight at the same time.
func TickerGo(ticker <-chan time.Time, procfunc gp.ProcessFunc) gp.Process {
	return gp.Go(goCallOnTicker(ticker, procfunc))
}