-
Notifications
You must be signed in to change notification settings - Fork 365
/
read.go
53 lines (44 loc) · 1.39 KB
/
read.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
package subscriber
import (
"time"
"github.com/ThreeDotsLabs/watermill/message"
)
// BulkRead reads provided amount of messages from the provided channel, until a timeout occurrs or the limit is reached.
func BulkRead(messagesCh <-chan *message.Message, limit int, timeout time.Duration) (receivedMessages message.Messages, all bool) {
MessagesLoop:
for len(receivedMessages) < limit {
select {
case msg, ok := <-messagesCh:
if !ok {
break MessagesLoop
}
receivedMessages = append(receivedMessages, msg)
msg.Ack()
case <-time.After(timeout):
break MessagesLoop
}
}
return receivedMessages, len(receivedMessages) == limit
}
// BulkReadWithDeduplication reads provided number of messages from the provided channel, ignoring duplicates,
// until a timeout occurrs or the limit is reached.
func BulkReadWithDeduplication(messagesCh <-chan *message.Message, limit int, timeout time.Duration) (receivedMessages message.Messages, all bool) {
receivedIDs := map[string]struct{}{}
MessagesLoop:
for len(receivedMessages) < limit {
select {
case msg, ok := <-messagesCh:
if !ok {
break MessagesLoop
}
if _, ok := receivedIDs[msg.UUID]; !ok {
receivedIDs[msg.UUID] = struct{}{}
receivedMessages = append(receivedMessages, msg)
}
msg.Ack()
case <-time.After(timeout):
break MessagesLoop
}
}
return receivedMessages, len(receivedMessages) == limit
}