Skip to content
Go asynchronous simple function utilities, for managing execution of closures and callbacks
Branch: master
Clone or download
Latest commit 10c5b99 Jun 7, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Create FUNDING.yml Jun 7, 2019
.gitignore
.hound.yml v1.0.0 May 11, 2019
.travis.yml v1.0.0 May 11, 2019
LICENSE.md v1.0.0 May 11, 2019
README.md Update README.md May 14, 2019
benchmark_test.go Add retry method May 14, 2019
doc.go v1.0.0 May 11, 2019
example_test.go Update example_test.go May 14, 2019
go.mod v1.0.0 May 11, 2019
gollback.go Close chanel when job is done May 14, 2019
gollback_test.go Fix cancel Race method with context May 14, 2019

README.md

Vardius - gollback

Build Status Go Report Card codecov license

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.

HOW TO USE

  1. GoDoc

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    	  500000	      2961 ns/op	     668 B/op	       5 allocs/op
BenchmarkAll-4     	 5000000	       273 ns/op	      40 B/op	       1 allocs/op
BenchmarkRetry-4   	200000000	         6.68 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/vardius/gollback	32.418s

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() {
	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
		},
	)
}

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() {
	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
		},
	)
}

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()

	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")
	})

	// Will retry 5 times or will timeout by context (after 5 seconds)
	res, e := g.Retry(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:

LICENSE

You can’t perform that action at this time.