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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs",
SkippableTimer: "resource:///modules/UrlbarUtils.sys.mjs",
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
});
const SEARCH_PARAMS = {
CLIENT_VARIANTS: "client_variants",
PROVIDERS: "providers",
QUERY: "q",
SEQUENCE_NUMBER: "seq",
SESSION_ID: "sid",
};
const SESSION_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
const HISTOGRAM_LATENCY = "FX_URLBAR_MERINO_LATENCY_MS";
const HISTOGRAM_RESPONSE = "FX_URLBAR_MERINO_RESPONSE";
/**
* Client class for querying the Merino server. Each instance maintains its own
* session state including a session ID and sequence number that is included in
* its requests to Merino.
*/
export class MerinoClient {
/**
* @returns {object}
* The names of URL search params.
*/
static get SEARCH_PARAMS() {
return { ...SEARCH_PARAMS };
}
/**
* @param {string} name
* An optional name for the client. It will be included in log messages.
*/
constructor(name = "anonymous") {
this.#name = name;
XPCOMUtils.defineLazyGetter(this, "logger", () =>
lazy.UrlbarUtils.getLogger({ prefix: `MerinoClient [${name}]` })
);
}
/**
* @returns {string}
* The name of the client.
*/
get name() {
return this.#name;
}
/**
* @returns {number}
* If `resetSession()` is not called within this timeout period after a
* session starts, the session will time out and the next fetch will begin a
* new session.
*/
get sessionTimeoutMs() {
return this.#sessionTimeoutMs;
}
set sessionTimeoutMs(value) {
this.#sessionTimeoutMs = value;
}
/**
* @returns {number}
* The current session ID. Null when there is no active session.
*/
get sessionID() {
return this.#sessionID;
}
/**
* @returns {number}
* The current sequence number in the current session. Zero when there is no
* active session.
*/
get sequenceNumber() {
return this.#sequenceNumber;
}
/**
* @returns {string}
* A string that indicates the status of the last fetch. The values are the
* same as the labels used in the `FX_URLBAR_MERINO_RESPONSE` histogram:
* success, timeout, network_error, http_error
*/
get lastFetchStatus() {
return this.#lastFetchStatus;
}
/**
* Fetches Merino suggestions.
*
* @param {object} options
* Options object
* @param {string} options.query
* The search string.
* @param {Array} options.providers
* Array of provider names to request from Merino. If this is given it will
* override the `merinoProviders` Nimbus variable and its fallback pref
* `browser.urlbar.merino.providers`.
* @param {number} options.timeoutMs
* Timeout in milliseconds. This method will return once the timeout
* elapses, a response is received, or an error occurs, whichever happens
* first.
* @param {string} options.extraLatencyHistogram
* If specified, the fetch's latency will be recorded in this histogram in
* addition to the usual Merino latency histogram.
* @param {string} options.extraResponseHistogram
* If specified, the fetch's response will be recorded in this histogram in
* addition to the usual Merino response histogram.
* @returns {Array}
* The Merino suggestions or null if there's an error or unexpected
* response.
*/
async fetch({
query,
providers = null,
timeoutMs = lazy.UrlbarPrefs.get("merinoTimeoutMs"),
extraLatencyHistogram = null,
extraResponseHistogram = null,
}) {
this.logger.info(`Fetch starting with query: "${query}"`);
// Set up the Merino session ID and related state. The session ID is a UUID
// without leading and trailing braces.
if (!this.#sessionID) {
let uuid = Services.uuid.generateUUID().toString();
this.#sessionID = uuid.substring(1, uuid.length - 1);
this.#sequenceNumber = 0;
this.#sessionTimer?.cancel();
// Per spec, for the user's privacy, the session should time out and a new
// session ID should be used if the engagement does not end soon.
this.#sessionTimer = new lazy.SkippableTimer({
name: "Merino session timeout",
time: this.#sessionTimeoutMs,
logger: this.logger,
callback: () => this.resetSession(),
});
}
// Get the endpoint URL. It's empty by default when running tests so they
// don't hit the network.
let endpointString = lazy.UrlbarPrefs.get("merinoEndpointURL");
if (!endpointString) {
return [];
}
let url;
try {
url = new URL(endpointString);
} catch (error) {
this.logger.error("Error creating endpoint URL: " + error);
return [];
}
url.searchParams.set(SEARCH_PARAMS.QUERY, query);
url.searchParams.set(SEARCH_PARAMS.SESSION_ID, this.#sessionID);
url.searchParams.set(SEARCH_PARAMS.SEQUENCE_NUMBER, this.#sequenceNumber);
this.#sequenceNumber++;
let clientVariants = lazy.UrlbarPrefs.get("merinoClientVariants");
if (clientVariants) {
url.searchParams.set(SEARCH_PARAMS.CLIENT_VARIANTS, clientVariants);
}
let providersString;
if (providers != null) {
if (!Array.isArray(providers)) {
throw new Error("providers must be an array if given");
}
providersString = providers.join(",");
} else {
let value = lazy.UrlbarPrefs.get("merinoProviders");
if (value) {
// The Nimbus variable/pref is used only if it's a non-empty string.
providersString = value;
}
}
// An empty providers string is a valid value and means Merino should
// receive the request but not return any suggestions, so do not do a simple
// `if (providersString)` here.
if (typeof providersString == "string") {
url.searchParams.set(SEARCH_PARAMS.PROVIDERS, providersString);
}
let details = { query, providers, timeoutMs, url };
this.logger.debug("Fetch details: " + JSON.stringify(details));
let recordResponse = category => {
this.logger.info("Fetch done with status: " + category);
Services.telemetry.getHistogramById(HISTOGRAM_RESPONSE).add(category);
if (extraResponseHistogram) {
Services.telemetry
.getHistogramById(extraResponseHistogram)
.add(category);
}
this.#lastFetchStatus = category;
recordResponse = null;
};
// Set up the timeout timer.
let timer = (this.#timeoutTimer = new lazy.SkippableTimer({
name: "Merino timeout",
time: timeoutMs,
logger: this.logger,
callback: () => {
// The fetch timed out.
this.logger.info(`Fetch timed out (timeout = ${timeoutMs}ms)`);
recordResponse?.("timeout");
},
}));
// If there's an ongoing fetch, abort it so there's only one at a time. By
// design we do not abort fetches on timeout or when the query is canceled
// so we can record their latency.
try {
this.#fetchController?.abort();
} catch (error) {
this.logger.error("Error aborting previous fetch: " + error);
}
// Do the fetch.
let response;
let controller = (this.#fetchController = new AbortController());
let stopwatchInstance = (this.#latencyStopwatchInstance = {});
TelemetryStopwatch.start(HISTOGRAM_LATENCY, stopwatchInstance);
if (extraLatencyHistogram) {
TelemetryStopwatch.start(extraLatencyHistogram, stopwatchInstance);
}
await Promise.race([
timer.promise,
(async () => {
try {
// Canceling the timer below resolves its promise, which can resolve
// the outer promise created by `Promise.race`. This inner async
// function happens not to await anything after canceling the timer,
// but if it did, `timer.promise` could win the race and resolve the
// outer promise without a value. For that reason, we declare
// `response` in the outer scope and set it here instead of returning
// the response from this inner function and assuming it will also be
// returned by `Promise.race`.
response = await fetch(url, { signal: controller.signal });
TelemetryStopwatch.finish(HISTOGRAM_LATENCY, stopwatchInstance);
if (extraLatencyHistogram) {
TelemetryStopwatch.finish(extraLatencyHistogram, stopwatchInstance);
}
this.logger.debug(
"Got response: " +
JSON.stringify({ "response.status": response.status, ...details })
);
if (!response.ok) {
recordResponse?.("http_error");
}
} catch (error) {
TelemetryStopwatch.cancel(HISTOGRAM_LATENCY, stopwatchInstance);
if (extraLatencyHistogram) {
TelemetryStopwatch.cancel(extraLatencyHistogram, stopwatchInstance);
}
if (error.name != "AbortError") {
this.logger.error("Fetch error: " + error);
recordResponse?.("network_error");
}
} finally {
// Now that the fetch is done, cancel the timeout timer so it doesn't
// fire and record a timeout. If it already fired, which it would have
// on timeout, or was already canceled, this is a no-op.
timer.cancel();
if (controller == this.#fetchController) {
this.#fetchController = null;
}
this.#nextResponseDeferred?.resolve(response);
this.#nextResponseDeferred = null;
}
})(),
]);
if (timer == this.#timeoutTimer) {
this.#timeoutTimer = null;
}
// Get the response body as an object.
let body;
try {
body = await response?.json();
} catch (error) {
this.logger.error("Error getting response as JSON: " + error);
}
if (body) {
this.logger.debug("Response body: " + JSON.stringify(body));
}
if (!body?.suggestions?.length) {
recordResponse?.("no_suggestion");
return [];
}
let { suggestions, request_id } = body;
if (!Array.isArray(suggestions)) {
this.logger.error("Unexpected response: " + JSON.stringify(body));
recordResponse?.("no_suggestion");
return [];
}
recordResponse?.("success");
return suggestions.map(suggestion => ({
...suggestion,
request_id,
source: "merino",
}));
}
/**
* Resets the Merino session ID and related state.
*/
resetSession() {
this.#sessionID = null;
this.#sequenceNumber = 0;
this.#sessionTimer?.cancel();
this.#sessionTimer = null;
this.#nextSessionResetDeferred?.resolve();
this.#nextSessionResetDeferred = null;
}
/**
* Cancels the timeout timer.
*/
cancelTimeoutTimer() {
this.#timeoutTimer?.cancel();
}
/**
* Returns a promise that's resolved when the next response is received or a
* network error occurs.
*
* @returns {Promise}
* The promise is resolved with the `Response` object or undefined if a
* network error occurred.
*/
waitForNextResponse() {
if (!this.#nextResponseDeferred) {
this.#nextResponseDeferred = lazy.PromiseUtils.defer();
}
return this.#nextResponseDeferred.promise;
}
/**
* Returns a promise that's resolved when the session is next reset, including
* on session timeout.
*
* @returns {Promise}
*/
waitForNextSessionReset() {
if (!this.#nextSessionResetDeferred) {
this.#nextSessionResetDeferred = lazy.PromiseUtils.defer();
}
return this.#nextSessionResetDeferred.promise;
}
get _test_sessionTimer() {
return this.#sessionTimer;
}
get _test_timeoutTimer() {
return this.#timeoutTimer;
}
get _test_fetchController() {
return this.#fetchController;
}
get _test_latencyStopwatchInstance() {
return this.#latencyStopwatchInstance;
}
// State related to the current session.
#sessionID = null;
#sequenceNumber = 0;
#sessionTimer = null;
#sessionTimeoutMs = SESSION_TIMEOUT_MS;
#name;
#timeoutTimer = null;
#fetchController = null;
#latencyStopwatchInstance = null;
#lastFetchStatus = null;
#nextResponseDeferred = null;
#nextSessionResetDeferred = null;
}
|