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 2.7KB

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. }