gollback: github.com/vardius/gollback Index | Examples | Files

package gollback

import "github.com/vardius/gollback"

Package gollback provides asynchronous function simple utilities, for managing execution of closure, callbacks

Code:

g := gollback.New(context.Background())

rs, errs := g.All(
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(3 * time.Second)
        return 1, nil
    },
    func(ctx context.Context) (interface{}, error) {
        return nil, errors.New("failed")
    },
    func(ctx context.Context) (interface{}, error) {
        return 3, nil
    },
)

fmt.Println(rs)
fmt.Println(errs)

Output:

[1 <nil> 3]
[<nil> failed <nil>]

Code:

g := gollback.New(context.Background())

r, err := g.Race(
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(3 * time.Second)
        return 1, nil
    },
    func(ctx context.Context) (interface{}, error) {
        return nil, errors.New("failed")
    },
    func(ctx context.Context) (interface{}, error) {
        return 3, nil
    },
)

fmt.Println(r)
fmt.Println(err)

Output:

3
<nil>

Code:

g := gollback.New(context.Background())

// Will retry 5 times
res, err := g.Retry(5, func(ctx context.Context) (interface{}, error) {
    return nil, errors.New("failed")
})

fmt.Println(res)
fmt.Println(err)

Output:

<nil>
failed

Code:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

g := gollback.New(ctx)

// Will retry infinitely until timeouts by context (after 5 seconds)
res, err := g.Retry(0, func(ctx context.Context) (interface{}, error) {
    return nil, errors.New("failed")
})

fmt.Println(res)
fmt.Println(err)

Output:

<nil>
context deadline exceeded

Index

Examples

Package Files

doc.go gollback.go

type AsyncFunc Uses

type AsyncFunc func(ctx context.Context) (interface{}, error)

AsyncFunc represents asynchronous function

type Gollback Uses

type Gollback interface {
    // Race method returns a response as soon as one of the callbacks in an iterable executes without an error,
    // otherwise last error is returned
    Race(fns ...AsyncFunc) (interface{}, error)
    // All method returns when all of the callbacks passed as an iterable have finished,
    // returned responses and errors are ordered according to callback order
    All(fns ...AsyncFunc) ([]interface{}, []error)
    // Retry method retries callback given amount of times until it executes without an error,
    // when retries = 0 it will retry infinitely
    Retry(retries int, fn AsyncFunc) (interface{}, error)
}

Gollback provides set of utility methods to easily manage asynchronous functions

func New Uses

func New(ctx context.Context) Gollback

New creates new gollback

Package gollback imports 2 packages (graph) and is imported by 2 packages. Updated 2019-08-24. Refresh now. Tools for package owners.