代码拉取完成,页面将自动刷新
package gws
import (
"sync"
"github.com/lxzan/gws/internal"
)
type (
workerQueue struct {
mu sync.Mutex // 锁
q internal.Deque[asyncJob] // 任务队列
maxConcurrency int32 // 最大并发
curConcurrency int32 // 当前并发
}
asyncJob func()
)
// newWorkerQueue 创建一个任务队列
func newWorkerQueue(maxConcurrency int32) *workerQueue {
c := &workerQueue{
mu: sync.Mutex{},
maxConcurrency: maxConcurrency,
curConcurrency: 0,
}
return c
}
// 获取一个任务
func (c *workerQueue) getJob(newJob asyncJob, delta int32) asyncJob {
c.mu.Lock()
defer c.mu.Unlock()
if newJob != nil {
c.q.PushBack(newJob)
}
c.curConcurrency += delta
if c.curConcurrency >= c.maxConcurrency {
return nil
}
var job = c.q.PopFront()
if job == nil {
return nil
}
c.curConcurrency++
return job
}
// 循环执行任务
func (c *workerQueue) do(job asyncJob) {
for job != nil {
job()
job = c.getJob(nil, -1)
}
}
// Push 追加任务, 有资源空闲的话会立即执行
func (c *workerQueue) Push(job asyncJob) {
if nextJob := c.getJob(job, 0); nextJob != nil {
go c.do(nextJob)
}
}
type channel chan struct{}
func (c channel) add() { c <- struct{}{} }
func (c channel) done() { <-c }
func (c channel) Go(m *Message, f func(*Message) error) error {
c.add()
go func() {
_ = f(m)
c.done()
}()
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。