chan_pool.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // 新工作管道加入工作管道池
  32. w.WorkerPool <- w.JobChannel
  33. defer utils.DefaultGoroutineRecover(nil, `chan池工作对象消息处理`)
  34. for {
  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. var s os.Signal
  50. c = make(chan os.Signal, 1)
  51. signal.Notify(c, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  52. for {
  53. s = <-c
  54. switch s {
  55. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  56. w.quit <- true
  57. return
  58. default:
  59. return
  60. }
  61. }
  62. }(w)
  63. }
  64. // 调度对象
  65. type ChanDispatcher struct {
  66. MsgQueue chan interface{} // 消息输入管道
  67. WorkerPool chan chan interface{} // 工作管道池
  68. maxWorkers int // 最大工作对象数
  69. capacity int // 工作管道消息缓冲大小
  70. }
  71. func NewChanDispatcher(msgQueue chan interface{}, maxWorkers int) *ChanDispatcher {
  72. return &ChanDispatcher{
  73. MsgQueue: msgQueue,
  74. WorkerPool: make(chan chan interface{}, maxWorkers),
  75. maxWorkers: maxWorkers,
  76. capacity: -1,
  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. }
  86. }
  87. func (d *ChanDispatcher) Run(callback func(workerId int, msg interface{})) {
  88. for i := 0; i < d.maxWorkers; i++ {
  89. worker := NewChanWorker(i, d.capacity, d.WorkerPool)
  90. worker.Start(callback)
  91. }
  92. d.dispatch()
  93. }
  94. func (d *ChanDispatcher) dispatch() {
  95. go func(d *ChanDispatcher) {
  96. defer utils.DefaultGoroutineRecover(nil, `chan池调度`)
  97. for {
  98. select {
  99. case msg := <-d.MsgQueue:
  100. // 从工作管道池中尝试取出一个空闲(未阻塞)的工作管道,无空闲工作管道时阻塞
  101. jobChannel := <-d.WorkerPool
  102. // 将一条消息发送给当前工作管道
  103. jobChannel <- msg
  104. }
  105. }
  106. }(d)
  107. }