github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/gopool/README.md (about)

     1  # gopool
     2  
     3  ## Introduction
     4  
     5  Package gopool is a high-performance goroutine pool which aims to reuse goroutines
     6  and limit the number of goroutines.
     7  
     8  It is an alternative to the `go` keyword.
     9  
    10  This package is a fork of `github.com/bytedance/gopkg/util/gopool`.
    11  
    12  ## Features
    13  
    14  - High performance
    15  - Auto-recovering panics
    16  - Limit goroutine numbers
    17  - Reuse goroutine stack
    18  
    19  ## QuickStart
    20  
    21  Just replace your `go func(){...}` with `gopool.Go(func(){...})`.
    22  
    23  old:
    24  ```go
    25  go func() {
    26  	// do your job
    27  }()
    28  ```
    29  
    30  new:
    31  ```go
    32  gopool.Go(func() {
    33  	// do your job
    34  })
    35  
    36  // or with context
    37  gopool.CtxGo(ctx, func() {
    38  	// do your job
    39  })
    40  ```
    41  
    42  Or create a dedicated pool for specific workload:
    43  ```go
    44  myPool := gopool.NewPool(&gopool.Config{
    45  	// configuration
    46  })
    47  
    48  myPool.Go(func() {
    49  	// do your job
    50  })
    51  myPool.CtxGo(ctx, func() {
    52  	// do your job
    53  })
    54  ```
    55  
    56  Or create a pool to execute a handler to process values of a specific type:
    57  ```go
    58  myHandler := func(ctx context.Context, arg SomeType) {
    59  	// do your job
    60  }
    61  myPool := gopool.NewTypedPool(myHandler, &gopool.Config{
    62  	// configuration
    63  })
    64  
    65  myPool.Go(varOfSomeType)
    66  myPool.CtxGo(ctx, varOfSomeType)
    67  ```
    68  
    69  See package doc for more information.