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

Limit sharp's concurrency #53385

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Changes from 2 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
23 changes: 17 additions & 6 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from 'crypto'
import { promises } from 'fs'
import { cpus } from 'os'
import type { IncomingMessage, ServerResponse } from 'http'
import { mediaType } from 'next/dist/compiled/@hapi/accept'
import chalk from 'next/dist/compiled/chalk'
Expand Down Expand Up @@ -43,15 +44,25 @@ const VECTOR_TYPES = [SVG]
const BLUR_IMG_SIZE = 8 // should match `next-image-loader`
const BLUR_QUALITY = 70 // should match `next-image-loader`

let sharp:
| ((
input?: string | Buffer,
options?: import('sharp').SharpOptions
) => import('sharp').Sharp)
| undefined
let sharp: typeof import('sharp') | undefined

try {
sharp = require(process.env.NEXT_SHARP_PATH || 'sharp')
if (sharp) {
// During development, there's no need to create a large thread pool to
// process images. This helpes to reduce the memory usage of the dev server.
shuding marked this conversation as resolved.
Show resolved Hide resolved
// For production, we set a larger number here.
// https://sharp.pixelplumbing.com/api-utility#concurrency
sharp.concurrency(
Math.min(
process.env.NODE_ENV === 'development' ? 2 : 8,
styfle marked this conversation as resolved.
Show resolved Hide resolved
cpus().length,
// The current concurrency value from Sharp. This varies based on the
// system the server is running on.
sharp.concurrency()
)
)
}
} catch (e) {
// Sharp not present on the server, Squoosh fallback will be used
}
Expand Down
Loading