This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathnotifications.go
More file actions
192 lines (167 loc) · 4.41 KB
/
notifications.go
File metadata and controls
192 lines (167 loc) · 4.41 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
183
184
185
186
187
188
189
190
191
192
package sse
import (
"context"
"net/http"
"sync"
"time"
"k8s.io/klog/v2"
openapi "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
type Notifier struct {
fsys filesystem.Filesystem
devfilePath string
// eventsChan is a channel for all events that will be broadcast to all subscribers.
// Because it is not natively possible to read a same value twice from a Go channel,
// we are storing the list of channels to broadcast the event to into the subscribers list.
eventsChan chan Event
// subscribers is a list of all channels where any event from eventsChan will be broadcast to.
subscribers []chan Event
// newSubscriptionChan is a channel where new subscribers can register the channel on which they wish to be notified.
// Such channels are stored into the subscribers list.
newSubscriptionChan chan chan Event
// cancelSubscriptionChan is a write-only channel where subscribers can cancel their registration and stop being broadcast new events coming from eventsChan.
cancelSubscriptionChan chan (<-chan Event)
}
func NewNotifier(ctx context.Context, fsys filesystem.Filesystem, devfilePath string, devfileFiles []string) (*Notifier, error) {
notifier := Notifier{
fsys: fsys,
devfilePath: devfilePath,
eventsChan: make(chan Event),
subscribers: make([]chan Event, 0),
newSubscriptionChan: make(chan chan Event),
cancelSubscriptionChan: make(chan (<-chan Event)),
}
err := notifier.watchDevfileChanges(ctx, devfileFiles)
if err != nil {
return nil, err
}
go notifier.manageSubscriptions(ctx)
// Heartbeat as a keep-alive mechanism to prevent some clients from closing inactive connections (notifications might not be sent regularly).
go func() {
ticker := time.NewTicker(7 * time.Second)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
notifier.eventsChan <- Event{
eventType: Heartbeat,
}
}
}
}()
return ¬ifier, nil
}
func (n *Notifier) manageSubscriptions(ctx context.Context) {
defer func() {
for _, listener := range n.subscribers {
if listener != nil {
close(listener)
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
case newSubscriber := <-n.newSubscriptionChan:
n.subscribers = append(n.subscribers, newSubscriber)
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
case subscriberToRemove := <-n.cancelSubscriptionChan:
for i, ch := range n.subscribers {
if ch == subscriberToRemove {
n.subscribers[i] = n.subscribers[len(n.subscribers)-1]
n.subscribers = n.subscribers[:len(n.subscribers)-1]
close(ch)
break
}
}
}
}
}()
for {
select {
case <-ctx.Done():
return
case val, ok := <-n.eventsChan:
if !ok {
return
}
var wg sync.WaitGroup
for _, subscriber := range n.subscribers {
subscriber := subscriber
if subscriber == nil {
continue
}
wg.Add(1)
go func() {
defer wg.Done()
select {
case subscriber <- val:
case <-ctx.Done():
return
}
}()
}
wg.Wait()
}
}
}
func (n *Notifier) Routes() openapi.Routes {
return openapi.Routes{
{
Name: "ServerSentEvents",
Method: http.MethodGet,
Pattern: "/api/v1/notifications",
HandlerFunc: n.handler,
},
}
}
func (n *Notifier) handler(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
newListener := make(chan Event)
n.newSubscriptionChan <- newListener
defer func() {
n.cancelSubscriptionChan <- newListener
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
// Headers sent back as early as possible to clients
flusher.Flush()
for {
select {
case ev := <-newListener:
func() {
defer flusher.Flush()
dataToWrite, err := ev.toSseString()
if err != nil {
klog.V(2).Infof("error writing notification data: %v", err)
return
}
_, err = w.Write([]byte(dataToWrite))
if err != nil {
klog.V(2).Infof("error writing notification data: %v", err)
return
}
}()
case <-r.Context().Done():
klog.V(8).Infof("Connection closed!")
return
}
}
}