-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_queue.go
49 lines (43 loc) · 1.01 KB
/
wp_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package piper
import (
"context"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"sync"
)
func NewWorkerPool(count int, queue string, deliveries <-chan amqp.Delivery) *QueueWorkerPool {
return &QueueWorkerPool{
workersCount: count,
queue: queue,
deliveries: deliveries,
results: make(chan ResultDelivery),
}
}
func (wp QueueWorkerPool) RunWorkerPool(ctx context.Context) {
var wg sync.WaitGroup
wg.Add(wp.workersCount)
for i := 0; i < wp.workersCount; i++ {
go wp.workerProcessing(ctx, i, &wg)
}
wg.Wait()
close(wp.results)
}
func (wp QueueWorkerPool) Results() <-chan ResultDelivery {
return wp.results
}
func (wp QueueWorkerPool) workerProcessing(ctx context.Context, numWorker int, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case delivery, ok := <-wp.deliveries:
if !ok {
log.Printf("[AMQP WORKER POOL] workerProcessing is close exit (%s)", numWorker)
return
}
wp.results <- ResultDelivery{
WorkerId: numWorker,
Delivery: &delivery,
}
}
}
}