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 ¶
- func Every(interval time.Duration, procfunc gp.ProcessFunc) gp.Process
- func EveryGo(interval time.Duration, procfunc gp.ProcessFunc) gp.Process
- func OnSignal(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process
- func OnSignalGo(sig <-chan struct{}, procfunc gp.ProcessFunc) gp.Process
- func Tick(interval time.Duration, procfunc gp.ProcessFunc) gp.Process
- func TickGo(interval time.Duration, procfunc gp.ProcessFunc) gp.Process
- func Ticker(ticker <-chan time.Time, procfunc gp.ProcessFunc) gp.Process
- func TickerGo(ticker <-chan time.Time, procfunc gp.ProcessFunc) gp.Process
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Every ¶
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.
Example ¶
package main
import (
"fmt"
"time"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
periodicproc "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic"
)
func main() {
tock := make(chan struct{})
i := 0
p := periodicproc.Every(time.Second, func(proc goprocess.Process) {
tock <- struct{}{}
fmt.Printf("hello %d\n", i)
i++
})
<-tock
<-tock
<-tock
p.Close()
}
Output: hello 0 hello 1 hello 2
func EveryGo ¶
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!
Example ¶
package main
import (
"fmt"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
periodicproc "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic"
)
func main() {
sig := make(chan struct{})
p := periodicproc.OnSignal(sig, func(proc goprocess.Process) {
fmt.Println("fire!")
})
sig <- struct{}{}
sig <- struct{}{}
sig <- struct{}{}
p.Close()
}
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 ¶
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!
Example ¶
package main
import (
"fmt"
"time"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
periodicproc "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic"
)
func main() {
p := periodicproc.Tick(time.Second, func(proc goprocess.Process) {
fmt.Println("tick")
})
<-time.After(3*time.Second + 500*time.Millisecond)
p.Close()
}
Output: tick tick tick
func TickGo ¶
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!
Example ¶
package main
import (
"fmt"
"time"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
periodicproc "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/periodic"
)
func main() {
// with TickGo, execution is not rate limited,
// there can be many in-flight simultaneously
wait := make(chan struct{})
p := periodicproc.TickGo(time.Second, func(proc goprocess.Process) {
fmt.Println("tick")
<-wait
})
<-time.After(3*time.Second + 500*time.Millisecond)
wait <- struct{}{}
wait <- struct{}{}
wait <- struct{}{}
p.Close() // blocks us until all children are closed.
}
Output: tick tick tick
Types ¶
This section is empty.