periodicproc

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2021 License: MIT, MIT Imports: 2 Imported by: 0

README

goprocess/periodic - periodic process creation

Documentation

Overview

Package periodic is part of github.com/jbenet/goprocess. It provides a simple periodic processor that calls a function periodically based on some options.

For example:

// use a time.Duration
p := periodicproc.Every(time.Second, func(proc goprocess.Process) {
	fmt.Printf("the time is %s and all is well", time.Now())
})

<-time.After(5*time.Second)
p.Close()

// use a time.Time channel (like time.Ticker)
p := periodicproc.Tick(time.Tick(time.Second), func(proc goprocess.Process) {
	fmt.Printf("the time is %s and all is well", time.Now())
})

<-time.After(5*time.Second)
p.Close()

// or arbitrary signals
signal := make(chan struct{})
p := periodicproc.OnSignal(signal, func(proc goprocess.Process) {
	fmt.Printf("the time is %s and all is well", time.Now())
})

signal<- struct{}{}
signal<- struct{}{}
<-time.After(5 * time.Second)
signal<- struct{}{}
p.Close()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Every

func Every(interval time.Duration, procfunc gp.ProcessFunc) gp.Process

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 EveryGo

func EveryGo(interval time.Duration, procfunc gp.ProcessFunc) gp.Process

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 OnSignal

func OnSignal(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process

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 OnSignalGo

func OnSignalGo(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process

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 Tick

func Tick(interval time.Duration, procfunc gp.ProcessFunc) gp.Process

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 TickGo

func TickGo(interval time.Duration, procfunc gp.ProcessFunc) gp.Process

TickGo constructs a ticker with interval, and calls the given ProcessFunc every time the ticker fires. This is not rate limited, multiple calls could be in-flight at the same time.

p := periodicproc.TickGo(time.Second, func(proc goprocess.Process) {
	fmt.Println("fire!")
	<-time.After(10 * time.Second) // will not block sequential execution
})

<-time.After(3 * time.Second)
p.Close()

// Output:
// fire!
// fire!
// fire!

func Ticker

func Ticker(ticker <-chan time.Time, procfunc gp.ProcessFunc) gp.Process

Ticker calls the given ProcessFunc every time the ticker fires. This is sequentially rate limited, only one call will be in-flight at a time.

func TickerGo

func TickerGo(ticker <-chan time.Time, procfunc gp.ProcessFunc) gp.Process

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL