Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency #3120

Merged
merged 7 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-gifts-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add `config.kit.prerender.concurrency` setting
4 changes: 3 additions & 1 deletion documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const config = {
base: ''
},
prerender: {
concurrency: 1,
crawl: true,
enabled: true,
entries: ['*'],
Expand All @@ -55,7 +56,7 @@ const config = {
trailingSlash: 'never',
vite: () => ({})
},

// SvelteKit uses vite-plugin-svelte. Its options can be provided directly here.
// See the available options at https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md

Expand Down Expand Up @@ -163,6 +164,7 @@ An object containing zero or more of the following `string` values:

See [Prerendering](#ssr-and-javascript-prerender). An object containing zero or more of the following:

- `concurrency` — how many pages can be prerendered simultaneously. JS is single-threaded, but in cases where prerendering performance is network-bound (for example loading content from a remote CMS) this can speed things up by processing other tasks while waiting on the network response
- `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s)
- `enabled` — set to `false` to disable prerendering altogether
- `entries` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` )
Expand Down
24 changes: 19 additions & 5 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { __fetch_polyfill } from '../../install-fetch.js';
import { SVELTE_KIT } from '../constants.js';
import { get_single_valued_header } from '../../utils/http.js';
import { is_root_relative, resolve } from '../../utils/url.js';
import { queue } from './queue.js';

/**
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
Expand Down Expand Up @@ -141,16 +142,27 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
return path;
}

const q = queue(config.kit.prerender.concurrency);

/**
* @param {string} decoded_path
* @param {string?} referrer
*/
async function visit(decoded_path, referrer) {
function enqueue(decoded_path, referrer) {
const path = encodeURI(normalize(decoded_path));

if (seen.has(path)) return;
seen.add(path);

return q.add(() => visit(path, decoded_path, referrer));
}

/**
* @param {string} path
* @param {string} decoded_path
* @param {string?} referrer
*/
async function visit(path, decoded_path, referrer) {
/** @type {Map<string, import('types/hooks').ServerResponse>} */
const dependencies = new Map();

Expand Down Expand Up @@ -195,7 +207,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const resolved = resolve(path, location);
if (is_root_relative(resolved)) {
await visit(resolved, path);
enqueue(resolved, path);
}
} else {
log.warn(`location header missing on redirect received from ${decoded_path}`);
Expand Down Expand Up @@ -282,7 +294,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
// TODO warn that query strings have no effect on statically-exported pages
}

await visit(pathname, path);
enqueue(pathname, path);
}
}
}
Expand All @@ -292,12 +304,14 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
for (const entry of config.kit.prerender.entries) {
if (entry === '*') {
for (const entry of build_data.entries) {
await visit(entry, null);
enqueue(entry, null);
}
} else {
await visit(entry, null);
enqueue(entry, null);
}
}

await q.done();
}

