github.com/pubgo/xprocess@v0.1.11/default.go (about)

     1  package xprocess
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/pubgo/xprocess/xprocess_test"
     8  )
     9  
    10  var defaultProcess = &process{}
    11  
    12  func Test(fn interface{}) *xprocess_test.Test { return xprocess_test.TestFuncWith(fn) }
    13  func MemStatsPrint()                          { defaultProcess.memStatsPrint() }
    14  func CostWith(fn func()) time.Duration        { return defaultProcess.costWith(fn) }
    15  func Count(n int) <-chan int                  { return defaultProcess.count(n) }
    16  
    17  // Tick 简单定时器
    18  // Example: Tick(100, time.Second)
    19  func Tick(args ...interface{}) <-chan time.Time { return defaultProcess.tick(args...) }
    20  
    21  // Try
    22  // try wrap
    23  func Try(fn func()) error { return defaultProcess.try(fn) }
    24  
    25  // Go
    26  // 启动一个goroutine
    27  func Go(fn func(ctx context.Context)) context.CancelFunc { return defaultProcess.goCtx(fn) }
    28  
    29  // GoLoop
    30  // 启动一个goroutine loop
    31  // 是为了替换 `go func() {for{ }}()` 这类的代码
    32  func GoLoop(fn func(ctx context.Context)) context.CancelFunc { return defaultProcess.goLoopCtx(fn) }
    33  
    34  // GoDelay
    35  // 延迟goroutine
    36  func GoDelay(dur time.Duration, fn func()) error { return defaultProcess.goWithDelay(dur, fn) }
    37  
    38  // Timeout
    39  // 执行超时函数, 超时后, 函数自动退出
    40  func Timeout(dur time.Duration, fn func(ctx context.Context)) error {
    41  	return defaultProcess.goWithTimeout(dur, fn)
    42  }