forked from wasmCloud/provider-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
404 lines (333 loc) · 9.92 KB
/
provider.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package provider
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"time"
nats "github.com/nats-io/nats.go"
wrpcnats "github.com/wrpc/wrpc/go/nats"
)
type WasmcloudProvider struct {
Id string
context context.Context
cancel context.CancelFunc
Logger *slog.Logger
hostData HostData
Topics Topics
RPCClient *wrpcnats.Client
natsConnection *nats.Conn
natsSubscriptions map[string]*nats.Subscription
healthMsgFunc func() string
shutdownFunc func() error
shutdown chan struct{}
putSourceLinkFunc func(InterfaceLinkDefinition) error
putTargetLinkFunc func(InterfaceLinkDefinition) error
delSourceLinkFunc func(InterfaceLinkDefinition) error
delTargetLinkFunc func(InterfaceLinkDefinition) error
lock sync.Mutex
// Links from the provider to other components, aka where the provider is the
// source of the link. Indexed by the component ID of the target
sourceLinks map[string]InterfaceLinkDefinition
// Links from other components to the provider, aka where the provider is the
// target of the link. Indexed by the component ID of the source
targetLinks map[string]InterfaceLinkDefinition
}
func New(options ...ProviderHandler) (*WasmcloudProvider, error) {
reader := bufio.NewReader(os.Stdin)
// Make a channel to receive the host data so we can timeout if we don't receive it
// All host data is sent immediately after the provider starts
hostDataChannel := make(chan string, 1)
go func() {
hostDataRaw, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
hostDataChannel <- hostDataRaw
}()
hostData := HostData{}
select {
case hostDataRaw := <-hostDataChannel:
decodedData, err := base64.StdEncoding.DecodeString(hostDataRaw)
if err != nil {
return nil, err
}
err = json.Unmarshal(decodedData, &hostData)
if err != nil {
return nil, err
}
case <-time.After(5 * time.Second):
log.Fatal("failed to read host data, did not receive after 5 seconds")
}
// Initialize Logging
var logger *slog.Logger
if hostData.StructuredLogging {
logger = slog.New(slog.NewJSONHandler(os.Stderr, nil))
} else {
logger = slog.New(slog.NewTextHandler(os.Stderr, nil))
}
// Connect to NATS
nc, err := nats.Connect(hostData.LatticeRPCURL)
if err != nil {
return nil, err
}
// partition links based on if the provider is the source or target
sourceLinks := []InterfaceLinkDefinition{}
targetLinks := []InterfaceLinkDefinition{}
// Loop over the numbers
for _, link := range hostData.LinkDefinitions {
if link.SourceID == hostData.ProviderKey {
sourceLinks = append(sourceLinks, link)
} else if link.Target == hostData.ProviderKey {
targetLinks = append(targetLinks, link)
} else {
logger.Warn("Link %s->%s is not connected to provider, ignoring", link.SourceID, link.Target)
}
}
wrpc := wrpcnats.NewClient(nc, fmt.Sprintf("%s.%s", hostData.LatticeRPCPrefix, hostData.ProviderKey))
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT)
ctx, cancel := context.WithCancel(context.Background())
provider := &WasmcloudProvider{
Id: hostData.ProviderKey,
Logger: logger,
RPCClient: wrpc,
Topics: LatticeTopics(hostData),
context: ctx,
cancel: cancel,
hostData: hostData,
natsConnection: nc,
natsSubscriptions: map[string]*nats.Subscription{},
healthMsgFunc: func() string { return "healthy" },
shutdownFunc: func() error { return nil },
shutdown: make(chan struct{}),
putSourceLinkFunc: func(InterfaceLinkDefinition) error { return nil },
putTargetLinkFunc: func(InterfaceLinkDefinition) error { return nil },
delSourceLinkFunc: func(InterfaceLinkDefinition) error { return nil },
delTargetLinkFunc: func(InterfaceLinkDefinition) error { return nil },
sourceLinks: make(map[string]InterfaceLinkDefinition, len(sourceLinks)),
targetLinks: make(map[string]InterfaceLinkDefinition, len(targetLinks)),
}
for _, opt := range options {
err := opt(provider)
if err != nil {
return nil, err
}
}
for _, link := range sourceLinks {
err := provider.putLink(link)
if err != nil {
logger.Error("failed to put source link", slog.Any("error", err))
}
}
for _, link := range targetLinks {
err := provider.putLink(link)
if err != nil {
logger.Error("failed to put target link", slog.Any("error", err))
}
}
return provider, nil
}
func (wp *WasmcloudProvider) HostData() HostData {
return wp.hostData
}
func (wp *WasmcloudProvider) NatsConnection() *nats.Conn {
return wp.natsConnection
}
func (wp *WasmcloudProvider) OutgoingRpcClient(target string) *wrpcnats.Client {
return wrpcnats.NewClient(wp.natsConnection, fmt.Sprintf("%s.%s", wp.hostData.LatticeRPCPrefix, target))
}
func (wp *WasmcloudProvider) Start() error {
err := wp.subToNats()
if err != nil {
return err
}
wp.Logger.Info("provider started", "id", wp.Id)
<-wp.context.Done()
wp.Logger.Info("provider exiting", "id", wp.Id)
return nil
}
func (wp *WasmcloudProvider) Shutdown() error {
err := wp.shutdownFunc()
if err != nil {
wp.cancel()
return err
}
err = wp.cleanupNatsSubscriptions()
if err != nil {
wp.cancel()
return err
}
wp.cancel()
return nil
}
func (wp *WasmcloudProvider) subToNats() error {
// ------------------ Subscribe to Health topic --------------------
health, err := wp.natsConnection.Subscribe(wp.Topics.LATTICE_HEALTH,
func(m *nats.Msg) {
msg := wp.healthMsgFunc()
hc := HealthCheckResponse{
Healthy: true,
Message: msg,
}
hcBytes, err := json.Marshal(hc)
if err != nil {
wp.Logger.Error("failed to encode health check", slog.Any("error", err))
return
}
err = wp.natsConnection.Publish(m.Reply, hcBytes)
if err != nil {
wp.Logger.Error("failed to publish health check response", slog.Any("error", err))
}
})
if err != nil {
wp.Logger.Error("LATTICE_HEALTH", slog.Any("error", err))
return err
}
wp.natsSubscriptions[wp.Topics.LATTICE_HEALTH] = health
// ------------------ Subscribe to Delete link topic --------------
linkDel, err := wp.natsConnection.Subscribe(wp.Topics.LATTICE_LINK_DEL,
func(m *nats.Msg) {
link := InterfaceLinkDefinition{}
err := json.Unmarshal(m.Data, &link)
if err != nil {
wp.Logger.Error("failed to decode link", slog.Any("error", err))
return
}
err = wp.deleteLink(link)
if err != nil {
// TODO(#10): handle better?
wp.Logger.Error("failed to delete link", slog.Any("error", err))
return
}
})
if err != nil {
wp.Logger.Error("LINK_DEL", slog.Any("error", err))
return err
}
wp.natsSubscriptions[wp.Topics.LATTICE_LINK_DEL] = linkDel
// ------------------ Subscribe to New link topic --------------
linkPut, err := wp.natsConnection.Subscribe(wp.Topics.LATTICE_LINK_PUT,
func(m *nats.Msg) {
link := InterfaceLinkDefinition{}
err := json.Unmarshal(m.Data, &link)
if err != nil {
wp.Logger.Error("failed to decode link", slog.Any("error", err))
return
}
err = wp.putLink(link)
if err != nil {
// TODO(#10): handle this better?
wp.Logger.Error("newLinkFunc", slog.Any("error", err))
}
})
if err != nil {
wp.Logger.Error("LINK_PUT", slog.Any("error", err))
return err
}
wp.natsSubscriptions[wp.Topics.LATTICE_LINK_PUT] = linkPut
// ------------------ Subscribe to Shutdown topic ------------------
shutdown, err := wp.natsConnection.Subscribe(wp.Topics.LATTICE_SHUTDOWN,
func(m *nats.Msg) {
err := wp.shutdownFunc()
if err != nil {
// TODO(#10): handle this better?
log.Println("ERROR: provider shutdown function failed: " + err.Error())
}
err = m.Respond([]byte("provider shutdown handled successfully"))
if err != nil {
// NOTE: This is a log message because we don't want to stop the shutdown process
log.Println("ERROR: provider shutdown failed to respond: " + err.Error())
}
err = wp.cleanupNatsSubscriptions()
if err != nil {
log.Println("ERROR: provider shutdown failed to drain connection: " + err.Error())
}
wp.cancel()
})
if err != nil {
wp.Logger.Error("LATTICE_SHUTDOWN", slog.Any("error", err))
return err
}
wp.natsSubscriptions[wp.Topics.LATTICE_SHUTDOWN] = shutdown
return nil
}
func (wp *WasmcloudProvider) cleanupNatsSubscriptions() error {
err := wp.natsConnection.Flush()
if err != nil {
return err
}
for _, s := range wp.natsSubscriptions {
err := s.Drain()
if err != nil {
// NOTE: This is a log message because we don't want to stop the shutdown process
log.Println("ERROR: provider shutdown failed to drain subscription: " + err.Error())
}
}
return wp.natsConnection.Drain()
}
func (wp *WasmcloudProvider) putLink(l InterfaceLinkDefinition) error {
// Ignore duplicate links
if wp.isLinked(l.SourceID, l.Target) {
wp.Logger.Info("ignoring duplicate link", "link", l)
return nil
}
wp.lock.Lock()
defer wp.lock.Unlock()
if l.SourceID == wp.Id {
err := wp.putSourceLinkFunc(l)
if err != nil {
return err
}
wp.sourceLinks[l.Target] = l
} else if l.Target == wp.Id {
err := wp.putTargetLinkFunc(l)
if err != nil {
return err
}
wp.targetLinks[l.SourceID] = l
} else {
wp.Logger.Info("received link that isn't for this provider, ignoring", "link", l)
}
return nil
}
func (wp *WasmcloudProvider) deleteLink(l InterfaceLinkDefinition) error {
wp.lock.Lock()
defer wp.lock.Unlock()
if l.SourceID == wp.Id {
err := wp.delSourceLinkFunc(l)
if err != nil {
return err
}
delete(wp.sourceLinks, l.Target)
} else if l.Target == wp.Id {
err := wp.delTargetLinkFunc(l)
if err != nil {
return err
}
delete(wp.targetLinks, l.SourceID)
} else {
wp.Logger.Info("received link delete that isn't for this provider, ignoring", "link", l)
}
return nil
}
func (wp *WasmcloudProvider) isLinked(sourceId string, target string) bool {
wp.lock.Lock()
defer wp.lock.Unlock()
if sourceId == wp.Id {
_, exists := wp.sourceLinks[target]
return exists
} else if target == wp.Id {
_, exists := wp.targetLinks[sourceId]
return exists
} else {
return false
}
}