chan_pool.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package queue
  2. import (
  3. "git.haoqitour.com/haoqi/go-common/utils"
  4. "git.haoqitour.com/haoqi/go-common/utils/logger"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. )
  9. // 工作对象封装
  10. type ChanWorker struct {
  11. ID int // 工作对象编号
  12. WorkerPool chan chan interface{} // 工作管道池,实例化时由调度器传入
  13. JobChannel chan interface{} // 工作管道
  14. quit chan bool // 退出消息
  15. log *logger.Logger
  16. }
  17. func NewChanWorker(workerId, capacity int, workerPool chan chan interface{}) *ChanWorker {
  18. var jobChannel chan interface{}
  19. if capacity < 0 {
  20. jobChannel = make(chan interface{})
  21. } else {
  22. jobChannel = make(chan interface{}, capacity)
  23. }
  24. return &ChanWorker{
  25. ID: workerId,
  26. WorkerPool: workerPool,
  27. JobChannel: jobChannel,
  28. quit: make(chan bool),
  29. log: logger.New(),
  30. }
  31. }
  32. func (w *ChanWorker) Start(callback func(workerId int, msg interface{})) {
  33. go func(w *ChanWorker, callback func(workerId int, msg interface{})) {
  34. defer utils.DefaultGoroutineRecover(w.log, `chan池工作对象消息处理`)
  35. for {
  36. // 新工作管道加入工作管道池
  37. w.WorkerPool <- w.JobChannel
  38. select {
  39. case msg := <-w.JobChannel:
  40. callback(w.ID, msg)
  41. case <-w.quit:
  42. return
  43. }
  44. }
  45. }(w, callback)
  46. w.closeWait()
  47. }
  48. func (w *ChanWorker) closeWait() {
  49. go func(w *ChanWorker) {
  50. defer utils.DefaultGoroutineRecover(w.log, `chan池关闭`)
  51. var c chan os.Signal
  52. c = make(chan os.Signal, 1)
  53. signal.Notify(c, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  54. for {
  55. select {
  56. case <-c:
  57. w.quit <- true
  58. }
  59. }
  60. }(w)
  61. }
  62. // 调度对象
  63. type ChanDispatcher struct {
  64. MsgQueue chan interface{} // 消息输入管道
  65. WorkerPool chan chan interface{} // 工作管道池
  66. maxWorkers int // 最大工作对象数
  67. capacity int // 工作管道消息缓冲大小
  68. log *logger.Logger
  69. }
  70. func NewChanDispatcher(msgQueue chan interface{}, maxWorkers int) *ChanDispatcher {
  71. return &ChanDispatcher{
  72. MsgQueue: msgQueue,
  73. WorkerPool: make(chan chan interface{}, maxWorkers),
  74. maxWorkers: maxWorkers,
  75. capacity: -1,
  76. log: logger.New(),
  77. }
  78. }
  79. func NewChanDispatcherWithCapacity(msgQueue chan interface{}, maxWorkers, capacity int) *ChanDispatcher {
  80. return &ChanDispatcher{
  81. MsgQueue: msgQueue,
  82. WorkerPool: make(chan chan interface{}, maxWorkers),
  83. maxWorkers: maxWorkers,
  84. capacity: capacity,
  85. log: logger.New(),
  86. }
  87. }
  88. func (d *ChanDispatcher) Run(callback func(workerId int, msg interface{})) {
  89. for i := 0; i < d.maxWorkers; i++ {
  90. worker := NewChanWorker(i, d.capacity, d.WorkerPool)
  91. worker.Start(callback)
  92. }
  93. d.dispatch()
  94. }
  95. func (d *ChanDispatcher) dispatch() {
  96. go func(d *ChanDispatcher) {
  97. defer utils.DefaultGoroutineRecover(d.log, `chan池调度`)
  98. for {
  99. select {
  100. case msg := <-d.MsgQueue:
  101. // 从工作管道池中尝试取出一个空闲(未阻塞)的工作管道,无空闲工作管道时阻塞
  102. jobChannel := <-d.WorkerPool
  103. // 将一条消息发送给当前工作管道
  104. jobChannel <- msg
  105. }
  106. }
  107. }(d)
  108. }