-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscriber.go
More file actions
182 lines (148 loc) · 4.53 KB
/
subscriber.go
File metadata and controls
182 lines (148 loc) · 4.53 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package pubsub
import (
"cloud.google.com/go/pubsub"
"context"
"fmt"
"github.com/xgodev/boost/bootstrap/function"
"github.com/xgodev/boost/model/errors"
"github.com/xgodev/boost/wrapper/log"
"github.com/xgodev/boost/wrapper/log/contrib/rs/zerolog/v1"
"math"
"time"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/google/uuid"
)
// Subscriber contains the Pub/Sub client, handler function, and options
type Subscriber[T any] struct {
client *pubsub.Client
handler function.Handler[T]
subscription string
options *Options
}
// NewSubscriber returns a subscriber listener.
func NewSubscriber[T any](client *pubsub.Client, handler function.Handler[T], subscription string, options *Options) *Subscriber[T] {
return &Subscriber[T]{
client: client,
handler: handler,
subscription: subscription,
options: options,
}
}
// Subscribe subscribes and consumes messages from multiple Pub/Sub topics concurrently
func (l *Subscriber[T]) Subscribe(ctx context.Context) error {
logger := log.FromContext(ctx).WithTypeOf(*l)
logger.Tracef("pubsub - Subscribing to %s", l.subscription)
subscription := l.client.Subscription(l.subscription)
subscription.ReceiveSettings = pubsub.ReceiveSettings{
MaxOutstandingMessages: int(l.options.Concurrency),
}
err := subscription.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
err := l.processMessage(ctx, msg)
if err != nil {
log.Errorf("processing failed: %v", err)
msg.Nack()
}
})
if err != nil {
logger.Fatalf("Failed to start subscription %s: %v", l.subscription, err)
}
return nil
}
// processMessage processes each message, retries if needed, and applies backoff
func (l *Subscriber[T]) processMessage(ctx context.Context, msg *pubsub.Message) error {
ctx = zerolog.NewLogger().ToContext(ctx)
retryCount := 0
in, err := l.generateCloudEvent(msg)
if err != nil {
msg.Nack()
return errors.Wrap(err, errors.Internalf("could not generate CloudEvent: %s", err.Error()))
}
for {
// Timeout por tentativa
msgCtx, cancel := context.WithTimeout(ctx, l.options.ProcessTimeout)
// Processes the event via handler
if _, err := l.handler(msgCtx, in); err != nil {
cancel()
retryCount++
log.Ctx(ctx, *l).Warnf("msgID=%s handler failed (attempt %d/%d): %v\nPayload: %s", msg.ID, retryCount, l.options.RetryLimit, err, string(msg.Data))
// Check retry limit
if l.options.RetryLimit != -1 && retryCount >= l.options.RetryLimit {
return errors.Wrap(err, errors.Internalf("max retry limit reached"))
}
// Apply backoff if enabled
if l.options.Backoff {
l.applyBackoff(retryCount)
}
// Retry processing the message
continue
}
cancel()
msg.Ack()
break
}
return nil
}
func (l *Subscriber[T]) generateCloudEvent(msg *pubsub.Message) (event.Event, error) {
in := event.New()
ce := false
contentType := "application/json"
// Checks attributes and transforms into a CloudEvent if applicable
for key, value := range msg.Attributes {
switch key {
case "content-type":
in.SetDataContentType(value)
contentType = value
case "ce_specversion":
in.SetSpecVersion(value)
ce = true
case "ce_id":
in.SetID(value)
ce = true
case "ce_source":
in.SetSource(value)
ce = true
case "ce_type":
in.SetType(value)
ce = true
case "ce_time":
ce = true
if t, err := time.Parse(time.RFC3339, value); err == nil {
in.SetTime(t)
}
case "ce_subject":
ce = true
in.SetSubject(value)
default:
in.SetExtension(key, value)
}
}
// If the event does not have a time, populate it with the time the message was published
if in.Time().IsZero() {
in.SetTime(msg.PublishTime)
}
// If it's not a CloudEvent, create one manually
if !ce {
in.SetID(uuid.NewString())
in.SetSource(fmt.Sprintf("pubsub://%s", l.subscription))
in.SetType("pubsub.message")
}
// Set the message body as CloudEvent data
if err := in.SetData(contentType, msg.Data); err != nil {
return event.Event{}, errors.Wrap(err, errors.Internalf("could not set data from pubsub message: %s", err.Error()))
}
/*
if err := in.Validate(); err != nil {
return event.Event{}, errors.Wrap(err, errors.Internalf("invalid CloudEvent: %s", err.Error()))
}
*/
return in, nil
}
// applyBackoff applies an exponential backoff strategy
func (l *Subscriber[T]) applyBackoff(retryCount int) {
backoffTime := time.Duration(math.Pow(2, float64(retryCount))) * l.options.BackoffBase
// Cap the backoff time
if backoffTime > l.options.MaxBackoff {
backoffTime = l.options.MaxBackoff
}
time.Sleep(backoffTime)
}