-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnotifier.go
209 lines (183 loc) · 5.51 KB
/
notifier.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2025 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
// Package azservicebus provides a notifier that subscribes to an Service Bus topic that watches change of configuration on Azure.
//
// It requires following roles:
// - Azure Service Bus Data Owner
// - Azure Service Bus Data Receiver
package azservicebus
import (
"context"
"errors"
"fmt"
"log/slog"
"reflect"
"slices"
"sync"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/admin"
"github.com/google/uuid"
)
// Notifier that watches change events on given Service Bus topic.
//
// To create a new Notifier, call [NewNotifier].
type Notifier struct {
namespace string
topic string
credential azcore.TokenCredential
logger *slog.Logger
loaders []loader
loadersMutex sync.RWMutex
}
type loader interface {
OnEvent(messaging.CloudEvent) error
}
// NewNotifier creates a Notifier with the given Service Bus namespace and topic.
func NewNotifier(namespace, topic string, opts ...Option) *Notifier {
option := &options{
namespace: namespace,
topic: topic,
// Place holder for the default credential.
credential: &azidentity.DefaultAzureCredential{},
}
for _, opt := range opts {
opt(option)
}
return (*Notifier)(option)
}
// Register registers a loader to the Notifier.
func (n *Notifier) Register(loaders ...loader) {
if n == nil {
return
}
n.loadersMutex.Lock()
defer n.loadersMutex.Unlock()
n.loaders = append(n.loaders, loaders...)
}
var errNil = errors.New("nil Notifier")
// Start starts watching events on given Service Bus topic and fanout to registered loaders.
// It blocks until ctx is done, or it returns an error.
func (n *Notifier) Start(ctx context.Context) error { //nolint:cyclop,funlen,gocognit
if n == nil {
return errNil
}
if token, ok := n.credential.(*azidentity.DefaultAzureCredential); ok && reflect.ValueOf(*token).IsZero() {
var err error
if n.credential, err = azidentity.NewDefaultAzureCredential(nil); err != nil {
return fmt.Errorf("load default Azure credential: %w", err)
}
}
logger := n.logger
if n.logger == nil {
logger = slog.Default()
}
adminClient, err := admin.NewClient(n.namespace, n.credential, nil)
if err != nil {
return fmt.Errorf("create Azure Service Bus admin client: %w", err)
}
subscription, err := adminClient.CreateSubscription(ctx, n.topic, "konf-"+uuid.NewString(), nil)
if err != nil {
return fmt.Errorf("create Azure Service Bus subscription: %w", err)
}
subscriptionName := subscription.SubscriptionName
defer func() {
if _, derr := adminClient.DeleteSubscription(context.WithoutCancel(ctx),
n.topic, subscriptionName, nil,
); derr != nil {
logger.LogAttrs(ctx, slog.LevelWarn,
"Fail to delete service bus subscription.",
slog.String("topic", n.topic),
slog.String("subscription", subscriptionName),
slog.Any("error", derr),
)
}
}()
client, err := azservicebus.NewClient(n.namespace, n.credential, nil)
if err != nil {
return fmt.Errorf("create Azure Service Bus client: %w", err)
}
receiver, err := client.NewReceiverForSubscription(n.topic, subscriptionName, &azservicebus.ReceiverOptions{
ReceiveMode: azservicebus.ReceiveModeReceiveAndDelete,
})
if err != nil {
return fmt.Errorf("create Azure Service Bus receiver: %w", err)
}
logger.LogAttrs(ctx, slog.LevelInfo,
"Start watching service bus topic.",
slog.String("topic", n.topic),
slog.String("subscription", subscriptionName),
)
timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-timer.C:
messages, err := receiver.ReceiveMessages(ctx, 10, nil) //nolint:mnd // default maximum.
if err != nil {
if !errors.Is(err, context.Canceled) {
logger.LogAttrs(ctx, slog.LevelWarn,
"Fail to receive service bus message.",
slog.String("subscription", subscriptionName),
slog.Any("error", err),
)
}
timer.Reset(20 * time.Second) //nolint:mnd // Retry after 20 seconds to avoid busy loop.
continue
}
timer.Reset(time.Second) // Reset timer for next polling.
if len(messages) == 0 {
continue
}
logger.LogAttrs(ctx, slog.LevelInfo,
"Received messages from service bus topic.",
slog.String("topic", n.topic),
slog.Int("count", len(messages)),
)
n.loadersMutex.RLock()
loaders := slices.Clone(n.loaders)
n.loadersMutex.RUnlock()
for _, msg := range messages {
if len(msg.Body) == 0 {
continue
}
var event messaging.CloudEvent
if err := event.UnmarshalJSON(msg.Body); err != nil {
logger.LogAttrs(ctx, slog.LevelWarn,
"Fail to unmarshal message.",
slog.String("msg", string(msg.Body)),
slog.Any("error", err),
)
continue
}
var errM error
for _, loader := range loaders {
errM = loader.OnEvent(event)
if errors.Is(errM, errors.ErrUnsupported) {
continue
}
if errM != nil {
logger.LogAttrs(ctx, slog.LevelWarn,
"Fail to process message.",
slog.Any("event", event),
slog.Any("loader", loader),
slog.Any("error", errM),
)
}
break
}
if errors.Is(errM, errors.ErrUnsupported) {
logger.LogAttrs(ctx, slog.LevelWarn,
"No loader to process message.",
slog.Any("event", event),
)
}
}
}
}
}