A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

237 lines
5.9 KiB

  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. // IsShutdown returns a channel which is closed when this Queue is shutdown
  144. func (q *ByteFIFOQueue) IsShutdown() <-chan struct{} {
  145. return q.closed
  146. }
  147. // Terminate this queue and close the queue
  148. func (q *ByteFIFOQueue) Terminate() {
  149. log.Trace("%s: %s Terminating", q.typ, q.name)
  150. q.Shutdown()
  151. q.lock.Lock()
  152. select {
  153. case <-q.terminated:
  154. q.lock.Unlock()
  155. return
  156. default:
  157. }
  158. close(q.terminated)
  159. q.lock.Unlock()
  160. if log.IsDebug() {
  161. log.Debug("%s: %s Closing with %d tasks left in queue", q.typ, q.name, q.byteFIFO.Len())
  162. }
  163. if err := q.byteFIFO.Close(); err != nil {
  164. log.Error("Error whilst closing internal byte fifo in %s: %s: %v", q.typ, q.name, err)
  165. }
  166. log.Debug("%s: %s Terminated", q.typ, q.name)
  167. }
  168. // IsTerminated returns a channel which is closed when this Queue is terminated
  169. func (q *ByteFIFOQueue) IsTerminated() <-chan struct{} {
  170. return q.terminated
  171. }
  172. var _ (UniqueQueue) = &ByteFIFOUniqueQueue{}
  173. // ByteFIFOUniqueQueue represents a UniqueQueue formed from a UniqueByteFifo
  174. type ByteFIFOUniqueQueue struct {
  175. ByteFIFOQueue
  176. }
  177. // NewByteFIFOUniqueQueue creates a new ByteFIFOUniqueQueue
  178. func NewByteFIFOUniqueQueue(typ Type, byteFIFO UniqueByteFIFO, handle HandlerFunc, cfg, exemplar interface{}) (*ByteFIFOUniqueQueue, error) {
  179. configInterface, err := toConfig(ByteFIFOQueueConfiguration{}, cfg)
  180. if err != nil {
  181. return nil, err
  182. }
  183. config := configInterface.(ByteFIFOQueueConfiguration)
  184. return &ByteFIFOUniqueQueue{
  185. ByteFIFOQueue: ByteFIFOQueue{
  186. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  187. byteFIFO: byteFIFO,
  188. typ: typ,
  189. closed: make(chan struct{}),
  190. terminated: make(chan struct{}),
  191. exemplar: exemplar,
  192. workers: config.Workers,
  193. name: config.Name,
  194. },
  195. }, nil
  196. }
  197. // Has checks if the provided data is in the queue
  198. func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) {
  199. if !assignableTo(data, q.exemplar) {
  200. return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name)
  201. }
  202. bs, err := json.Marshal(data)
  203. if err != nil {
  204. return false, err
  205. }
  206. return q.byteFIFO.(UniqueByteFIFO).Has(bs)
  207. }