Documentation
¶
Overview ¶
Package nctask provides most helpers in task package for "func() error" type
Index ¶
- func FixedDur(dur time.Duration) func(time.Duration) time.Duration
- type Task
- func (t Task) AlterError(f func(error) error) Task
- func (t Task) Cached() Task
- func (t Task) Defer(f func()) Task
- func (t Task) Go() <-chan error
- func (t Task) GoWithChan(ch chan<- error)
- func (t Task) HandleErr(f func(error) error) Task
- func (t Task) IgnoreErrs(f func(error) bool) Task
- func (t Task) Loop() Task
- func (t Task) NoErr() func()
- func (t Task) Once() Task
- func (t Task) OnlyErrs(f func(error) bool) Task
- func (t Task) Post(f func(error)) Task
- func (t Task) Pre(f func()) Task
- func (t Task) Retry() Task
- func (t Task) RetryIf(errf func(error) bool) Task
- func (t Task) RetryN(n int) Task
- func (t Task) RetryNIf(errf func(error) bool, n int) Task
- func (t Task) Run() error
- func (t Task) Then(next Task) Task
- func (t Task) Timed(dur time.Duration) Task
- func (t Task) TimedDone(dur time.Duration) Task
- func (t Task) TimedDoneF(f func(time.Duration) time.Duration) Task
- func (t Task) TimedF(f func(time.Duration) time.Duration) Task
- func (t Task) TimedFail(dur time.Duration) Task
- func (t Task) TimedFailF(f func(time.Duration) time.Duration) Task
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Task ¶
type Task func() error
Task repeasents a routine that not cancellable.
func Iter ¶
Iter creates a task run tasks with same context and stops at first error.
Example ¶
e := errors.New("err")
a := func() error { fmt.Println("a"); return nil }
b := func() error { fmt.Println("b"); return e }
c := func() error { fmt.Println("c"); return nil }
err := Iter(a, b, c).Run()
if err != e {
fmt.Println("unexpected error:", err)
return
}
Output: a b
func Wait ¶
Wait creates a task that runs all task concurrently, wait them get done, and return first non-nil error.
func (Task) AlterError ¶
AlterError wraps t to run f to alter the error before returning.
func (Task) Go ¶
Go runs t in separated goroutine and returns a channel to retrieve error.
It's safe to ignore the channel if you don't need the result.
func (Task) GoWithChan ¶
GoWithChan runs t in separated goroutine and sends returned error into ch.
func (Task) HandleErr ¶
HandleErr creates a task that handles specific error after running t. It could change the error returned by Run. f is called only if t.Run returns an error.
func (Task) IgnoreErrs ¶
IgnoreErrs ignores errors if f(error) is true.
func (Task) Loop ¶
Loop creates a task that repeatedly runs t with same context until it returns an error.
func (Task) NoErr ¶
func (t Task) NoErr() func()
NoErr converts the task into a simple function that ignores returned error.
func (Task) Once ¶
Once creates a task that can be run only once, further attempt returns task.ErrOnce.
func (Task) Retry ¶
Retry creates a task thats repeatedly runs t with same context until it returns nil.
Retrying [Micro] task is resource-wasting as it never fail.
func (Task) RetryIf ¶
RetryIf is like Retry, but retries only if errf returns true.
Error passed to errf can never be nil.
func (Task) RetryN ¶
RetryN is like Retry, but retries no more than n times.
In other words, RetryN(2) will run at most 3 times:
- first try
- first retry
- second retry
Retrying [Micro] task is resource-wasting as it never fail.
Example ¶
n := 1
errTask := func() error {
fmt.Println(n)
n++
return errors.New("")
}
retry := Task(errTask).RetryN(2)
retry.Run()
Output: 1 2 3
func (Task) RetryNIf ¶
RetryNIf is like RetryN, but retries only if errf returns true.
Error passed to errf can never be nil.
func (Task) Timed ¶
Timed wraps t into a task ensures that it is not returned before dur passed.
It focuses on "How long I should wait before returning". Take a look at example for how it works.
If you're looking for rate limiting solution, you should take a look at "rated" subdirectory.
Example ¶
begin := time.Now()
quickTask := Task(func() error {
// simulates a quick task like computing 1+1
fmt.Printf("quick done at +%d socond\n", time.Since(begin)/time.Second)
return nil
}).Timed(time.Second)
quickTask.Run()
fmt.Printf("quick returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
slowTask := Task(func() error {
// simulates a slow task like calling web api
time.Sleep(2 * time.Second)
fmt.Printf("slow done at +%d socond\n", time.Since(begin)/time.Second)
return nil
}).Timed(time.Second)
slowTask.Run()
fmt.Printf("slow returns at +%d second\n", time.Since(begin)/time.Second)
Output: quick done at +0 socond quick returns at +1 second slow done at +2 socond slow returns at +2 second
func (Task) TimedDone ¶
TimedDone is like Timed, but limits only successful run.
If you're looking for rate limiting solution, you should take a look at "rated" subdirectory.
Example ¶
begin := time.Now()
doneTask := Task(func() error {
// a task which always success
return nil
}).TimedDone(time.Second)
doneTask.Run()
fmt.Printf("done returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
failTask := Task(func() error {
return errors.New("a task which always fail")
}).TimedDone(time.Second)
failTask.Run()
fmt.Printf("fail returns at +%d second\n", time.Since(begin)/time.Second)
Output: done returns at +1 second fail returns at +0 second
func (Task) TimedDoneF ¶
TimedDoneF is like TimedDone, but use function instead.
The function accepts actual execution time, and returns how long it should wait.
func (Task) TimedF ¶
TimedF is like Timed, but use function instead.
The function accepts actual execution time, and returns how long it should wait.