1、为了防止一下子起很多goroutine,使用一个带缓冲channel的信号量限制并发起的goroutine起的数量,tryAcquire非常新颖的用法
package semaphore
import ()
type Semaphore struct {
bufSize int
channel chan int8
}
func NewSemaphore(concurrencyNum int) *Semaphore {
return &Semaphore{channel: make(chan int8, concurrencyNum), bufSize: concurrencyNum}
}
func (this *Semaphore) TryAcquire() bool {
select {
case this.channel <- int8(0):
return true
default:
return false
}
}
func (this *Semaphore) Acquire() {
this.channel <- int8(0)
}
func (this *Semaphore) Release() {
<-this.channel
}
func (this *Semaphore) AvailablePermits() int {
return this.bufSize - len(this.channel)
}
transfer 将数据rpc到judge:
// Judge定时任务, 将 Judge发送缓存中的数据 通过rpc连接池 发送到Judge
func forward2JudgeTask(Q *list.SafeListLimited, node string, concurrent int) {
batch := g.Config().Judge.Batch // 一次发送,最多batch条数据
addr := g.Config().