Skip to content

shenghui0779/nightfall

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8e9cd0f · Nov 14, 2024

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nightfall

Go协程并发控制,降低CPU和内存负载

特点

  1. 实现简单
  2. 性能优异
  3. 任务支持context
  4. 任务支持缓冲队列
  5. 非阻塞模式下,任务缓存到全局链表

与ants比较

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz

1. example-1

👉 nightfall

func main() {
    ctx := context.Background()
    
    pool := woker.NewPool(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

nightfall_cpu_1.png

mem

nightfall_mem_1.png

👉 ants

func main() {
    ctx := context.Background()
    
    pool, _ := ants.NewPool(5000)
    for i := 0; i < 100000000; i++ {
        i := i
        pool.Submit(func() {
            time.Sleep(time.Second)
            fmt.Println("Index:", i)
        })
    }
    
    <-ctx.Done()
}
cpu

ants_cpu_1.png

mem

ants_mem_1.png

2. example-2

👉 nightfall

func main() {
    ctx := context.Background()
    
    pool := woker.NewPool(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

nightfall_cpu_2.png

mem

nightfall_mem_2.png

👉 ants

func main() {
    ctx := context.Background()
    
    pool, _ := ants.NewPool(5000)
    for i := 0; i < 100; i++ {
        i := i
        pool.Submit(func() {
            for j := 0; j < 1000000; j++ {
                j := j
                pool.Submit(func() {
                    time.Sleep(time.Second)
                    fmt.Println("Index:", i, "-", j)
                })
            }
        })
    }
    
    <-ctx.Done()
}
cpu

ants_cpu_2.png

mem

ants_mem_2.png