|
@@ -1,6 +1,8 @@
|
|
package queue
|
|
package queue
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "git.haoqitour.com/haoqi/go-common/utils"
|
|
|
|
+ "git.haoqitour.com/haoqi/go-common/utils/logger"
|
|
"os"
|
|
"os"
|
|
"os/signal"
|
|
"os/signal"
|
|
"syscall"
|
|
"syscall"
|
|
@@ -12,6 +14,7 @@ type ChanWorker struct {
|
|
WorkerPool chan chan interface{} // 工作管道池,实例化时由调度器传入
|
|
WorkerPool chan chan interface{} // 工作管道池,实例化时由调度器传入
|
|
JobChannel chan interface{} // 工作管道
|
|
JobChannel chan interface{} // 工作管道
|
|
quit chan bool // 退出消息
|
|
quit chan bool // 退出消息
|
|
|
|
+ log *logger.Logger
|
|
}
|
|
}
|
|
|
|
|
|
func NewChanWorker(workerId, capacity int, workerPool chan chan interface{}) *ChanWorker {
|
|
func NewChanWorker(workerId, capacity int, workerPool chan chan interface{}) *ChanWorker {
|
|
@@ -27,11 +30,13 @@ func NewChanWorker(workerId, capacity int, workerPool chan chan interface{}) *Ch
|
|
WorkerPool: workerPool,
|
|
WorkerPool: workerPool,
|
|
JobChannel: jobChannel,
|
|
JobChannel: jobChannel,
|
|
quit: make(chan bool),
|
|
quit: make(chan bool),
|
|
|
|
+ log: logger.New(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func (w *ChanWorker) Start(callback func(workerId int, msg interface{})) {
|
|
func (w *ChanWorker) Start(callback func(workerId int, msg interface{})) {
|
|
go func(w *ChanWorker, callback func(workerId int, msg interface{})) {
|
|
go func(w *ChanWorker, callback func(workerId int, msg interface{})) {
|
|
|
|
+ defer utils.DefaultGoroutineRecover(w.log, `chan池工作对象消息处理`)
|
|
for {
|
|
for {
|
|
// 新工作管道加入工作管道池
|
|
// 新工作管道加入工作管道池
|
|
w.WorkerPool <- w.JobChannel
|
|
w.WorkerPool <- w.JobChannel
|
|
@@ -45,25 +50,22 @@ func (w *ChanWorker) Start(callback func(workerId int, msg interface{})) {
|
|
}
|
|
}
|
|
}(w, callback)
|
|
}(w, callback)
|
|
|
|
|
|
- go w.closeWait()
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (w *ChanWorker) Stop() {
|
|
|
|
- go func(w *ChanWorker) {
|
|
|
|
- w.quit <- true
|
|
|
|
- }(w)
|
|
|
|
|
|
+ w.closeWait()
|
|
}
|
|
}
|
|
|
|
|
|
func (w *ChanWorker) closeWait() {
|
|
func (w *ChanWorker) closeWait() {
|
|
- var c chan os.Signal
|
|
|
|
- c = make(chan os.Signal, 1)
|
|
|
|
- signal.Notify(c, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
- for {
|
|
|
|
- select {
|
|
|
|
- case <-c:
|
|
|
|
- w.Stop()
|
|
|
|
|
|
+ go func(w *ChanWorker) {
|
|
|
|
+ defer utils.DefaultGoroutineRecover(w.log, `chan池关闭`)
|
|
|
|
+ var c chan os.Signal
|
|
|
|
+ c = make(chan os.Signal, 1)
|
|
|
|
+ signal.Notify(c, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case <-c:
|
|
|
|
+ w.quit <- true
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ }(w)
|
|
}
|
|
}
|
|
|
|
|
|
// 调度对象
|
|
// 调度对象
|
|
@@ -72,6 +74,7 @@ type ChanDispatcher struct {
|
|
WorkerPool chan chan interface{} // 工作管道池
|
|
WorkerPool chan chan interface{} // 工作管道池
|
|
maxWorkers int // 最大工作对象数
|
|
maxWorkers int // 最大工作对象数
|
|
capacity int // 工作管道消息缓冲大小
|
|
capacity int // 工作管道消息缓冲大小
|
|
|
|
+ log *logger.Logger
|
|
}
|
|
}
|
|
|
|
|
|
func NewChanDispatcher(msgQueue chan interface{}, maxWorkers int) *ChanDispatcher {
|
|
func NewChanDispatcher(msgQueue chan interface{}, maxWorkers int) *ChanDispatcher {
|
|
@@ -80,6 +83,7 @@ func NewChanDispatcher(msgQueue chan interface{}, maxWorkers int) *ChanDispatche
|
|
WorkerPool: make(chan chan interface{}, maxWorkers),
|
|
WorkerPool: make(chan chan interface{}, maxWorkers),
|
|
maxWorkers: maxWorkers,
|
|
maxWorkers: maxWorkers,
|
|
capacity: -1,
|
|
capacity: -1,
|
|
|
|
+ log: logger.New(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -89,6 +93,7 @@ func NewChanDispatcherWithCapacity(msgQueue chan interface{}, maxWorkers, capaci
|
|
WorkerPool: make(chan chan interface{}, maxWorkers),
|
|
WorkerPool: make(chan chan interface{}, maxWorkers),
|
|
maxWorkers: maxWorkers,
|
|
maxWorkers: maxWorkers,
|
|
capacity: capacity,
|
|
capacity: capacity,
|
|
|
|
+ log: logger.New(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -98,20 +103,21 @@ func (d *ChanDispatcher) Run(callback func(workerId int, msg interface{})) {
|
|
worker.Start(callback)
|
|
worker.Start(callback)
|
|
}
|
|
}
|
|
|
|
|
|
- go d.dispatch()
|
|
|
|
|
|
+ d.dispatch()
|
|
}
|
|
}
|
|
|
|
|
|
func (d *ChanDispatcher) dispatch() {
|
|
func (d *ChanDispatcher) dispatch() {
|
|
- for {
|
|
|
|
- select {
|
|
|
|
- case msg := <-d.MsgQueue:
|
|
|
|
- go func(d *ChanDispatcher, msg interface{}) {
|
|
|
|
|
|
+ go func(d *ChanDispatcher) {
|
|
|
|
+ defer utils.DefaultGoroutineRecover(d.log, `chan池调度`)
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case msg := <-d.MsgQueue:
|
|
// 从工作管道池中尝试取出一个空闲(未阻塞)的工作管道,无空闲工作管道时阻塞
|
|
// 从工作管道池中尝试取出一个空闲(未阻塞)的工作管道,无空闲工作管道时阻塞
|
|
jobChannel := <-d.WorkerPool
|
|
jobChannel := <-d.WorkerPool
|
|
|
|
|
|
// 将一条消息发送给当前工作管道
|
|
// 将一条消息发送给当前工作管道
|
|
jobChannel <- msg
|
|
jobChannel <- msg
|
|
- }(d, msg)
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ }(d)
|
|
}
|
|
}
|