1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| package main
import ( "fmt" "runtime" "time" )
var ( MaxWorker = 10 MaxDispatchNumControl = 20 )
type Payload struct { Num int }
type Job struct { Payload Payload }
var JobQueue chan Job
var WorkerList []*Worker
var DispatchNumControl chan bool
func Limit(job Job) bool { select { case <-time.After(time.Millisecond * 100): fmt.Println("我很忙") return false case DispatchNumControl <- true: JobQueue <- job return true } }
type Worker struct { WorkerPool chan chan Job JobChan chan Job quit chan bool No int }
func NewWorker(workerPool chan chan Job, no int) *Worker { fmt.Println("创建了工作者", no) return &Worker{ WorkerPool: workerPool, JobChan: make(chan Job), quit: make(chan bool), No: no, } }
func (w *Worker) Start() { go func() { for { w.WorkerPool <- w.JobChan fmt.Println("w.WorkerPool <- w.JobChan | w:", w) select { case job := <-w.JobChan: fmt.Println("job := <-w.JobChan") fmt.Println(job) <-DispatchNumControl time.Sleep(5 * time.Second) case <-w.quit: fmt.Println("<-w.Quit | w:", w) return } } }() }
func (w *Worker) Stop() { go func() { w.quit <- true }() }
type Dispatcher struct { WorkerPool chan chan Job MaxWorkers int ActiveCount int }
func NewDispatcher(max int) *Dispatcher { return &Dispatcher{ WorkerPool: make(chan chan Job, max), MaxWorkers: max, } }
func (d *Dispatcher) Run() { for i := 0; i < d.MaxWorkers; i++ { worker := NewWorker(d.WorkerPool, i) worker.Start() WorkerList[i] = worker d.ActiveCount++ } go d.dispatcher() }
func (d *Dispatcher) dispatcher() { for { select { case job := <-JobQueue: go func(job Job) { jobChan := <-d.WorkerPool fmt.Println("jobChan := <-d.WorkerPool") jobChan <- job
fmt.Println("d.ActiveCount: ", d.ActiveCount) if d.ActiveCount > 5 { worker := WorkerList[d.ActiveCount-1] fmt.Println("worker := WorkerList[d.ActiveCount-1] | worker: ", worker) worker.Stop() d.ActiveCount-- } }(job) } } }
func AddQueue(n int) { for i := 0; i < n; i++ { job := Job{Payload{i}} fmt.Println("JobQueue <- job", job)
if Limit(job) { fmt.Println("任务成功加入全局队列") } else { fmt.Println("全局队列已满, 暂不处理任务") i-- } fmt.Println("当前协程数:", runtime.NumGoroutine()) time.Sleep(200 * time.Millisecond) } }
func main() { DispatchNumControl = make(chan bool, MaxDispatchNumControl) JobQueue = make(chan Job) WorkerList = make([]*Worker, 10)
disp := NewDispatcher(MaxWorker) disp.Run() time.Sleep(1 * time.Second)
AddQueue(100) fmt.Println() time.Sleep(1000 * time.Second) }
|