nctask

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2025 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package nctask provides most helpers in task package for "func() error" type

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FixedDur

func FixedDur(dur time.Duration) func(time.Duration) time.Duration

FixedDur creats a func to be used in TimedF, TimedDoneF and TimedFailF, which does not take actual execution time into consideration.

Types

type Task

type Task func() error

Task repeasents a routine that not cancellable.

func Iter

func Iter(tasks ...Task) Task

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

func Wait(tasks ...Task) Task

Wait creates a task that runs all task concurrently, wait them get done, and return first non-nil error.

func (Task) AlterError

func (t Task) AlterError(f func(error) error) Task

AlterError wraps t to run f to alter the error before returning.

func (Task) Cached

func (t Task) Cached() Task

Cached wraps t to cache the result, and reuse it in later call.

func (Task) Defer

func (t Task) Defer(f func()) Task

Defer wraps t to run f after it.

func (Task) Go

func (t Task) Go() <-chan error

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

func (t Task) GoWithChan(ch chan<- error)

GoWithChan runs t in separated goroutine and sends returned error into ch.

func (Task) HandleErr

func (t Task) HandleErr(f func(error) error) Task

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

func (t Task) IgnoreErrs(f func(error) bool) Task

IgnoreErrs ignores errors if f(error) is true.

func (Task) Loop

func (t Task) Loop() Task

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

func (t Task) Once() Task

Once creates a task that can be run only once, further attempt returns task.ErrOnce.

func (Task) OnlyErrs

func (t Task) OnlyErrs(f func(error) bool) Task

OnlyErrs preserves errors if f(error) is true.

func (Task) Post

func (t Task) Post(f func(error)) Task

Post wraps t to run f after it.

func (Task) Pre

func (t Task) Pre(f func()) Task

Pre wraps t to run f before it.

func (Task) Retry

func (t Task) Retry() Task

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

func (t Task) RetryIf(errf func(error) bool) Task

RetryIf is like Retry, but retries only if errf returns true.

Error passed to errf can never be nil.

func (Task) RetryN

func (t Task) RetryN(n int) Task

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

func (t Task) RetryNIf(errf func(error) bool, n int) Task

RetryNIf is like RetryN, but retries only if errf returns true.

Error passed to errf can never be nil.

func (Task) Run

func (t Task) Run() error

Run runs the task, equals to t().

func (Task) Then

func (t Task) Then(next Task) Task

Next creates a task that runs next after t finished successfully.

func (Task) Timed

func (t Task) Timed(dur time.Duration) Task

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

func (t Task) TimedDone(dur time.Duration) Task

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

func (t Task) TimedDoneF(f func(time.Duration) time.Duration) Task

TimedDoneF is like TimedDone, but use function instead.

The function accepts actual execution time, and returns how long it should wait.

func (Task) TimedF

func (t Task) TimedF(f func(time.Duration) time.Duration) Task

TimedF is like Timed, but use function instead.

The function accepts actual execution time, and returns how long it should wait.

func (Task) TimedFail

func (t Task) TimedFail(dur time.Duration) Task

TimedFail is like Timed, but limits only failed run.

If you're looking for rate limiting solution, you should take a look at "rated" subdirectory.

func (Task) TimedFailF

func (t Task) TimedFailF(f func(time.Duration) time.Duration) Task

TimedFailF is like TimedFail, but use function instead.

The function accepts actual execution time, and returns how long it should wait.

Jump to

Keyboard shortcuts

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