forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.ts
259 lines (219 loc) · 6.72 KB
/
lambda.ts
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
INack,
ISequencedDocumentMessage,
ISignalClient,
ISignalMessage,
MessageType,
} from "@fluidframework/protocol-definitions";
import {
extractBoxcar,
IClientManager,
IContext,
IMessageBatch,
INackMessage,
IPartitionLambda,
IPublisher,
IQueuedMessage,
ISequencedOperationMessage,
IServiceConfiguration,
ITicketedSignalMessage,
NackOperationType,
SequencedOperationType,
SignalOperationType,
} from "@fluidframework/server-services-core";
/**
* Container for a batch of messages being sent for a specific tenant/document id
*/
type BroadcasterMessageBatch = IMessageBatch<ISequencedDocumentMessage | INack | ISignalMessage>;
// Set immediate is not available in all environments, specifically it does not work in a browser.
// Fallback to set timeout in those cases
let taskScheduleFunction: (cb: () => any) => unknown;
let clearTaskScheduleTimerFunction: (timer: any) => void;
if (typeof setImmediate === "function") {
taskScheduleFunction = setImmediate;
clearTaskScheduleTimerFunction = clearImmediate;
} else {
taskScheduleFunction = setTimeout;
clearTaskScheduleTimerFunction = clearTimeout;
}
/**
* @internal
*/
export class BroadcasterLambda implements IPartitionLambda {
private pending = new Map<string, BroadcasterMessageBatch>();
private pendingOffset: IQueuedMessage | undefined;
private current = new Map<string, BroadcasterMessageBatch>();
private messageSendingTimerId: unknown | undefined;
constructor(
private readonly publisher: IPublisher<ISequencedDocumentMessage | INack | ISignalMessage>,
private readonly context: IContext,
private readonly serviceConfiguration: IServiceConfiguration,
private readonly clientManager: IClientManager | undefined,
) {}
public async handler(message: IQueuedMessage) {
const boxcar = extractBoxcar(message);
for (const baseMessage of boxcar.contents) {
let topic: string | undefined;
let event: string | undefined;
switch (baseMessage.type) {
case SequencedOperationType: {
event = "op";
const sequencedOperationMessage = baseMessage as ISequencedOperationMessage;
topic = `${sequencedOperationMessage.tenantId}/${sequencedOperationMessage.documentId}`;
break;
}
case NackOperationType: {
event = "nack";
const nackMessage = baseMessage as INackMessage;
topic = `client#${nackMessage.clientId}`;
break;
}
case SignalOperationType: {
event = "signal";
const ticketedSignalMessage = baseMessage as ITicketedSignalMessage;
topic = `${ticketedSignalMessage.tenantId}/${ticketedSignalMessage.documentId}`;
if (this.clientManager && ticketedSignalMessage.operation) {
const signalContent = JSON.parse(
ticketedSignalMessage.operation.content as string,
);
const signalType: MessageType | undefined =
typeof signalContent.type === "string" ? signalContent.type : undefined;
switch (signalType) {
case MessageType.ClientJoin: {
const signalClient: ISignalClient = signalContent.content;
await this.clientManager.addClient(
ticketedSignalMessage.tenantId,
ticketedSignalMessage.documentId,
signalClient.clientId,
signalClient.client,
ticketedSignalMessage.operation,
);
break;
}
case MessageType.ClientLeave:
await this.clientManager.removeClient(
ticketedSignalMessage.tenantId,
ticketedSignalMessage.documentId,
signalContent.content,
ticketedSignalMessage.operation,
);
break;
default:
// ignore unknown types
break;
}
}
break;
}
default:
// ignore unknown types
continue;
}
const value = baseMessage as
| INackMessage
| ISequencedOperationMessage
| ITicketedSignalMessage;
if (
value.type === SequencedOperationType &&
value.operation?.traces &&
value.operation.traces.length > 0
) {
value.operation.traces.push({
action: "start",
service: "broadcaster",
timestamp: Date.now(),
});
}
if (this.serviceConfiguration.broadcaster.includeEventInMessageBatchName) {
topic += event;
}
let pendingBatch = this.pending.get(topic);
if (!pendingBatch) {
pendingBatch = {
tenantId: value.tenantId,
documentId: value.documentId,
event,
messages: [value.operation],
};
this.pending.set(topic, pendingBatch);
} else {
pendingBatch.messages.push(value.operation);
}
}
this.pendingOffset = message;
this.sendPending();
return undefined;
}
public close() {
this.pending.clear();
this.current.clear();
this.pendingOffset = undefined;
if (this.messageSendingTimerId !== undefined) {
clearTaskScheduleTimerFunction(this.messageSendingTimerId);
this.messageSendingTimerId = undefined;
}
}
public hasPendingWork() {
return this.pending.size !== 0 || this.current.size !== 0;
}
private sendPending() {
if (this.messageSendingTimerId !== undefined) {
// a send is in progress
return;
}
if (this.pending.size === 0) {
// no pending work. checkpoint now if we have a pending offset
if (this.pendingOffset) {
this.context.checkpoint(this.pendingOffset);
this.pendingOffset = undefined;
}
return;
}
// Invoke the next send after a delay to give IO time to create more batches
this.messageSendingTimerId = taskScheduleFunction(async () => {
const batchOffset = this.pendingOffset;
this.current = this.pending;
this.pending = new Map<string, BroadcasterMessageBatch>();
this.pendingOffset = undefined;
// Process all the batches + checkpoint
if (this.publisher.emitBatch) {
const promises: Promise<void>[] = [];
for (const [topic, batch] of this.current) {
promises.push(this.publisher.emitBatch(topic, batch));
}
try {
await Promise.all(promises);
} catch (ex) {
this.context.error(ex, { restart: true });
return;
}
} else if (this.publisher.emit) {
const promises: Promise<void>[] = [];
for (const [topic, batch] of this.current) {
promises.push(
this.publisher.emit(topic, batch.event, batch.documentId, batch.messages),
);
}
try {
await Promise.all(promises);
} catch (ex) {
this.context.error(ex, { restart: true });
return;
}
} else {
for (const [topic, batch] of this.current) {
this.publisher.to(topic).emit(batch.event, batch.documentId, batch.messages);
}
}
this.messageSendingTimerId = undefined;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.context.checkpoint(batchOffset!);
this.current.clear();
this.sendPending();
});
}
}