-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
71 lines (60 loc) · 1.48 KB
/
stream.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
package eventsourcing
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
cloudevents "github.com/cloudevents/sdk-go"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
func writeEvents(conn *nats.Conn,
streamName string,
eventSubjectPrefix string,
jsDomain string,
events []cloudevents.Event) error {
var err error
ctx, cancelF := context.WithTimeout(context.Background(), bucketTimeout)
defer cancelF()
var js jetstream.JetStream
if len(jsDomain) == 0 {
js, err = jetstream.New(conn)
} else {
js, err = jetstream.NewWithDomain(conn, jsDomain)
}
if err != nil {
return err
}
_, err = js.Stream(ctx, streamName)
if err != nil {
if errors.Is(err, jetstream.ErrStreamNotFound) {
_, err = js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
Name: streamName,
Subjects: []string{fmt.Sprintf("%s.>", eventSubjectPrefix)},
})
if err != nil {
return err
}
} else {
return err
}
}
for _, event := range events {
outSubject := eventSubject(eventSubjectPrefix, event)
bytes, _ := json.Marshal(event)
err = conn.Publish(outSubject, bytes)
if err != nil {
return err
}
}
return nil
}
func eventSubject(prefix string, event cloudevents.Event) string {
outSubject := fmt.Sprintf("%s.%s", prefix, event.Type())
if ext, ok := event.Extensions()[extensionEntityKey]; ok {
ek := strings.ReplaceAll(ext.(string), ".", "_")
outSubject = fmt.Sprintf("%s.%s.%s", prefix, ek, event.Type())
}
return outSubject
}