-
Notifications
You must be signed in to change notification settings - Fork 8
/
meetings_request_handler.go
296 lines (269 loc) · 7.84 KB
/
meetings_request_handler.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
package requests
import (
"context"
"net/http"
"github.com/b3scale/b3scale/pkg/bbb"
"github.com/b3scale/b3scale/pkg/cluster"
"github.com/b3scale/b3scale/pkg/store"
"github.com/b3scale/b3scale/pkg/templates"
)
// MeetingsHandlerOptions has configuration options for
// this middleware handling all meetings related stuff.
type MeetingsHandlerOptions struct {
// UseReverseProxy will handle the request in a way
// that a reverse proxy can be used. This is an experimental
// feature and a known issue is the unfortunate handling of breakout rooms.
// When deployed in reverse proxy mode we will handle the
// join internally and the proxy needs to handle subsequent requests.
UseReverseProxy bool
}
// MeetingsHandler will handle all meetings related API requests
type MeetingsHandler struct {
opts *MeetingsHandlerOptions
router *cluster.Router
}
// MeetingsRequestHandler creates a new request middleware for handling
// all requests related to meetings.
func MeetingsRequestHandler(
router *cluster.Router,
opts *MeetingsHandlerOptions,
) cluster.RequestMiddleware {
h := &MeetingsHandler{
opts: opts,
router: router,
}
return func(next cluster.RequestHandler) cluster.RequestHandler {
return func(
ctx context.Context,
req *bbb.Request,
) (bbb.Response, error) {
switch req.Resource {
case bbb.ResourceJoin:
return h.Join(ctx, req)
case bbb.ResourceCreate:
return h.Create(ctx, req)
case bbb.ResourceIsMeetingRunning:
return h.IsMeetingRunning(ctx, req)
case bbb.ResourceEnd:
return h.End(ctx, req)
case bbb.ResourceGetMeetingInfo:
return h.GetMeetingInfo(ctx, req)
case bbb.ResourceGetMeetings:
return h.GetMeetings(ctx, req)
}
// Invoke next middlewares
return next(ctx, req)
}
}
}
// Join will try to join the meeting
//
// If the meeting is not yet available we stall. If the backend is not
// available we stall.
// We do this by redirecting to a waiting page and reissue the request
// after some seconds.
func (h *MeetingsHandler) Join(
ctx context.Context, req *bbb.Request,
) (bbb.Response, error) {
tx, err := store.ConnectionFromContext(ctx).Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
// Lookup meeting
meetingID, _ := req.Params.MeetingID()
meeting, err := store.GetMeetingStateByID(ctx, tx, meetingID)
if err != nil {
return nil, err
}
if meeting == nil {
// The meeting is not known to the cluster.
// To prevent endless loops we fail here.
return unknownMeetingBrowserResponse(), nil
}
// Get backend do redirect
backendState, err := meeting.GetBackendState(ctx, tx)
if err != nil {
return nil, err
}
// In case the meeting is not assigned to backend (yet)
if backendState == nil {
return retryJoinResponse(req), nil
}
// We have a backend - yay! check that the backend is
// ok and the node agent is alive.
if !backendState.IsNodeReady() {
return retryJoinResponse(req), nil
}
// Commit changes
if err := tx.Commit(ctx); err != nil {
return nil, err
}
// Dispatch to backend
backend := cluster.NewBackend(backendState)
if h.opts.UseReverseProxy {
return backend.JoinProxy(ctx, req)
}
return backend.Join(ctx, req)
}
// Create will acquire a backend from the router
// selected for the request and create the meeting.
func (h *MeetingsHandler) Create(
ctx context.Context,
req *bbb.Request,
) (bbb.Response, error) {
var (
backend *cluster.Backend
err error
)
// Lookup backend, as we need to make this
// endpoint idempotent
backend, err = h.router.LookupBackend(ctx, req)
if err != nil {
return nil, err
}
// When no backend is found, select a new one.
if backend == nil {
backend, err = h.router.SelectBackend(ctx, req)
}
if err != nil {
return nil, err
}
return backend.Create(ctx, req)
}
// IsMeetingRunning will check on a backend if the meeting is still running
func (h *MeetingsHandler) IsMeetingRunning(
ctx context.Context, req *bbb.Request,
) (bbb.Response, error) {
// Fallback response: If we encounter an error
// in the backend, we assume that the meeting is
// not running.
notRunningRes := &bbb.IsMeetingRunningResponse{
XMLResponse: &bbb.XMLResponse{
Returncode: bbb.RetSuccess,
},
Running: false,
}
notRunningRes.SetStatus(http.StatusOK) // I'm pretty sure we need to do this...
backend, err := h.router.LookupBackend(ctx, req)
if err != nil {
return nil, err
}
if backend == nil {
return notRunningRes, nil
}
// We have a backend to handle the request. Let's try this.
res, err := backend.IsMeetingRunning(ctx, req)
if err != nil {
return notRunningRes, nil
}
return res, nil
}
// End will end a meeting on a backend
func (h *MeetingsHandler) End(
ctx context.Context, req *bbb.Request,
) (bbb.Response, error) {
backend, err := h.router.LookupBackend(ctx, req)
if err != nil {
return nil, err
}
if backend != nil {
return backend.End(ctx, req)
}
return unknownMeetingResponse(), nil
}
// GetMeetingInfo will not hit a backend, but we will query
// the store directly.
func (h *MeetingsHandler) GetMeetingInfo(
ctx context.Context,
req *bbb.Request,
) (bbb.Response, error) {
backend, err := h.router.LookupBackend(ctx, req)
if err != nil {
return nil, err
}
if backend != nil {
return backend.GetMeetingInfo(ctx, req)
}
return unknownMeetingResponse(), nil
}
// GetMeetings lists all meetings in the cluster relevant
// for the frontend
func (h *MeetingsHandler) GetMeetings(
ctx context.Context,
req *bbb.Request,
) (bbb.Response, error) {
tx, err := store.ConnectionFromContext(ctx).Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
// Get all meetings from our store associated
// with the requesting frontend.
mstates, err := store.GetMeetingStates(ctx, tx, store.Q().
Join("frontends ON frontends.id = meetings.frontend_id").
Where("meetings.backend_id IS NOT NULL").
Where("frontends.key = ?", req.Frontend.Key))
if err != nil {
return nil, err
}
tx.Rollback(ctx)
meetings := make([]*bbb.Meeting, 0, len(mstates))
for _, state := range mstates {
meetings = append(meetings, state.Meeting)
}
// Create response with all meetings
res := &bbb.GetMeetingsResponse{
XMLResponse: &bbb.XMLResponse{
Returncode: "SUCCESS",
},
Meetings: meetings,
}
res.SetStatus(http.StatusOK)
return res, nil
}
// retryJoinResponse makes a new JoinResponse with
// a redirect to a waiting page. The original request will be
// encoded and passed to the page as a parameter.
func retryJoinResponse(req *bbb.Request) *bbb.JoinResponse {
retryURL := "/b3s/retry-join/" + string(req.MarshalURLSafe())
body := templates.Redirect(retryURL)
// Create custom join response
res := &bbb.JoinResponse{
XMLResponse: new(bbb.XMLResponse),
}
res.SetStatus(http.StatusFound)
res.SetRaw(body)
res.SetHeader(http.Header{
"content-type": []string{"text/html"},
"location": []string{retryURL},
})
return res
}
// unknownMeetingResponse is a standard error response,
// when the meeting could not be found by a lookup.
func unknownMeetingResponse() *bbb.XMLResponse {
// response mirrors https://github.com/bigbluebutton/bigbluebutton/blob/main/bbb-common-web/src/main/java/org/bigbluebutton/api/model/constraint/MeetingExistsConstraint.java
res := &bbb.XMLResponse{
Returncode: bbb.RetFailed,
Message: "A meeting with that ID does not exist",
MessageKey: "notFound",
}
res.SetStatus(http.StatusOK) // I'm pretty sure we need
// to respond with some success status code, otherwise
// greenlight and the like will assume incorrect credentials
// or something.
return res
}
// The unknownMeetingBrowserResponse renders a human readable 404 template
// in case the meeting was not found.
func unknownMeetingBrowserResponse() *bbb.JoinResponse {
// Create custom join response
body := templates.MeetingNotFound()
res := &bbb.JoinResponse{
XMLResponse: new(bbb.XMLResponse),
}
res.SetRaw(body)
res.SetStatus(http.StatusNotFound)
return res
}