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.

62 lines
1.4 KiB

  1. // Copyright 2018 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 issues
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/modules/setting"
  8. )
  9. // ChannelQueue implements
  10. type ChannelQueue struct {
  11. queue chan *IndexerData
  12. indexer Indexer
  13. batchNumber int
  14. }
  15. // NewChannelQueue create a memory channel queue
  16. func NewChannelQueue(indexer Indexer, batchNumber int) *ChannelQueue {
  17. return &ChannelQueue{
  18. queue: make(chan *IndexerData, setting.Indexer.UpdateQueueLength),
  19. indexer: indexer,
  20. batchNumber: batchNumber,
  21. }
  22. }
  23. // Run starts to run the queue
  24. func (c *ChannelQueue) Run() error {
  25. var i int
  26. var datas = make([]*IndexerData, 0, c.batchNumber)
  27. for {
  28. select {
  29. case data := <-c.queue:
  30. if data.IsDelete {
  31. c.indexer.Delete(data.IDs...)
  32. continue
  33. }
  34. datas = append(datas, data)
  35. if len(datas) >= c.batchNumber {
  36. c.indexer.Index(datas)
  37. // TODO: save the point
  38. datas = make([]*IndexerData, 0, c.batchNumber)
  39. }
  40. case <-time.After(time.Millisecond * 100):
  41. i++
  42. if i >= 3 && len(datas) > 0 {
  43. c.indexer.Index(datas)
  44. // TODO: save the point
  45. datas = make([]*IndexerData, 0, c.batchNumber)
  46. }
  47. }
  48. }
  49. }
  50. // Push will push the indexer data to queue
  51. func (c *ChannelQueue) Push(data *IndexerData) error {
  52. c.queue <- data
  53. return nil
  54. }