flow

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 5 Imported by: 1

README

Flow

🌊Flow is an easy-used concurrent calculation model to handle CPU-intensive jobs in Go.

Flow can easily limit concurrent job count and can be very straight-forward to use.

Flow use ants to reuse goroutine.

Basically, flow is a 2D matrix and it run jobs row-by-row and in same row, the jobs will run concurrently.

Usage

Minimal Go Version is 1.18

package main

import (
	"fmt"

	"github.com/BouncyElf/flow"
)

func main() {
	f, counter := flow.New(), 0
	// declare a job
	showLevel := func() {
		counter++
		fmt.Println("level", counter)
	}
	// Next starts a new level, and put the job `showLevel` in it
	f.Next(showLevel)
	for i := 0; i < 20; i++ {
		v := i
		// With add job in this level
		f.With(func() {
			// do some stuff
			fmt.Println(v)
		})
	}
	// start a new level
	f.Next(showLevel)
	for i := 0; i < 20; i++ {
		v := i
		f.With(func() {
			// do some stuff
			fmt.Println(v)
		})
	}
	// limit the number of concurrent jobs
	f.Limit(10)
	// wait and execute job
	f.Run()
}

Doc

GoDoc

LICENSE

MIT LICENSE Copyright (c) 2025 BouncyElf

Documentation

Overview

Example
f, counter := New(), 0
showLevel := func() {
	counter++
	fmt.Println("level", counter)
}
// Next starts a new level, and put the job `showLevel` in it
f.Next(showLevel)
for i := 0; i < 20; i++ {
	v := i
	// With add func in this level
	f.With(func() {
		// do some stuff
		fmt.Println(v)
	})
}
// start a new level
f.Next(showLevel)
for i := 0; i < 20; i++ {
	v := i
	f.With(func() {
		// do some stuff
		fmt.Println(v)
	})
}
// limit the number of concurrent jobs
f.SetLimit(10)
// wait and add counter
f.Run()
Output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
level 1
level 2

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidRunner = errors.New("invalid runner")
)
View Source
var Silent = false

Silent disable all error message

Functions

func SetGlobalPoolSize

func SetGlobalPoolSize(size int) error

SetGlobalPoolSize sets global ants pool size (must be called before any task runs)

Types

type ErrorHandler

type ErrorHandler func(error)

type Flow

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

Flow is a sync model

func New

func New() *Flow

New returns a flow instance

func NewWithLimit

func NewWithLimit(limit int) *Flow

func NewWithPool

func NewWithPool(p GroutinePool) *Flow

func (*Flow) Next

func (f *Flow) Next(jobs ...func()) *Flow

Next add funcs in next level Next: wait level1(run f1, run f2, run f3...) ... wait level2(...)... (in order)

func (*Flow) OnPanic

func (f *Flow) OnPanic(panicHandler func(interface{})) *Flow

OnPanic set panicHandler

func (*Flow) Run

func (f *Flow) Run()

Run execute these funcs

func (*Flow) SetErrorHandler

func (f *Flow) SetErrorHandler(h ErrorHandler) *Flow

func (*Flow) SetGroutinePool

func (f *Flow) SetGroutinePool(p GroutinePool) *Flow

func (*Flow) SetLimit

func (f *Flow) SetLimit(limit int) *Flow

SetLimit set the max concurrent goroutines number

func (*Flow) With

func (f *Flow) With(jobs ...func()) *Flow

With add funcs in this level With: run f1, run f2, run f3 ... (random execute order)

type GroutinePool

type GroutinePool interface {
	Submit(func()) error
}

type PanicHandler

type PanicHandler func(interface{})

type Runner

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

func NewRunner

func NewRunner(p GroutinePool, ro ...RunnerOptions) *Runner

func NewRunnerWithAntsPool

func NewRunnerWithAntsPool(size int, ro ...RunnerOptions) (*Runner, error)

func (*Runner) Add

func (r *Runner) Add(f func()) error

func (*Runner) Wait

func (r *Runner) Wait()

type RunnerOptions

type RunnerOptions func(*Runner)

func WithPanicHandler

func WithPanicHandler(ph PanicHandler) RunnerOptions

Jump to

Keyboard shortcuts

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