Skip to content

noble-gase/xenon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

氙-Xe

golang pkg.go.dev MIT

[氙-Xe] Go协程并发复用,降低CPU和内存负载

安装

go get -u github.com/noble-gase/xe
模块 说明
errgroup 控制协程数量并按需创建
timewheel 分层时间轮(任务支持重试)
worker 基于 channel 实现的协程池

流程图

flowchart.jpg

效果

goos: darwin
goarch: arm64
cpu: Apple M4

场景-1

func main() {
    ctx := context.Background()

    pool := worker.New(5000)
    for i := 0; i < 100000000; i++ {
        i := i
        pool.Go(ctx, func(ctx context.Context) {
            time.Sleep(time.Second)
            fmt.Println("Index:", i)
        })
    }

    <-ctx.Done()
}

cpu

cpu-1.png

mem

mem-1.png

场景-2

func main() {
    ctx := context.Background()

    pool := worker.New(5000)
    for i := 0; i < 100; i++ {
        i := i
        pool.Go(ctx, func(ctx context.Context) {
            for j := 0; j < 1000000; j++ {
                j := j
                pool.Go(ctx, func(ctx context.Context) {
                    time.Sleep(time.Second)
                    fmt.Println("Index:", i, "-", j)
                })
            }
        })
    }

    <-ctx.Done()
}

cpu

cpu-2.png

mem

mem-2.png

About

[氙-Xe] Go协程并发复用,降低CPU和内存负载

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages