concurrent

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 5 Imported by: 0

README

Package concurrent

run function concurrently.

Usage

see unit test files for more examples.

package main

import (
	"errors"
	"log"
	"time"

	"github.com/QisFj/godry/concurrent"
)

func main() {
	log.Println("with error:")
	withErr()
	log.Println("no error:")
	noErr()
}

func withErr() {
	if err := concurrent.Do(
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-1")
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-1")
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-2")
		},
	); err != nil {
		log.Printf("err: %s\n", err)
	} else {
		log.Println("no error")
	}
}

func noErr() {
	if err := concurrent.Do(
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
	); err != nil {
		log.Printf("err: %s\n", err)
	} else {
		log.Println("no error")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(functions ...FuncMayError) error

func Foreach

func Foreach(slice interface{}, f func(i int, v interface{}) error) error

func Merge added in v0.1.2

func Merge(eachFuncs []EachFunc, merge MergeFunc) error

func MergeForeach added in v0.1.2

func MergeForeach(slice interface{}, f func(i int, v interface{}) (interface{}, error), merge MergeFunc) error

Types

type EachFunc added in v0.1.2

type EachFunc func() (interface{}, error)

type FuncMayError

type FuncMayError func() error

type Limiter added in v0.1.2

type Limiter struct {
	// contains filtered or unexported fields
}

wrap sync.Cond on a uint, use to limit concurrent

zero value is not read to use, use NewLimiter to create

func NewLimiter added in v0.1.2

func NewLimiter(n int) *Limiter

func (*Limiter) Acquire added in v0.1.2

func (l *Limiter) Acquire()

func (*Limiter) Release added in v0.1.2

func (l *Limiter) Release()

type MergeFunc added in v0.1.2

type MergeFunc func(i int, v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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