Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

Repository files navigation

GoDocGo Report CardBuild StatusVersionCoverage Status

Routine Architecture

Quick Start

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.

Main Routine

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)})),),)}

Routine

create and control your own routine byroutine.New.

import"github.com/x-mod/routine"err:=routine.New(opts...).Execute(ctx)

Executors

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,...)

Enjoy

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

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp