• Stars
    star
    97
  • Rank 336,375 (Top 7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.
Housekeeper

GitHub tag (latest SemVer) Build status codecov Go Report Card GitHub GoDoc

Hunch

Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.

About Hunch

Go have several concurrency patterns, here're some articles:

But nowadays, using the context package is the most powerful pattern.

So base on context, Hunch provides functions that can help you deal with complex asynchronous logics with ease.

Usage

Installation

go get

$ go get -u -v github.com/aaronjan/hunch

go mod (Recommended)

import "github.com/aaronjan/hunch"
$ go mod tidy

Types

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

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

API

All

func All(parentCtx context.Context, execs ...Executable) ([]interface{}, error) 

All returns all the outputs from all Executables, order guaranteed.

Examples
ctx := context.Background()
r, err := hunch.All(
    ctx,
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(300 * time.Millisecond)
        return 1, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(200 * time.Millisecond)
        return 2, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(100 * time.Millisecond)
        return 3, nil
    },
)

fmt.Println(r, err)
// Output:
// [1 2 3] <nil>

Take

func Take(parentCtx context.Context, num int, execs ...Executable) ([]interface{}, error)

Take returns the first num values outputted by the Executables.

Examples
ctx := context.Background()
r, err := hunch.Take(
    ctx,
    // Only need the first 2 values.
    2,
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(300 * time.Millisecond)
        return 1, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(200 * time.Millisecond)
        return 2, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(100 * time.Millisecond)
        return 3, nil
    },
)

fmt.Println(r, err)
// Output:
// [3 2] <nil>

Last

func Last(parentCtx context.Context, num int, execs ...Executable) ([]interface{}, error)

Last returns the last num values outputted by the Executables.

Examples
ctx := context.Background()
r, err := hunch.Last(
    ctx,
    // Only need the last 2 values.
    2,
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(300 * time.Millisecond)
        return 1, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(200 * time.Millisecond)
        return 2, nil
    },
    func(ctx context.Context) (interface{}, error) {
        time.Sleep(100 * time.Millisecond)
        return 3, nil
    },
)

fmt.Println(r, err)
// Output:
// [2 1] <nil>

Waterfall

func Waterfall(parentCtx context.Context, execs ...ExecutableInSequence) (interface{}, error)

Waterfall runs ExecutableInSequences one by one, passing previous result to next Executable as input. When an error occurred, it stop the process then returns the error. When the parent Context canceled, it returns the Err() of it immediately.

Examples
ctx := context.Background()
r, err := hunch.Waterfall(
    ctx,
    func(ctx context.Context, n interface{}) (interface{}, error) {
        return 1, nil
    },
    func(ctx context.Context, n interface{}) (interface{}, error) {
        return n.(int) + 1, nil
    },
    func(ctx context.Context, n interface{}) (interface{}, error) {
        return n.(int) + 1, nil
    },
)

fmt.Println(r, err)
// Output:
// 3 <nil>

Retry

func Retry(parentCtx context.Context, retries int, fn Executable) (interface{}, error)

Retry attempts to get a value from an Executable instead of an Error. It will keeps re-running the Executable when failed no more than retries times. Also, when the parent Context canceled, it returns the Err() of it immediately.

Examples
count := 0
getStuffFromAPI := func() (int, error) {
    if count == 5 {
        return 1, nil
    }
    count++

    return 0, fmt.Errorf("timeout")
}

ctx := context.Background()
r, err := hunch.Retry(
    ctx,
    10,
    func(ctx context.Context) (interface{}, error) {
        rs, err := getStuffFromAPI()

        return rs, err
    },
)

fmt.Println(r, err, count)
// Output:
// 1 <nil> 5

Credits

Heavily inspired by Async and ReactiveX.

Licence

Apache 2.0

More Repositories

1

postcss-font-grabber

A postcss plugin, it grabs remote font files and update your CSS, just like that.
TypeScript
27
star
2

Housekeeper

Powerful, simple Repository-Pattern implementation for Laravel (>=5.1), and it come with tests.
PHP
25
star
3

Kobe

Kobe is a Swagger definition writing tool for PHP, it can be used with framework like Laravel or just native PHP.
PHP
16
star
4

rock-your-head

[Hackathon 2015] 码农累了烦了无聊了怎么办?打开PC摄像头,一起Rock your head!
JavaScript
14
star
5

ArrayTree

A good tool for generate tree structure array from just 2-dimensions array.
PHP
8
star
6

StanchionJS

A simple & fast queue done right. backed by Redis, supports auto-reconnect, TypeScript, Promise and Rxjs.
TypeScript
6
star
7

Feve

[abandoned, use `create-react-app` instead] Elegant & simple & convenient React + Redux + React Router + Babel + Webpack + BrowserSync boilerplate.
JavaScript
5
star
8

Academe

实用的 Data Mapper,使用统一的 API 操作 MySQL、PostgreSQL 和 MongoDB,支持关系、出入库自动转换等功能,不强制设计模式。
PHP
4
star
9

Marksnap

[abandoned] CLI tool for parse markdown(.md) to HTML, PDF.
JavaScript
4
star
10

laravel5-example

This is simple comment-system DEMO project for Laravel 5 & Semantic-UI.
PHP
2
star
11

thinkphp-phpunit-mode

ThinkPHP的模式扩展,为phpunit命令行测试而生。Let ThinkPHP & Phpunit be together.
PHP
2
star
12

Commanding

A simple yet practical command-Line application framework, written in TypeScript, with only 2 dependencies (chalk and lodash).
TypeScript
2
star
13

SourceMap-Scout

Source map as a HTTP service.
TypeScript
2
star
14

Hammer-PHP

Docker PHP image on steroids.
Shell
1
star
15

php-armed

Base on official php-fpm 5.5 docker image, installed bunch of other frequently-used extensions.
Shell
1
star
16

yii2-example

This is a basic example that demonstrate how to write a message board using YII2.
JavaScript
1
star