-
Notifications
You must be signed in to change notification settings - Fork 124
/
manifest_update_scheduler.ts
287 lines (267 loc) · 11.2 KB
/
manifest_update_scheduler.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
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
/**
* Copyright 2015 CANAL+ Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
defer as observableDefer,
EMPTY,
from as observableFrom,
merge as observableMerge,
Observable,
of as observableOf,
timer as observableTimer,
} from "rxjs";
import {
mapTo,
mergeMap,
mergeMapTo,
take,
} from "rxjs/operators";
import config from "../../config";
import log from "../../log";
import Manifest from "../../manifest";
import {
IManifestFetcherParsedResult,
IManifestFetcherParserOptions,
} from "../fetchers";
import { IWarningEvent } from "./types";
const { FAILED_PARTIAL_UPDATE_MANIFEST_REFRESH_DELAY,
MAX_CONSECUTIVE_MANIFEST_PARSING_IN_UNSAFE_MODE,
MIN_MANIFEST_PARSING_TIME_TO_ENTER_UNSAFE_MODE } = config;
/** Arguments to give to the `manifestUpdateScheduler` */
export interface IManifestUpdateSchedulerArguments {
/** Function used to refresh the manifest */
fetchManifest : IManifestFetcher;
/** Information about the initial load of the manifest */
initialManifest : { manifest : Manifest;
sendingTime? : number;
receivedTime? : number;
parsingTime : number; };
/** URL at which a shorter version of the Manifest can be found. */
manifestUpdateUrl : string | undefined;
/** Minimum interval to keep between Manifest updates */
minimumManifestUpdateInterval : number;
/** Allows the rest of the code to ask for a Manifest refresh */
scheduleRefresh$ : IManifestRefreshScheduler;
}
/** Function defined to refresh the Manifest */
export type IManifestFetcher =
(manifestURL : string | undefined, options : IManifestFetcherParserOptions) =>
Observable<IManifestFetcherParsedResult | IWarningEvent>;
/** Events sent by the `IManifestRefreshScheduler` Observable */
export interface IManifestRefreshSchedulerEvent {
/**
* if `true`, the Manifest should be fully updated.
* if `false`, a shorter version with just the added information can be loaded
* instead.
*/
completeRefresh : boolean;
/**
* Optional wanted refresh delay, which is the minimum time you want to wait
* before updating the Manifest
*/
delay? : number;
/**
* Whether the parsing can be done in the more efficient "unsafeMode".
* This mode is extremely fast but can lead to de-synchronisation with the
* server.
*/
canUseUnsafeMode : boolean;
}
/** Observable to send events related to refresh requests coming from the Player. */
export type IManifestRefreshScheduler = Observable<IManifestRefreshSchedulerEvent>;
/**
* Refresh the Manifest at the right time.
* @param {Object} manifestUpdateSchedulerArguments
* @returns {Observable}
*/
export default function manifestUpdateScheduler({
fetchManifest,
initialManifest,
manifestUpdateUrl,
minimumManifestUpdateInterval,
scheduleRefresh$,
} : IManifestUpdateSchedulerArguments) : Observable<IWarningEvent> {
// The Manifest always keeps the same Manifest
const { manifest } = initialManifest;
/** Number of consecutive times the parsing has been done in `unsafeMode`. */
let consecutiveUnsafeMode = 0;
function handleManifestRefresh$(
manifestInfos: { manifest: Manifest;
sendingTime?: number;
receivedTime? : number;
parsingTime : number;
updatingTime? : number; }): Observable<IWarningEvent> {
const { sendingTime,
parsingTime,
updatingTime } = manifestInfos;
/**
* Total time taken to fully update the last Manifest.
* Note: this time also includes possible requests done by the parsers.
*/
const totalUpdateTime = parsingTime + (updatingTime ?? 0);
// Only perform parsing in `unsafeMode` when the last full parsing took a
// lot of time and do not go higher than the maximum consecutive time.
const unsafeModeEnabled = consecutiveUnsafeMode > 0 ?
consecutiveUnsafeMode < MAX_CONSECUTIVE_MANIFEST_PARSING_IN_UNSAFE_MODE :
totalUpdateTime >= MIN_MANIFEST_PARSING_TIME_TO_ENTER_UNSAFE_MODE;
const internalRefresh$ = scheduleRefresh$
.pipe(mergeMap(({ completeRefresh, delay, canUseUnsafeMode }) => {
const unsafeMode = canUseUnsafeMode && unsafeModeEnabled;
return startManualRefreshTimer(delay ?? 0,
minimumManifestUpdateInterval,
sendingTime)
.pipe(mapTo({ completeRefresh, unsafeMode }));
}));
const timeSinceRequest = sendingTime == null ? 0 :
performance.now() - sendingTime;
const minInterval = Math.max(minimumManifestUpdateInterval - timeSinceRequest, 0);
let autoRefresh$;
if (manifest.lifetime === undefined || manifest.lifetime < 0) {
autoRefresh$ = EMPTY;
} else {
let autoRefreshInterval = manifest.lifetime * 1000 - timeSinceRequest;
if (manifest.lifetime < 3 && totalUpdateTime >= 100) {
const defaultDelay = (3 - manifest.lifetime) * 1000 + autoRefreshInterval;
const newInterval = Math.max(defaultDelay,
Math.max(autoRefreshInterval, 0) + totalUpdateTime);
log.info("MUS: Manifest update rythm is too frequent. Postponing next request.",
autoRefreshInterval,
newInterval);
autoRefreshInterval = newInterval;
} else if (totalUpdateTime >= (manifest.lifetime * 1000) / 10) {
const newInterval = Math.max(autoRefreshInterval, 0) + totalUpdateTime;
log.info("MUS: Manifest took too long to parse. Postponing next request",
autoRefreshInterval,
newInterval);
autoRefreshInterval = newInterval;
}
autoRefresh$ = observableTimer(Math.max(autoRefreshInterval, minInterval))
.pipe(mapTo({ completeRefresh: false, unsafeMode: unsafeModeEnabled }));
}
const expired$ = manifest.expired === null ?
EMPTY :
observableTimer(minInterval)
.pipe(mergeMapTo(observableFrom(manifest.expired)),
mapTo({ completeRefresh: true, unsafeMode: unsafeModeEnabled }));
// Emit when the manifest should be refreshed. Either when:
// - A buffer asks for it to be refreshed
// - its lifetime expired.
return observableMerge(autoRefresh$, internalRefresh$, expired$).pipe(
take(1),
mergeMap(({ completeRefresh,
unsafeMode }) => refreshManifest({ completeRefresh,
unsafeMode })),
mergeMap(evt => {
if (evt.type === "warning") {
return observableOf(evt);
}
return handleManifestRefresh$(evt);
}));
}
return observableDefer(() => handleManifestRefresh$(initialManifest));
/**
* Refresh the Manifest.
* Perform a full update if a partial update failed.
* @param {boolean} completeRefresh
* @returns {Observable}
*/
function refreshManifest(
{ completeRefresh,
unsafeMode } : { completeRefresh : boolean;
unsafeMode : boolean; }
) : Observable<IManifestFetcherParsedResult | IWarningEvent> {
const fullRefresh = completeRefresh || manifestUpdateUrl === undefined;
const refreshURL = fullRefresh ? manifest.getUrl() :
manifestUpdateUrl;
const externalClockOffset = manifest.clockOffset;
if (unsafeMode) {
consecutiveUnsafeMode += 1;
log.info("Init: Refreshing the Manifest in \"unsafeMode\" for the " +
String(consecutiveUnsafeMode) + " consecutive time.");
} else if (consecutiveUnsafeMode > 0) {
log.info("Init: Not parsing the Manifest in \"unsafeMode\" anymore after " +
String(consecutiveUnsafeMode) + " consecutive times.");
consecutiveUnsafeMode = 0;
}
return fetchManifest(refreshURL, { externalClockOffset,
previousManifest: manifest,
unsafeMode })
.pipe(mergeMap((value) => {
if (value.type === "warning") {
return observableOf(value);
}
const { manifest: newManifest,
sendingTime: newSendingTime,
receivedTime,
parsingTime } = value;
const updateTimeStart = performance.now();
if (fullRefresh) {
manifest.replace(newManifest);
} else {
try {
manifest.update(newManifest);
} catch (e) {
const message = e instanceof Error ? e.message :
"unknown error";
log.warn(`MUS: Attempt to update Manifest failed: ${message}`,
"Re-downloading the Manifest fully");
return startManualRefreshTimer(FAILED_PARTIAL_UPDATE_MANIFEST_REFRESH_DELAY,
minimumManifestUpdateInterval,
newSendingTime)
.pipe(mergeMap(() =>
refreshManifest({ completeRefresh: true, unsafeMode: false })));
}
}
return observableOf({ type: "parsed" as const,
manifest,
sendingTime: newSendingTime,
receivedTime,
parsingTime,
updatingTime: performance.now() - updateTimeStart });
}));
}
}
/**
* Launch a timer Observable which will emit when it is time to refresh the
* Manifest.
* The timer's delay is calculated from:
* - a target delay (`wantedDelay`), which is the minimum time we want to wait
* in the best scenario
* - the minimum set possible interval between manifest updates
* (`minimumManifestUpdateInterval`)
* - the time at which was done the last Manifest refresh
* (`lastManifestRequestTime`)
* @param {number} wantedDelay
* @param {number} minimumManifestUpdateInterval
* @param {number|undefined} lastManifestRequestTime
* @returns {Observable}
*/
function startManualRefreshTimer(
wantedDelay : number,
minimumManifestUpdateInterval : number,
lastManifestRequestTime : number | undefined
) : Observable<unknown> {
return observableDefer(() => {
// The value allows to set a delay relatively to the last Manifest refresh
// (to avoid asking for it too often).
const timeSinceLastRefresh = lastManifestRequestTime == null ?
0 :
performance.now() - lastManifestRequestTime;
const _minInterval = Math.max(minimumManifestUpdateInterval - timeSinceLastRefresh,
0);
return observableTimer(Math.max(wantedDelay - timeSinceLastRefresh,
_minInterval));
});
}