gitea源码

queue_tester.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package admin
  4. import (
  5. "runtime/pprof"
  6. "sync"
  7. "time"
  8. "code.gitea.io/gitea/modules/graceful"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/process"
  11. "code.gitea.io/gitea/modules/queue"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. var testQueueOnce sync.Once
  15. // initTestQueueOnce initializes the test queue for dev mode
  16. // the test queue will also be shown in the queue list
  17. // developers could see the queue length / worker number / items number on the admin page and try to remove the items
  18. func initTestQueueOnce() {
  19. testQueueOnce.Do(func() {
  20. ctx, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "TestQueue", process.SystemProcessType, false)
  21. qs := setting.QueueSettings{
  22. Name: "test-queue",
  23. Type: "channel",
  24. Length: 20,
  25. BatchLength: 2,
  26. MaxWorkers: 3,
  27. }
  28. testQueue, err := queue.NewWorkerPoolQueueWithContext(ctx, "test-queue", qs, func(t ...int64) (unhandled []int64) {
  29. for range t {
  30. select {
  31. case <-graceful.GetManager().ShutdownContext().Done():
  32. case <-time.After(5 * time.Second):
  33. }
  34. }
  35. return nil
  36. }, true)
  37. if err != nil {
  38. log.Error("unable to create test queue: %v", err)
  39. return
  40. }
  41. queue.GetManager().AddManagedQueue(testQueue)
  42. testQueue.SetWorkerMaxNumber(5)
  43. go graceful.GetManager().RunWithCancel(testQueue)
  44. go func() {
  45. pprof.SetGoroutineLabels(ctx)
  46. defer finished()
  47. cnt := int64(0)
  48. adding := true
  49. for {
  50. select {
  51. case <-ctx.Done():
  52. case <-time.After(500 * time.Millisecond):
  53. if adding {
  54. if testQueue.GetQueueItemNumber() == qs.Length {
  55. adding = false
  56. }
  57. } else {
  58. if testQueue.GetQueueItemNumber() == 0 {
  59. adding = true
  60. }
  61. }
  62. if adding {
  63. _ = testQueue.Push(cnt)
  64. cnt++
  65. }
  66. }
  67. }
  68. }()
  69. })
  70. }