示例#1
0
文件: periodic.go 项目: rht/ipget
// 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()
	})
}
示例#2
0
文件: periodic.go 项目: rht/ipget
// 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
			}
		}
	})
}
示例#3
0
文件: periodic.go 项目: rht/ipget
// 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
			}
		}
	})
}
示例#4
0
文件: periodic.go 项目: rht/ipget
// 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
			}
		}
	})
}
示例#5
0
文件: periodic.go 项目: rht/ipget
// 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
			}
		}
	})
}
示例#6
0
文件: periodic.go 项目: rht/ipget
// 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))
}