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