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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "fmt"
  8. "sync"
  9. "time"
  10. "code.gitea.io/gitea/modules/log"
  11. jsoniter "github.com/json-iterator/go"
  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. json := jsoniter.ConfigCompatibleWithStandardLibrary
  64. bs, err := json.Marshal(data)
  65. if err != nil {
  66. return err
  67. }
  68. return q.byteFIFO.PushFunc(bs, fn)
  69. }
  70. // IsEmpty checks if the queue is empty
  71. func (q *ByteFIFOQueue) IsEmpty() bool {
  72. q.lock.Lock()
  73. defer q.lock.Unlock()
  74. if !q.WorkerPool.IsEmpty() {
  75. return false
  76. }
  77. return q.byteFIFO.Len() == 0
  78. }
  79. // Run runs the bytefifo queue
  80. func (q *ByteFIFOQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
  81. atShutdown(context.Background(), q.Shutdown)
  82. atTerminate(context.Background(), q.Terminate)
  83. log.Debug("%s: %s Starting", q.typ, q.name)
  84. go func() {
  85. _ = q.AddWorkers(q.workers, 0)
  86. }()
  87. go q.readToChan()
  88. log.Trace("%s: %s Waiting til closed", q.typ, q.name)
  89. <-q.closed
  90. log.Trace("%s: %s Waiting til done", q.typ, q.name)
  91. q.Wait()
  92. log.Trace("%s: %s Waiting til cleaned", q.typ, q.name)
  93. ctx, cancel := context.WithCancel(context.Background())
  94. atTerminate(ctx, cancel)
  95. q.CleanUp(ctx)
  96. cancel()
  97. }
  98. func (q *ByteFIFOQueue) readToChan() {
  99. for {
  100. select {
  101. case <-q.closed:
  102. // tell the pool to shutdown.
  103. q.cancel()
  104. return
  105. default:
  106. q.lock.Lock()
  107. bs, err := q.byteFIFO.Pop()
  108. if err != nil {
  109. q.lock.Unlock()
  110. log.Error("%s: %s Error on Pop: %v", q.typ, q.name, err)
  111. time.Sleep(time.Millisecond * 100)
  112. continue
  113. }
  114. if len(bs) == 0 {
  115. q.lock.Unlock()
  116. time.Sleep(time.Millisecond * 100)
  117. continue
  118. }
  119. data, err := unmarshalAs(bs, q.exemplar)
  120. if err != nil {
  121. log.Error("%s: %s Failed to unmarshal with error: %v", q.typ, q.name, err)
  122. q.lock.Unlock()
  123. time.Sleep(time.Millisecond * 100)
  124. continue
  125. }
  126. log.Trace("%s %s: Task found: %#v", q.typ, q.name, data)
  127. q.WorkerPool.Push(data)
  128. q.lock.Unlock()
  129. }
  130. }
  131. }
  132. // Shutdown processing from this queue
  133. func (q *ByteFIFOQueue) Shutdown() {
  134. log.Trace("%s: %s Shutting down", q.typ, q.name)
  135. q.lock.Lock()
  136. select {
  137. case <-q.closed:
  138. default:
  139. close(q.closed)
  140. }
  141. q.lock.Unlock()
  142. log.Debug("%s: %s Shutdown", q.typ, q.name)
  143. }
  144. // IsShutdown returns a channel which is closed when this Queue is shutdown
  145. func (q *ByteFIFOQueue) IsShutdown() <-chan struct{} {
  146. return q.closed
  147. }
  148. // Terminate this queue and close the queue
  149. func (q *ByteFIFOQueue) Terminate() {
  150. log.Trace("%s: %s Terminating", q.typ, q.name)
  151. q.Shutdown()
  152. q.lock.Lock()
  153. select {
  154. case <-q.terminated:
  155. q.lock.Unlock()
  156. return
  157. default:
  158. }
  159. close(q.terminated)
  160. q.lock.Unlock()
  161. if log.IsDebug() {
  162. log.Debug("%s: %s Closing with %d tasks left in queue", q.typ, q.name, q.byteFIFO.Len())
  163. }
  164. if err := q.byteFIFO.Close(); err != nil {
  165. log.Error("Error whilst closing internal byte fifo in %s: %s: %v", q.typ, q.name, err)
  166. }
  167. log.Debug("%s: %s Terminated", q.typ, q.name)
  168. }
  169. // IsTerminated returns a channel which is closed when this Queue is terminated
  170. func (q *ByteFIFOQueue) IsTerminated() <-chan struct{} {
  171. return q.terminated
  172. }
  173. var _ UniqueQueue = &ByteFIFOUniqueQueue{}
  174. // ByteFIFOUniqueQueue represents a UniqueQueue formed from a UniqueByteFifo
  175. type ByteFIFOUniqueQueue struct {
  176. ByteFIFOQueue
  177. }
  178. // NewByteFIFOUniqueQueue creates a new ByteFIFOUniqueQueue
  179. func NewByteFIFOUniqueQueue(typ Type, byteFIFO UniqueByteFIFO, handle HandlerFunc, cfg, exemplar interface{}) (*ByteFIFOUniqueQueue, error) {
  180. configInterface, err := toConfig(ByteFIFOQueueConfiguration{}, cfg)
  181. if err != nil {
  182. return nil, err
  183. }
  184. config := configInterface.(ByteFIFOQueueConfiguration)
  185. return &ByteFIFOUniqueQueue{
  186. ByteFIFOQueue: ByteFIFOQueue{
  187. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  188. byteFIFO: byteFIFO,
  189. typ: typ,
  190. closed: make(chan struct{}),
  191. terminated: make(chan struct{}),
  192. exemplar: exemplar,
  193. workers: config.Workers,
  194. name: config.Name,
  195. },
  196. }, nil
  197. }
  198. // Has checks if the provided data is in the queue
  199. func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) {
  200. if !assignableTo(data, q.exemplar) {
  201. return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name)
  202. }
  203. json := jsoniter.ConfigCompatibleWithStandardLibrary
  204. bs, err := json.Marshal(data)
  205. if err != nil {
  206. return false, err
  207. }
  208. return q.byteFIFO.(UniqueByteFIFO).Has(bs)
  209. }