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_bytefifo.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2020 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. "encoding/json"
  8. "fmt"
  9. "sync"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue
  14. type ByteFIFOQueueConfiguration struct {
  15. WorkerPoolConfiguration
  16. Workers int
  17. Name string
  18. }
  19. var _ (Queue) = &ByteFIFOQueue{}
  20. // ByteFIFOQueue is a Queue formed from a ByteFIFO and WorkerPool
  21. type ByteFIFOQueue struct {
  22. *WorkerPool
  23. byteFIFO ByteFIFO
  24. typ Type
  25. closed chan struct{}
  26. terminated chan struct{}
  27. exemplar interface{}
  28. workers int
  29. name string
  30. lock sync.Mutex
  31. }
  32. // NewByteFIFOQueue creates a new ByteFIFOQueue
  33. func NewByteFIFOQueue(typ Type, byteFIFO ByteFIFO, handle HandlerFunc, cfg, exemplar interface{}) (*ByteFIFOQueue, error) {
  34. configInterface, err := toConfig(ByteFIFOQueueConfiguration{}, cfg)
  35. if err != nil {
  36. return nil, err
  37. }
  38. config := configInterface.(ByteFIFOQueueConfiguration)
  39. return &ByteFIFOQueue{
  40. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  41. byteFIFO: byteFIFO,
  42. typ: typ,
  43. closed: make(chan struct{}),
  44. terminated: make(chan struct{}),
  45. exemplar: exemplar,
  46. workers: config.Workers,
  47. name: config.Name,
  48. }, nil
  49. }
  50. // Name returns the name of this queue
  51. func (q *ByteFIFOQueue) Name() string {
  52. return q.name
  53. }
  54. // Push pushes data to the fifo
  55. func (q *ByteFIFOQueue) Push(data Data) error {
  56. return q.PushFunc(data, nil)
  57. }
  58. // PushFunc pushes data to the fifo
  59. func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error {
  60. if !assignableTo(data, q.exemplar) {
  61. return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name)
  62. }
  63. bs, err := json.Marshal(data)
  64. if err != nil {
  65. return err
  66. }
  67. return q.byteFIFO.PushFunc(bs, fn)
  68. }
  69. // IsEmpty checks if the queue is empty
  70. func (q *ByteFIFOQueue) IsEmpty() bool {
  71. q.lock.Lock()
  72. defer q.lock.Unlock()
  73. if !q.WorkerPool.IsEmpty() {
  74. return false
  75. }
  76. return q.byteFIFO.Len() == 0
  77. }
  78. // Run runs the bytefifo queue
  79. func (q *ByteFIFOQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
  80. atShutdown(context.Background(), q.Shutdown)
  81. atTerminate(context.Background(), q.Terminate)
  82. log.Debug("%s: %s Starting", q.typ, q.name)
  83. go func() {
  84. _ = q.AddWorkers(q.workers, 0)
  85. }()
  86. go q.readToChan()
  87. log.Trace("%s: %s Waiting til closed", q.typ, q.name)
  88. <-q.closed
  89. log.Trace("%s: %s Waiting til done", q.typ, q.name)
  90. q.Wait()
  91. log.Trace("%s: %s Waiting til cleaned", q.typ, q.name)
  92. ctx, cancel := context.WithCancel(context.Background())
  93. atTerminate(ctx, cancel)
  94. q.CleanUp(ctx)
  95. cancel()
  96. }
  97. func (q *ByteFIFOQueue) readToChan() {
  98. for {
  99. select {
  100. case <-q.closed:
  101. // tell the pool to shutdown.
  102. q.cancel()
  103. return
  104. default:
  105. q.lock.Lock()
  106. bs, err := q.byteFIFO.Pop()
  107. if err != nil {
  108. q.lock.Unlock()
  109. log.Error("%s: %s Error on Pop: %v", q.typ, q.name, err)
  110. time.Sleep(time.Millisecond * 100)
  111. continue
  112. }
  113. if len(bs) == 0 {
  114. q.lock.Unlock()
  115. time.Sleep(time.Millisecond * 100)
  116. continue
  117. }
  118. data, err := unmarshalAs(bs, q.exemplar)
  119. if err != nil {
  120. log.Error("%s: %s Failed to unmarshal with error: %v", q.typ, q.name, err)
  121. q.lock.Unlock()
  122. time.Sleep(time.Millisecond * 100)
  123. continue
  124. }
  125. log.Trace("%s %s: Task found: %#v", q.typ, q.name, data)
  126. q.WorkerPool.Push(data)
  127. q.lock.Unlock()
  128. }
  129. }
  130. }
  131. // Shutdown processing from this queue
  132. func (q *ByteFIFOQueue) Shutdown() {
  133. log.Trace("%s: %s Shutting down", q.typ, q.name)
  134. q.lock.Lock()
  135. select {
  136. case <-q.closed:
  137. default:
  138. close(q.closed)
  139. }
  140. q.lock.Unlock()
  141. log.Debug("%s: %s Shutdown", q.typ, q.name)
  142. }
  143. // Terminate this queue and close the queue
  144. func (q *ByteFIFOQueue) Terminate() {
  145. log.Trace("%s: %s Terminating", q.typ, q.name)
  146. q.Shutdown()
  147. q.lock.Lock()
  148. select {
  149. case <-q.terminated:
  150. q.lock.Unlock()
  151. return
  152. default:
  153. }
  154. close(q.terminated)
  155. q.lock.Unlock()
  156. if log.IsDebug() {
  157. log.Debug("%s: %s Closing with %d tasks left in queue", q.typ, q.name, q.byteFIFO.Len())
  158. }
  159. if err := q.byteFIFO.Close(); err != nil {
  160. log.Error("Error whilst closing internal byte fifo in %s: %s: %v", q.typ, q.name, err)
  161. }
  162. log.Debug("%s: %s Terminated", q.typ, q.name)
  163. }
  164. var _ (UniqueQueue) = &ByteFIFOUniqueQueue{}
  165. // ByteFIFOUniqueQueue represents a UniqueQueue formed from a UniqueByteFifo
  166. type ByteFIFOUniqueQueue struct {
  167. ByteFIFOQueue
  168. }
  169. // NewByteFIFOUniqueQueue creates a new ByteFIFOUniqueQueue
  170. func NewByteFIFOUniqueQueue(typ Type, byteFIFO UniqueByteFIFO, handle HandlerFunc, cfg, exemplar interface{}) (*ByteFIFOUniqueQueue, error) {
  171. configInterface, err := toConfig(ByteFIFOQueueConfiguration{}, cfg)
  172. if err != nil {
  173. return nil, err
  174. }
  175. config := configInterface.(ByteFIFOQueueConfiguration)
  176. return &ByteFIFOUniqueQueue{
  177. ByteFIFOQueue: ByteFIFOQueue{
  178. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  179. byteFIFO: byteFIFO,
  180. typ: typ,
  181. closed: make(chan struct{}),
  182. terminated: make(chan struct{}),
  183. exemplar: exemplar,
  184. workers: config.Workers,
  185. name: config.Name,
  186. },
  187. }, nil
  188. }
  189. // Has checks if the provided data is in the queue
  190. func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) {
  191. if !assignableTo(data, q.exemplar) {
  192. return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name)
  193. }
  194. bs, err := json.Marshal(data)
  195. if err != nil {
  196. return false, err
  197. }
  198. return q.byteFIFO.(UniqueByteFIFO).Has(bs)
  199. }