gollback

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: MIT Imports: 2 Imported by: 4

README

⚙️ gollback

Build Status Go Report Card codecov license

logo

gollback - Go asynchronous simple function utilities, for managing execution of closures and callbacks

📖 ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

📚 Documentation

For examples visit godoc#pkg-examples

For GoDoc reference, visit pkg.go.dev

🚏 HOW TO USE

🚅 Benchmark

CPU: 3,3 GHz Intel Core i7

RAM: 16 GB 2133 MHz LPDDR3

➜  gollback git:(master) ✗ go test -bench=. -cpu=4 -benchmem
goos: darwin
goarch: amd64
pkg: github.com/vardius/gollback
BenchmarkRace-4    	  566022	      2608 ns/op	     663 B/op	       5 allocs/op
BenchmarkAll-4     	 5052489	       241 ns/op	      42 B/op	       1 allocs/op
BenchmarkRetry-4   	206430384	         5.93 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/vardius/gollback	31.741s

Race

Race method returns a response as soon as one of the callbacks in an iterable resolves with the value that is not an error, otherwise last error is returne

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

func main() {
	r, err := gollback.Race(
		context.Background(),
		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
		},
	)
}

All

All method returns when all of the callbacks passed as an iterable have finished, returned responses and errors are ordered according to callback order

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

func main() {
	rs, errs := gollback.All(
		context.Background(),
		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
		},
	)
}

Retry

Retry method retries callback given amount of times until it executes without an error, when retries = 0 it will retry infinitely

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

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

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

	// Will retry 5 times or will timeout by context (after 5 seconds)
	res, err := gollback.Retry(ctx, 5, func(ctx context.Context) (interface{}, error) {
		return nil, errors.New("failed")
	})
}

📜 License

This package is released under the MIT license. See the complete license in the package

Documentation

Overview

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

Example (All)
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

func main() {
	rs, errs := gollback.All(
		context.Background(),
		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>]
Example (Race)
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

func main() {
	r, err := gollback.Race(
		context.Background(),
		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>
Example (RetryFiveTimes)
package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/vardius/gollback"
)

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

	fmt.Println(res)
	fmt.Println(err)
}
Output:

<nil>
failed
Example (RetryTimeout)
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vardius/gollback"
)

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

	// Will retry infinitely until timeouts by context (after 5 seconds)
	res, err := gollback.Retry(ctx, 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v1.1.0

func All(ctx context.Context, 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 will panic if context is nil

func Race added in v1.1.0

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

Race method returns a response as soon as one of the callbacks in an iterable executes without an error, otherwise last error is returned will panic if context is nil

func Retry added in v1.1.0

func Retry(ctx context.Context, retires int, fn AsyncFunc) (interface{}, error)

Retry method retries callback given amount of times until it executes without an error, when retries = 0 it will retry infinitely will panic if context is nil

Types

type AsyncFunc

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

AsyncFunc represents asynchronous function

Source Files

Jump to

Keyboard shortcuts

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