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.

base_channel.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package queue
  4. import (
  5. "context"
  6. "errors"
  7. "sync"
  8. "time"
  9. "code.gitea.io/gitea/modules/container"
  10. )
  11. var errChannelClosed = errors.New("channel is closed")
  12. type baseChannel struct {
  13. c chan []byte
  14. set container.Set[string]
  15. mu sync.Mutex
  16. isUnique bool
  17. }
  18. var _ baseQueue = (*baseChannel)(nil)
  19. func newBaseChannelGeneric(cfg *BaseConfig, unique bool) (baseQueue, error) {
  20. q := &baseChannel{c: make(chan []byte, cfg.Length), isUnique: unique}
  21. if unique {
  22. q.set = container.Set[string]{}
  23. }
  24. return q, nil
  25. }
  26. func newBaseChannelSimple(cfg *BaseConfig) (baseQueue, error) {
  27. return newBaseChannelGeneric(cfg, false)
  28. }
  29. func newBaseChannelUnique(cfg *BaseConfig) (baseQueue, error) {
  30. return newBaseChannelGeneric(cfg, true)
  31. }
  32. func (q *baseChannel) PushItem(ctx context.Context, data []byte) error {
  33. if q.c == nil {
  34. return errChannelClosed
  35. }
  36. if q.isUnique {
  37. q.mu.Lock()
  38. has := q.set.Contains(string(data))
  39. q.mu.Unlock()
  40. if has {
  41. return ErrAlreadyInQueue
  42. }
  43. }
  44. select {
  45. case q.c <- data:
  46. if q.isUnique {
  47. q.mu.Lock()
  48. q.set.Add(string(data))
  49. q.mu.Unlock()
  50. }
  51. return nil
  52. case <-time.After(pushBlockTime):
  53. return context.DeadlineExceeded
  54. case <-ctx.Done():
  55. return ctx.Err()
  56. }
  57. }
  58. func (q *baseChannel) PopItem(ctx context.Context) ([]byte, error) {
  59. select {
  60. case data, ok := <-q.c:
  61. if !ok {
  62. return nil, errChannelClosed
  63. }
  64. q.mu.Lock()
  65. q.set.Remove(string(data))
  66. q.mu.Unlock()
  67. return data, nil
  68. case <-ctx.Done():
  69. return nil, ctx.Err()
  70. }
  71. }
  72. func (q *baseChannel) HasItem(ctx context.Context, data []byte) (bool, error) {
  73. q.mu.Lock()
  74. defer q.mu.Unlock()
  75. if !q.isUnique {
  76. return false, nil
  77. }
  78. return q.set.Contains(string(data)), nil
  79. }
  80. func (q *baseChannel) Len(ctx context.Context) (int, error) {
  81. q.mu.Lock()
  82. defer q.mu.Unlock()
  83. if q.c == nil {
  84. return 0, errChannelClosed
  85. }
  86. return len(q.c), nil
  87. }
  88. func (q *baseChannel) Close() error {
  89. q.mu.Lock()
  90. defer q.mu.Unlock()
  91. close(q.c)
  92. if q.isUnique {
  93. q.set = container.Set[string]{}
  94. }
  95. return nil
  96. }
  97. func (q *baseChannel) RemoveAll(ctx context.Context) error {
  98. q.mu.Lock()
  99. defer q.mu.Unlock()
  100. for q.c != nil && len(q.c) > 0 {
  101. <-q.c
  102. }
  103. if q.isUnique {
  104. q.set = container.Set[string]{}
  105. }
  106. return nil
  107. }