github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/percpu/chan.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package percpu
     4  
     5  import (
     6  	"runtime"
     7  )
     8  
     9  type Chan[T any] struct {
    10  	c chan T
    11  
    12  	count int
    13  	cList []chan T
    14  }
    15  
    16  func NewChan[T any](size int) *Chan[T] {
    17  	var count = max(runtime.NumCPU(), runtime.GOMAXPROCS(0))
    18  	var cList []chan T
    19  	for i := 0; i < count; i++ {
    20  		cList = append(cList, make(chan T, size))
    21  	}
    22  
    23  	return &Chan[T]{
    24  		c:     make(chan T, size),
    25  		count: count,
    26  		cList: cList,
    27  	}
    28  }
    29  
    30  func (this *Chan[T]) C() chan T {
    31  	var procId = GetProcId()
    32  	if procId < this.count {
    33  		return this.cList[procId]
    34  	}
    35  	return this.c
    36  }