-
Notifications
You must be signed in to change notification settings - Fork 10
/
consumer.go
124 lines (99 loc) · 3.04 KB
/
consumer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"errors"
"fmt"
)
const (
eventTypePublish eventType = iota
eventTypeNack
eventTypeBack
eventTypeMsgReturned
)
type eventType int
type notifier interface {
NotifyConsumer(topic string, ev eventType)
}
// consumer handles providing values iteratively to a single consumer. Methods
// on a consumer are not thread safe as operations should occur serially.
type consumer struct {
id string
topic string
ackOffset int
store storer
eventChan chan eventType
notifier notifier
outstanding bool // indicates whether the consumer has an outstanding message to ack
}
func (c *consumer) String() string {
return fmt.Sprintf("consumer{id: %s}", c.id)
}
// Next will attempt to retrieve the next value on the topic, or it will
// block waiting for a msg indicating there is a new value available.
func (c *consumer) Next(ctx context.Context) (val *value, err error) {
// Prevent Next from being called if the consumer already has one outstanding
// unacknowledged message.
if c.outstanding {
return nil, errors.New("unacknowledged message outstanding")
}
var ao int
// Repeat trying to get the next value while the topic is either empty or not
// created yet. It may exist sometime in the future.
for {
val, ao, err = c.store.GetNext(c.topic)
if !errors.Is(err, errTopicEmpty) && !errors.Is(err, errTopicNotExist) {
break
}
select {
case <-c.eventChan:
case <-ctx.Done():
return nil, errRequestCancelled
}
}
if err != nil {
return nil, fmt.Errorf("getting next from store: %v", err)
}
c.ackOffset = ao
c.outstanding = true
return val, err
}
// Ack acknowledges the previously consumed value.
func (c *consumer) Ack() error {
if err := c.store.Ack(c.topic, c.ackOffset); err != nil {
return fmt.Errorf("acking topic %s with offset %d: %v", c.topic, c.ackOffset, err)
}
c.outstanding = false
return nil
}
// Nack negatively acknowledges a message, returning it for consumption by other
// consumers.
func (c *consumer) Nack() error {
if err := c.store.Nack(c.topic, c.ackOffset); err != nil {
return fmt.Errorf("nacking topic %s with offset %d: %w", c.topic, c.ackOffset, err)
}
c.outstanding = false
c.notifier.NotifyConsumer(c.topic, eventTypeNack)
return nil
}
// Back negatively acknowledges a message, returning it to the back of the queue
// for consumption.
func (c *consumer) Back() error {
if err := c.store.Back(c.topic, c.ackOffset); err != nil {
return fmt.Errorf("backing topic %s with offset %d: %v", c.topic, c.ackOffset, err)
}
c.outstanding = false
c.notifier.NotifyConsumer(c.topic, eventTypeBack)
return nil
}
func (c *consumer) Dack(delaySeconds int) error {
if err := c.store.Dack(c.topic, c.ackOffset, delaySeconds); err != nil {
return fmt.Errorf("dacking topic %s with offset %d and delay %ds: %v", c.topic, c.ackOffset, delaySeconds, err)
}
c.outstanding = false
return nil
}
// EventChan returns a channel to notify the consumer of events occurring on the
// topic.
func (c *consumer) EventChan() <-chan eventType {
return c.eventChan
}