forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (49 loc) · 1.12 KB
/
main.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
50
51
52
53
54
55
56
package main
import (
"fmt"
"log"
"time"
"github.com/adjust/rmq"
)
const (
unackedLimit = 1000
numConsumers = 10
batchSize = 1000
)
func main() {
connection := rmq.OpenConnection("consumer", "tcp", "localhost:6379", 2)
queue := connection.OpenQueue("things")
queue.StartConsuming(unackedLimit, 500*time.Millisecond)
for i := 0; i < numConsumers; i++ {
name := fmt.Sprintf("consumer %d", i)
queue.AddConsumer(name, NewConsumer(i))
}
select {}
}
type Consumer struct {
name string
count int
before time.Time
}
func NewConsumer(tag int) *Consumer {
return &Consumer{
name: fmt.Sprintf("consumer%d", tag),
count: 0,
before: time.Now(),
}
}
func (consumer *Consumer) Consume(delivery rmq.Delivery) {
consumer.count++
if consumer.count%batchSize == 0 {
duration := time.Now().Sub(consumer.before)
consumer.before = time.Now()
perSecond := time.Second / (duration / batchSize)
log.Printf("%s consumed %d %s %d", consumer.name, consumer.count, delivery.Payload(), perSecond)
}
time.Sleep(time.Millisecond)
if consumer.count%batchSize == 0 {
delivery.Reject()
} else {
delivery.Ack()
}
}