chan_pool.go 2.9 KB

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