- Notifications
You must be signed in to change notification settings - Fork7
go routine control, abstraction of the Main and some useful Executors.如果你不会管理Goroutine的话,用它
License
NotificationsYou must be signed in to change notification settings
x-mod/routine
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
package mainimport ("log""context""github.com/x-mod/routine")funcmain(){iferr:=routine.Main(context.TODO(),routine.Command("echo",routine.ARG("hello routine!")),);err!=nil {log.Fatal(err)}}
Or you can just clone the repo, then runninggo run quickstart/main.go
.
The most functional feature is providing theMain
function abstraction, you can use theroutine.Main
to wrap your main function logic very quickly.
package mainimport ("context""github.com/x-mod/routine")funcMainGo(ctx context.Context)error {log.Println("this is the Main Go func")returnnil}funcChildGo(ctx context.Context)error {log.Println("this is the Child Go func")returnnil}funcprepareGo(ctx context.Context)error {log.Println("this is the prepare Go func")returnnil}funccleanupGo(ctx context.Context)error {log.Println("this is the Clean Go func")returnnil}funcmain(){log.Println(routine.Main(context.TODO(),//main Goroutine.ExecutorFunc(MainGo),//prpare Goroutine.Prepare(routine.ExecutorFunc(prepareGo)),//cleanup Goroutine.Cleanup(routine.ExecutorFunc(cleanupGo)),routine.Go(routine.ExecutorFunc(ChildGo)),//child Goroutine.Go(routine.ExecutorFunc(ChildGo)),routine.Go(routine.ExecutorFunc(ChildGo)),//signalsroutine.Signal(syscall.SIGINT,routine.SigHandler(func() {os.Exit(1)})),),)}
create and control your own routine byroutine.New
.
import"github.com/x-mod/routine"err:=routine.New(opts...).Execute(ctx)
The package provides many useful executor adapters for you:
- guarantee
- timeout & deadline
- retry & repeat
- concurrent
- crontab
- parallel & sequence
- command
- profiling
with these executor adapters, you can building the most complex goroutine logic.
import"github.com/x-mod/routine"//timeouttimeout:=routine.Timeout(time.Minute,exec)//retryretry:=routine.Retry(3,exec)//repeatrepeat:=routine.Repeat(10,time.Second,exec)//concurrentconcurrent:=routine.Concurrent(4,exec)//schedule executorcrontab:=routine.Crontab("* * * * *",exec)//commandcommand:=routine.Command("echo",routine.ARG("hello routine!"))//parallelparallel:=routine.Parallel(exec1,exec2,exec3,...)//sequencesequece:=routine.Append(exec1,exec2,exec3,...)
More details, please check theexample and trace it.
$: go run example/main.go# trace go routine & tasks$: go tool trace trace.out
Then you can check the tasks like this:
About
go routine control, abstraction of the Main and some useful Executors.如果你不会管理Goroutine的话,用它
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published