Skip to content
go routine control with context, support: Main, Go, Pool and some useful Executors.如果你不会管理Goroutine的话,用它
Go
Branch: master
Clone or download
liujianping
Latest commit 5b5d591 Aug 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml more test May 27, 2019
LICENSE more test May 27, 2019
README.md support WithPool Option for Main May 31, 2019
code.go job routine updates Apr 4, 2019
code_string.go job routine updates Apr 4, 2019
code_test.go add tests May 23, 2019
context.go support WithPool Option for Main May 31, 2019
context_test.go more test May 27, 2019
count.go more test May 27, 2019
count_test.go more test May 27, 2019
executors.go update for errors version Aug 19, 2019
executors_test.go more test May 27, 2019
go.mod update for errors version Aug 19, 2019
go.sum update for errors version Aug 19, 2019
interruptor.go job routine updates Apr 4, 2019
logger.go job routine updates Apr 4, 2019
main.go
main_test.go support WithPool Option for Main May 31, 2019
pool.go support WithPool Option for Main May 31, 2019
pool_test.go more test May 27, 2019
routine.go more test May 27, 2019
waits.go fix main wait & support prepare and cleanup for main routine May 7, 2019

README.md

routine

GoDoc Go Report Card Build Status Version Coverage Status

go routine control with context, support: Main, Go, Pool and some useful Executors.

Why we need control go routine?

The keyword go will create a go routine for the function, but if you want to control the routine, it need more work to do, like:

  • manage the go routine object
  • signal to the running go routine to stop
  • waiting for signals when go routine died

these works are really boring, that's why we need control go routine.

How to control go routine?

use an Executor interface, which user context.Context to control the go routine. According to the go routines level, you can use the following entry function help you to control the go routine.

  • Main, encapsulates default signal handlers, process level waiting for go routines, and with prepare & cleanup options
  • Go, wrapper for the go. You should use it in the Main scope.
  • Pool, the simplest go routine pool which implement Routine interface

Quick Start

Main function

the Main function encapsulates default signal handlers, process level waiting for go routines, and with prepare & cleanup options

import "github.com/x-mod/routine"

func main() {
	err := routine.Main(routine.ExecutorFunc(func(ctx context.Context) error {
		//TODO your code here
		return nil
	}),
		//options for the Main
		routine.Context(...),
		routine.Arguments(...),
		routine.WithPool(...),	
	)
	//...
}

Go function

the Go function is the wrapper of the golang's keyword go, when you use the Go function, it act the same like keywork go, but with inside context controling.

import "github.com/x-mod/routine"

func main() {
	err := routine.Main(routine.ExecutorFunc(func(ctx context.Context) error {
		//ignore the result error
		routine.Go(ctx, routine.ExecutorFunc(func(ctx context.Context) error {
			//go routine 1 ...
			return nil
		}))

		//get the result error
		err := <-routine.Go(ctx, routine.ExecutorFunc(func(ctx context.Context) error {
			//go routine 2 ...
			return nil
		}))
		return nil
	}))
	//...
}

Pool routines

Use WithPool Option for the Main, then inside Go will automaticlly choose the pool to run the executors

import "github.com/x-mod/routine"

func main() {
	err := routine.Main(routine.ExecutorFunc(func(ctx context.Context) error {	
		//ignore the result error
		routine.Go(ctx, routine.ExecutorFunc(func(ctx context.Context) error {
			//go routine 1 ...
			return nil
		}))

		//get the result error
		err := <-routine.Go(ctx, routine.ExecutorFunc(func(ctx context.Context) error {
			//go routine 2 ...
			return nil
		}))
		return nil
	}), WithPool(routine.NewPool(routine.NumOfRoutines(4))))
	//...
}

Executors

provide some useful executors, like:

  • retry, retry your executor when failed
  • repeat, repeat your executor
  • crontab, schedule your executor
  • guarantee, make sure your executor never panic

and so on.

import "github.com/x-mod/routine"

func main() {
	
	err := routine.Main(routine.ExecutorFunc(func(ctx context.Context) error {	
		//retry
		routine.Go(ctx, routine.Retry(3, routine.ExecutorFunc(func(ctx context.Context) error {
			//go routine 1 ...
			return nil
		})))

		//guarantee
		routine.Go(ctx, routine.Guarantee(3, routine.ExecutorFunc(func(ctx context.Context) error {
			panic("panic")
			return nil
		})))

		//concurrent
		routine.Go(ctx, routine.Concurrent(10, routine.ExecutorFunc(func(ctx context.Context) error {
			return nil
		})))
	
		//timeout
		routine.Go(ctx, routine.Timeout(time.Second, routine.ExecutorFunc(func(ctx context.Context) error {
			return nil
		})))
		return nil
	}))
	//...
}
You can’t perform that action at this time.