concurrent

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 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 Go added in v0.2.9

func Go(f FuncWithResultMayError, options ...GoOption)

func Merge added in v0.1.2

func Merge(eachFuncs []FuncWithResultMayError, 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 Func added in v0.2.9

type Func func()

type FuncMayError

type FuncMayError func() error

type FuncWithResult added in v0.2.9

type FuncWithResult func() (result interface{})

type FuncWithResultMayError added in v0.2.9

type FuncWithResultMayError func() (result interface{}, err error)

type GoOption added in v0.2.9

type GoOption func(*GoOptions)

func WithErrorHandler added in v0.2.9

func WithErrorHandler(f func(err error)) GoOption

func WithLimiter added in v0.2.9

func WithLimiter(l *Limiter) GoOption

func WithRecoverPanic added in v0.2.9

func WithRecoverPanic(f func(p interface{})) GoOption

nil to not recover

func WithResultHandler added in v0.2.9

func WithResultHandler(f func(result interface{})) GoOption

func WithSkipNilFunc added in v0.2.9

func WithSkipNilFunc(skip bool) GoOption

func WithWaitGroup added in v0.2.9

func WithWaitGroup(wg *sync.WaitGroup) GoOption

func WithWrapper added in v0.2.9

func WithWrapper(w func(f FuncWithResultMayError) FuncWithResultMayError, checkFuncNil bool) GoOption

set checkFuncNil to return nil when f is nil

type GoOptions added in v0.2.9

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

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 a nil *Limiter is a noop Limiter

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