if (fallback) {
Expand Down
78 changes: 78 additions & 0 deletions packages/kit/src/core/adapt/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/** @typedef {{
* fn: () => Promise<any>,
* fulfil: (value: any) => void,
* reject: (error: Error) => void
* }} Task */

/** @param {number} concurrency */
export function queue(concurrency) {
/** @type {Task[]} */
const tasks = [];

let current = 0;

/** @type {(value?: any) => void} */
let fulfil;

/** @type {(error: Error) => void} */
let reject;

let closed = false;

const done = new Promise((f, r) => {
fulfil = f;
reject = r;
});

done.catch(() => {
// this is necessary in case a catch handler is never added
// to the done promise by the user
});

function dequeue() {
if (current < concurrency) {
const task = tasks.shift();

if (task) {
current += 1;
const promise = Promise.resolve(task.fn());

promise
.then(task.fulfil, (err) => {
task.reject(err);
reject(err);
})
.then(() => {
current -= 1;
dequeue();
});
} else if (current === 0) {
closed = true;
fulfil();
}
}
}

return {
/** @param {() => any} fn */
add: (fn) => {
if (closed) throw new Error('Cannot add tasks to a queue that has ended');

const promise = new Promise((fulfil, reject) => {
tasks.push({ fn, fulfil, reject });
});

dequeue();
return promise;
},

done: () => {
if (current === 0) {
closed = true;
fulfil();
}

return done;
}
};
}
114 changes: 114 additions & 0 deletions packages/kit/src/core/adapt/queue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { queue } from './queue.js';

/** @param {number} ms */
function sleep(ms) {
return new Promise((f) => setTimeout(f, ms));
}

test('q.add resolves to the correct value', async () => {
const q = queue(1);

const value = await q.add(() => 42);
assert.equal(value, 42);
});

test('q.add rejects if task rejects', async () => {
const q = queue(1);

try {
await q.add(async () => {
await sleep(1);
throw new Error('nope');
});

assert.ok(false);
} catch (e) {
assert.equal(/** @type {Error} */ (e).message, 'nope');
}
});

test('starts tasks in sequence', async () => {
const q = queue(2);

const promises = [];

/** @type {Array<(value?: any) => void>} */
const fulfils = [];

const started = [false, false, false, false];
const finished = [false, false, false, false];

for (let i = 0; i < 4; i += 1) {
promises[i] = q
.add(() => {
started[i] = true;
return new Promise((f) => {
fulfils.push(f);
});
})
.then(() => {
finished[i] = true;
});
}

assert.equal(started, [true, true, false, false]);
assert.equal(finished, [false, false, false, false]);

fulfils[0]();
await promises[0];

assert.equal(started, [true, true, true, false]);
assert.equal(finished, [true, false, false, false]);

fulfils[1]();
await promises[1];

assert.equal(started, [true, true, true, true]);
assert.equal(finished, [true, true, false, false]);

fulfils[2]();
fulfils[3]();
await q.done();

assert.equal(finished, [true, true, true, true]);
});

test('q.add fails if queue is already finished', async () => {
const q = queue(1);
q.add(() => {});

await q.done();
assert.throws(() => q.add(() => {}), /Cannot add tasks to a queue that has ended/);
});

test('q.done() resolves if nothing was added to the queue', async () => {
const q = queue(100);
await Promise.race([
q.done(),
sleep(1).then(() => {
throw new Error('Timed out');
})
]);
});

test('q.done() rejects if task rejects', async () => {
const q = queue(1);

q.add(async () => {
await sleep(1);
throw new Error('nope');
}).catch((e) => {
assert.equal(e.message, 'nope');
});

try {
await q.done();
assert.ok(false);
} catch (e) {
assert.equal(/** @type {Error} */ (e).message, 'nope');
}
});

test.run();
3 changes: 2 additions & 1 deletion packages/kit/src/core/adapt/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ suite('prerender', async () => {
},
appDir: '_app',
prerender: {
concurrency: 1,
enabled: true,
entries: ['*']
}
Expand All @@ -115,7 +116,7 @@ suite('prerender', async () => {
dest
});

assert.equal(glob('**', { cwd: `${prerendered_files}` }), glob('**', { cwd: dest }));
assert.equal(glob('**', { cwd: prerendered_files }), glob('**', { cwd: dest }));

rmSync(dest, { recursive: true, force: true });
});
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test('fills in defaults', () => {
assets: ''
},
prerender: {
concurrency: 1,
crawl: true,
enabled: true,
entries: ['*'],
Expand Down Expand Up @@ -142,6 +143,7 @@ test('fills in partial blanks', () => {
assets: ''
},
prerender: {
concurrency: 1,
crawl: true,
enabled: true,
entries: ['*'],
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const options = object(
}),

prerender: object({
concurrency: number(1),
crawl: boolean(true),
enabled: boolean(true),
entries: validate(['*'], (input, keypath) => {
Expand Down Expand Up @@ -261,6 +262,19 @@ function string(fallback, allow_empty = true) {
});
}

/**
* @param {number} fallback
* @returns {Validator}
*/
function number(fallback) {
return validate(fallback, (input, keypath) => {
if (typeof input !== 'number') {
throw new Error(`${keypath} should be a number, if specified`);
}
return input;
});
}

/**
* @param {boolean} fallback
* @returns {Validator}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function testLoadDefaultConfig(path) {
serviceWorker: {},
paths: { base: '', assets: '' },
prerender: {
concurrency: 1,
crawl: true,
enabled: true,
entries: ['*'],
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface Config {
base?: string;
};
prerender?: {
concurrency?: number;
crawl?: boolean;
enabled?: boolean;
entries?: string[];
Expand Down