Documentation
¶
Overview ¶
Package task provides some helper to work with simple routine.
Functions and methods which requires a task accepts raw Task type for best compatibility. Those create task returns Helper instead so it's easier to use.
Index ¶
- func Sleep(ctx context.Context, timeout time.Duration) error
- type CtxMod
- type Func
- type Helper
- func Copy(dst io.Writer, src io.ReadCloser) Helper
- func F(f func(context.Context) error) Helper
- func FromServer(start func() error, stop func()) Helper
- func HTTPServer(s *http.Server, shutdown ...CtxMod) Helper
- func Iter(tasks ...Task) Helper
- func IterF(tasks ...func(context.Context) error) Helper
- func Skip(tasks ...Task) Helper
- func SkipF(tasks ...func(context.Context) error) Helper
- func T(t Task) Helper
- func Wait(tasks ...Task) Helper
- func WaitF(tasks ...func(context.Context) error) Helper
- func (t Helper) Go(ctx context.Context) <-chan error
- func (t Helper) GoWithChan(ctx context.Context, ch chan<- error)
- func (t Helper) HandleErr(f func(error) error) Helper
- func (t Helper) IgnoreErr() Helper
- func (t Helper) Loop() Helper
- func (t Helper) Retry() Helper
- func (t Helper) RetryN(n int) Helper
- func (t Helper) Timed(dur time.Duration) Helper
- func (t Helper) TimedDone(dur time.Duration) Helper
- func (t Helper) TimedDoneF(f func(time.Duration) time.Duration) Helper
- func (t Helper) TimedF(f func(time.Duration) time.Duration) Helper
- func (t Helper) TimedFail(dur time.Duration) Helper
- func (t Helper) TimedFailF(f func(time.Duration) time.Duration) Helper
- func (t Helper) With(modder CtxMod) Helper
- type Task
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CtxMod ¶
CtxMod defines how you modify a context.
func WithTimeout ¶
WithTimeout creates a CtxMod which adds timeout info to a context.
type Helper ¶
type Helper struct{ Task }
Helper provides some useful tools.
func Copy ¶
func Copy(dst io.Writer, src io.ReadCloser) Helper
Copy wraps io.Copy into a cancellable task. Cancelling context will close src.
func FromServer ¶
FromServer creates a task from server, which can be started or stopped. Running the task calls start, and cancelling context calls stop.
func HTTPServer ¶
HTTPServer wraps s into a task so it can shutdown gracefully when canceled.
func Iter ¶
Iter creates a task run tasks with same context and stops at first error.
Example ¶
e := errors.New("err")
a := F(func(_ context.Context) error { fmt.Println("a"); return nil })
b := F(func(_ context.Context) error { fmt.Println("b"); return e })
c := F(func(_ context.Context) error { fmt.Println("c"); return nil })
err := Iter(a, b, c).Run(context.Background())
if err != e {
fmt.Println("unexpected error:", err)
return
}
Output: a b
func Skip ¶
Skip creates a task that runs tasks concurrently, cancel others if any error, and wait them done.
func Wait ¶
Wait creates a task that runs all task concurrently, wait them get done, and return first non-nil error.
func (Helper) GoWithChan ¶
GoWithChan runs t in separated goroutine and sends returned error into ch.
func (Helper) HandleErr ¶
HandleErr creates a task that handles specific error after running t. It will change the error returned by Run. f is called only if t.Run returns an error.
func (Helper) IgnoreErr ¶
IgnoreErr ignores the error returned by t.Run. Context errors are unmodified.
func (Helper) Loop ¶
Loop creates a task that repeatedly runs t with same context until it returns an error.
func (Helper) Retry ¶
Retry creates a task thats repeatedly runs t with same context until it returns nil.
func (Helper) 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
Example ¶
ctx := context.Background()
n := 1
errTask := func(_ context.Context) error {
fmt.Println(n)
n++
return errors.New("")
}
retry := F(errTask).RetryN(2)
retry.Run(ctx)
Output: 1 2 3
func (Helper) 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 ¶
ctx := context.Background()
begin := time.Now()
quickTask := F(func(_ context.Context) 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(ctx)
fmt.Printf("quick returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
slowTask := F(func(_ context.Context) 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(ctx)
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 (Helper) 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 ¶
ctx := context.Background()
begin := time.Now()
doneTask := F(func(_ context.Context) error {
// a task which always success
return nil
}).TimedDone(time.Second)
doneTask.Run(ctx)
fmt.Printf("done returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
failTask := F(func(_ context.Context) error {
return errors.New("a task which always fail")
}).TimedDone(time.Second)
failTask.Run(ctx)
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 (Helper) TimedDoneF ¶
TimedDoneF is like TimedDone, but use function instead.
func (Helper) TimedFail ¶
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 (Helper) TimedFailF ¶
TimedFailF is like TimedFail, but use function instead.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package deptask provides a tool, [Runner], to run tasks in order according to its dependency.
|
Package deptask provides a tool, [Runner], to run tasks in order according to its dependency. |
|
Package future defines [Future], a value which is determined some time in future.
|
Package future defines [Future], a value which is determined some time in future. |
|
Package rated controls rate of a task with [rate.Limiter].
|
Package rated controls rate of a task with [rate.Limiter]. |