go实现简单的并发限流

XXX_MAX_CONN = 5
var XXX_SEMAPHORE = make(chan struct{}, XXX_MAX_CONN)

func your_func() {
  // 在开始处理请求时放入一个信号
  XXX_SEMAPHORE <- struct{}{}
  defer func() {
    // 在完成请求后移除一个信号
    <-XXX_SEMAPHORE
  }()
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *