Skip to content

Commit

Permalink
Add auto close of inactive requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Larkworthy authored and Tom Larkworthy committed Mar 29, 2021
1 parent 1db1e42 commit 42d2865
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions index.mjs
Expand Up @@ -144,12 +144,18 @@ app.all(routes.pattern, async (req, res) => {
try {

page = await newPage(shard);

// Tidy page on cancelled request
req.on('close', async function (err) {
if (page) await page.close(); page = undefined;
});

await page.evaluateOnNewDocument((notebook) => {
window["@endpointservices.context"] = {
serverless: true,
notebook: notebook,
secrets: {}
};;
};
}, notebook);
await page.setUserAgent(useragent.encode({
terminal: isTerminal,
Expand Down Expand Up @@ -261,7 +267,7 @@ app.all(routes.pattern, async (req, res) => {
});

} catch (err) {
if (page) await page.close();
if (page) await page.close(); page = undefined;
const millis = Date.now() - t_start;
let status;
if (err.message.startsWith("waiting for function failed")) {
Expand All @@ -278,7 +284,6 @@ app.all(routes.pattern, async (req, res) => {
status,
duration: millis
});

res.status(status).send(err.message);
}
});
Expand Down
2 changes: 1 addition & 1 deletion useragent.test.mjs
Expand Up @@ -36,7 +36,7 @@ test('External UA', () => {
test('Encode UA', () => {
expect(useragent.decode(useragent.encode({
terminal: true,
orchistrator: true
orchestrator: true
}))).toEqual({
"isExternalUA": false,
"isTerminalUA": true,
Expand Down

0 comments on commit 42d2865

Please sign in to comment.