You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue_channel.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package queue
  5. import (
  6. "context"
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // ChannelQueueType is the type for channel queue
  11. const ChannelQueueType Type = "channel"
  12. // ChannelQueueConfiguration is the configuration for a ChannelQueue
  13. type ChannelQueueConfiguration struct {
  14. WorkerPoolConfiguration
  15. Workers int
  16. Name string
  17. }
  18. // ChannelQueue implements Queue
  19. //
  20. // A channel queue is not persistable and does not shutdown or terminate cleanly
  21. // It is basically a very thin wrapper around a WorkerPool
  22. type ChannelQueue struct {
  23. *WorkerPool
  24. shutdownCtx context.Context
  25. shutdownCtxCancel context.CancelFunc
  26. terminateCtx context.Context
  27. terminateCtxCancel context.CancelFunc
  28. exemplar interface{}
  29. workers int
  30. name string
  31. }
  32. // NewChannelQueue creates a memory channel queue
  33. func NewChannelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
  34. configInterface, err := toConfig(ChannelQueueConfiguration{}, cfg)
  35. if err != nil {
  36. return nil, err
  37. }
  38. config := configInterface.(ChannelQueueConfiguration)
  39. if config.BatchLength == 0 {
  40. config.BatchLength = 1
  41. }
  42. terminateCtx, terminateCtxCancel := context.WithCancel(context.Background())
  43. shutdownCtx, shutdownCtxCancel := context.WithCancel(terminateCtx)
  44. queue := &ChannelQueue{
  45. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  46. shutdownCtx: shutdownCtx,
  47. shutdownCtxCancel: shutdownCtxCancel,
  48. terminateCtx: terminateCtx,
  49. terminateCtxCancel: terminateCtxCancel,
  50. exemplar: exemplar,
  51. workers: config.Workers,
  52. name: config.Name,
  53. }
  54. queue.qid = GetManager().Add(queue, ChannelQueueType, config, exemplar)
  55. return queue, nil
  56. }
  57. // Run starts to run the queue
  58. func (q *ChannelQueue) Run(atShutdown, atTerminate func(func())) {
  59. atShutdown(q.Shutdown)
  60. atTerminate(q.Terminate)
  61. log.Debug("ChannelQueue: %s Starting", q.name)
  62. _ = q.AddWorkers(q.workers, 0)
  63. }
  64. // Push will push data into the queue
  65. func (q *ChannelQueue) Push(data Data) error {
  66. if !assignableTo(data, q.exemplar) {
  67. return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in queue: %s", data, q.exemplar, q.name)
  68. }
  69. q.WorkerPool.Push(data)
  70. return nil
  71. }
  72. // Shutdown processing from this queue
  73. func (q *ChannelQueue) Shutdown() {
  74. q.lock.Lock()
  75. defer q.lock.Unlock()
  76. select {
  77. case <-q.shutdownCtx.Done():
  78. log.Trace("ChannelQueue: %s Already Shutting down", q.name)
  79. return
  80. default:
  81. }
  82. log.Trace("ChannelQueue: %s Shutting down", q.name)
  83. go func() {
  84. log.Trace("ChannelQueue: %s Flushing", q.name)
  85. if err := q.FlushWithContext(q.terminateCtx); err != nil {
  86. log.Warn("ChannelQueue: %s Terminated before completed flushing", q.name)
  87. return
  88. }
  89. log.Debug("ChannelQueue: %s Flushed", q.name)
  90. }()
  91. q.shutdownCtxCancel()
  92. log.Debug("ChannelQueue: %s Shutdown", q.name)
  93. }
  94. // Terminate this queue and close the queue
  95. func (q *ChannelQueue) Terminate() {
  96. log.Trace("ChannelQueue: %s Terminating", q.name)
  97. q.Shutdown()
  98. select {
  99. case <-q.terminateCtx.Done():
  100. return
  101. default:
  102. }
  103. q.terminateCtxCancel()
  104. log.Debug("ChannelQueue: %s Terminated", q.name)
  105. }
  106. // Name returns the name of this queue
  107. func (q *ChannelQueue) Name() string {
  108. return q.name
  109. }
  110. func init() {
  111. queuesMap[ChannelQueueType] = NewChannelQueue
  112. }