-
Notifications
You must be signed in to change notification settings - Fork 1
/
events_controller.go
196 lines (164 loc) · 5.19 KB
/
events_controller.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
package controller
import (
"encoding/json"
"fmt"
"golang.org/x/net/websocket"
"github.com/labstack/echo/v4"
"github.com/twuillemin/kuboxy/pkg/types"
)
// CommandType is the type of command sent to the EventListener
type CommandType string
const (
// AddSource is the command for adding a new source
AddSource CommandType = "AddSource"
// RemoveSource is the command for deleting a source
RemoveSource CommandType = "RemoveSource"
// RemoveAllSources is the command for deleting all sources
RemoveAllSources CommandType = "RemoveAllSources"
)
// Command is a command received by the event controller
type Command struct {
Command CommandType `json:"command"`
ObjectType types.ObjectType `json:"objectType,omitempty"`
ContextName string `json:"contextName,omitempty"`
NamespaceName string `json:"namespaceName,omitempty"`
}
// The source of an event
type eventSource struct {
ObjectType types.ObjectType
ContextName string
NamespaceName string
}
// The information related to a forwarder
type forwarderInformation struct {
source eventSource
stopForwarderChannel chan struct{}
}
// getEventsByWebSocket create the persistent websocket between a client and the server
// @Summary Connect to a WebSocket for managing events (port 8081)
// @Description the websocket used for receiving the configuration and then return the requested events. Each event is a full object when created / updated / deleted
// @ID get-events-by-websocket
// @Tags Events
// @Produce text/plain
// @Param contextName path string true "the name of the configuration"
// @Success 200 {string} string
// @Failure 404 {object} HTTPError
// @Router /api/v1/events/ [get]
func getEventsByWebSocket(c echo.Context) (err error) {
websocket.Handler(func(ws *websocket.Conn) {
defer func() {
err = ws.Close()
}()
forwarders := make([]*forwarderInformation, 0)
var stopFlag struct{}
sendMessageChannel := make(chan interface{})
stopSendingChannel := make(chan struct{})
stopHandlerChannel := make(chan struct{})
// Start a goroutine for receiving client
go func() {
// Wait client message indefinitely
for {
msg := ""
err = websocket.Message.Receive(ws, &msg)
if err == nil {
forwarders = processCommand(c, msg, forwarders, sendMessageChannel)
} else {
if err.Error() != "EOF" {
c.Logger().Error(err)
}
stopSendingChannel <- stopFlag
break
}
}
}()
// Start a goroutine for sending to the client
go func() {
// Loop endlessly
sendLoop:
for {
// Block until we receive from one of following events
select {
case message := <-sendMessageChannel:
jsonMessage, _ := json.Marshal(message)
err = websocket.Message.Send(ws, string(jsonMessage))
if err != nil {
if err.Error() != "EOF" {
c.Logger().Error(err)
}
break sendLoop
}
case <-stopSendingChannel:
break sendLoop
}
}
stopHandlerChannel <- stopFlag
}()
// Wait for the sending routine to terminate
<-stopHandlerChannel
// Stop all forwarders if any remaining
forwarders = stopAllForwarders(forwarders)
}).ServeHTTP(c.Response(), c.Request())
return nil
}
// Process a command
func processCommand(c echo.Context, message string, forwarders []*forwarderInformation, sendMessageChannel chan interface{}) []*forwarderInformation {
c.Logger().Info("Processing message: %s\n", message)
commandReceived := new(Command)
err := json.Unmarshal([]byte(message), &commandReceived)
if err != nil {
c.Logger().Error(err)
return forwarders
}
source := eventSource{
commandReceived.ObjectType,
commandReceived.ContextName,
commandReceived.NamespaceName,
}
switch commandReceived.Command {
case AddSource:
// Ensure previous provider does not exist
var idxForwarder = getExistingProviderIndex(forwarders, source)
if idxForwarder == -1 {
forwarders, err = addEventSource(source, forwarders, sendMessageChannel)
if err != nil {
c.Logger().Error(err)
}
} else {
c.Logger().Warn(fmt.Sprintf("unable to add the source %v is already registered", source))
}
case RemoveSource:
// Search previous forwarder
var idxForwarder = getExistingProviderIndex(forwarders, source)
// If previous forwarder found
if idxForwarder > -1 {
var stopFlag struct{}
// Stop the forwarder
forwarders[idxForwarder].stopForwarderChannel <- stopFlag
// Remove from list (don't preserve the order)
forwarders[idxForwarder] = forwarders[len(forwarders)-1]
forwarders = forwarders[:len(forwarders)-1]
}
case RemoveAllSources:
forwarders = stopAllForwarders(forwarders)
}
return forwarders
}
// stopAllForwarders stops all forwarders if any
func stopAllForwarders(forwarders []*forwarderInformation) []*forwarderInformation {
var stopFlag struct{}
for _, forwarder := range forwarders {
// Stop the forwarder
forwarder.stopForwarderChannel <- stopFlag
}
return make([]*forwarderInformation, 0)
}
// stopAllForwarders stops all forwarders if any
func getExistingProviderIndex(forwarders []*forwarderInformation, source eventSource) int {
// Search previous forwarder
for i := 0; i < len(forwarders); i++ {
if forwarders[i].source == source {
return i
}
}
return -1
}