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

queue_channel.go 2.8 kB

Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "reflect"
  9. "time"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // ChannelQueueType is the type for channel queue
  13. const ChannelQueueType Type = "channel"
  14. // ChannelQueueConfiguration is the configuration for a ChannelQueue
  15. type ChannelQueueConfiguration struct {
  16. QueueLength int
  17. BatchLength int
  18. Workers int
  19. MaxWorkers int
  20. BlockTimeout time.Duration
  21. BoostTimeout time.Duration
  22. BoostWorkers int
  23. Name string
  24. }
  25. // ChannelQueue implements
  26. type ChannelQueue struct {
  27. pool *WorkerPool
  28. exemplar interface{}
  29. workers int
  30. name string
  31. }
  32. // NewChannelQueue create 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. dataChan := make(chan Data, config.QueueLength)
  43. ctx, cancel := context.WithCancel(context.Background())
  44. queue := &ChannelQueue{
  45. pool: &WorkerPool{
  46. baseCtx: ctx,
  47. cancel: cancel,
  48. batchLength: config.BatchLength,
  49. handle: handle,
  50. dataChan: dataChan,
  51. blockTimeout: config.BlockTimeout,
  52. boostTimeout: config.BoostTimeout,
  53. boostWorkers: config.BoostWorkers,
  54. maxNumberOfWorkers: config.MaxWorkers,
  55. },
  56. exemplar: exemplar,
  57. workers: config.Workers,
  58. name: config.Name,
  59. }
  60. queue.pool.qid = GetManager().Add(queue, ChannelQueueType, config, exemplar, queue.pool)
  61. return queue, nil
  62. }
  63. // Run starts to run the queue
  64. func (c *ChannelQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
  65. atShutdown(context.Background(), func() {
  66. log.Warn("ChannelQueue: %s is not shutdownable!", c.name)
  67. })
  68. atTerminate(context.Background(), func() {
  69. log.Warn("ChannelQueue: %s is not terminatable!", c.name)
  70. })
  71. go func() {
  72. _ = c.pool.AddWorkers(c.workers, 0)
  73. }()
  74. }
  75. // Push will push data into the queue
  76. func (c *ChannelQueue) Push(data Data) error {
  77. if c.exemplar != nil {
  78. // Assert data is of same type as r.exemplar
  79. t := reflect.TypeOf(data)
  80. exemplarType := reflect.TypeOf(c.exemplar)
  81. if !t.AssignableTo(exemplarType) || data == nil {
  82. return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in queue: %s", data, c.exemplar, c.name)
  83. }
  84. }
  85. c.pool.Push(data)
  86. return nil
  87. }
  88. // Name returns the name of this queue
  89. func (c *ChannelQueue) Name() string {
  90. return c.name
  91. }
  92. func init() {
  93. queuesMap[ChannelQueueType] = NewChannelQueue
  94. }