Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Reduce errors in logs

Chocobozzz3d86a570d3aa
diff --git a/server/core/controllers/download.ts b/server/core/controllers/download.ts
index 2cc213eea5..c34d137808 100644
--- a/server/core/controllers/download.ts
+++ b/server/core/controllers/download.ts
@@ -1,7 +1,7 @@
import { forceNumber, maxBy } from '@peertube/peertube-core-utils'
import { FileStorage, HttpStatusCode, UserRight, VideoResolution, VideoStreamingPlaylistType } from '@peertube/peertube-models'
import { exists } from '@server/helpers/custom-validators/misc.js'
-import { getAuthUser, parseRangeHeader } from '@server/helpers/express-utils.js'
+import { getAuthUser, parseRangeHeader, pipelineToResponse } from '@server/helpers/express-utils.js'
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
import { generateRequestStream } from '@server/helpers/requests.js'
import { ThrottleStream } from '@server/helpers/stream-throttle.js'
@@ -28,7 +28,6 @@ import express from 'express'
import { createReadStream } from 'fs'
import { stat } from 'fs/promises'
import { join } from 'path'
-import { pipeline } from 'stream/promises'
import { DOWNLOAD_PATHS, WEBSERVER } from '../initializers/constants.js'
import {
asyncMiddleware,
@@ -145,7 +144,7 @@ async function downloadTorrent (req: express.Request, res: express.Response) {
const remoteUrl = file.getRemoteTorrentUrl(video)
- await pipeline(generateRequestStream(remoteUrl), res)
+ await pipelineToResponse({ streams: [ generateRequestStream(remoteUrl) ], res, logLabel: `torrent download of ${remoteUrl}` })
}
// ---------------------------------------------------------------------------
@@ -505,7 +504,11 @@ async function downloadLocalFileWithOptionalThrottle (options: {
})
})
- await pipeline(readStream, new ThrottleStream({ totalBytesPerSecond, bytesPerIpPerSecond, ip: req.ip }), res)
+ await pipelineToResponse({
+ streams: [ readStream, new ThrottleStream({ totalBytesPerSecond, bytesPerIpPerSecond, ip: req.ip }) ],
+ res,
+ logLabel: `download of ${path}`
+ })
}
async function redirectVideoDownloadToObjectStorage (options: {
diff --git a/server/core/controllers/lazy-static.ts b/server/core/controllers/lazy-static.ts
index b5e365f935..c88508636f 100644
--- a/server/core/controllers/lazy-static.ts
+++ b/server/core/controllers/lazy-static.ts
@@ -1,11 +1,11 @@
import { HttpStatusCode } from '@peertube/peertube-models'
+import { pipelineToResponse } from '@server/helpers/express-utils.js'
import { generateRequestStream } from '@server/helpers/requests.js'
import { CONFIG } from '@server/initializers/config.js'
import { VideoFileModel } from '@server/models/video/video-file.js'
import cors from 'cors'
import express from 'express'
import { join } from 'node:path'
-import { pipeline } from 'node:stream/promises'
import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants.js'
import { AvatarImageFileCache } from '@server/lib/files-cache/avatar-image-file-cache.js'
@@ -121,5 +121,5 @@ async function getTorrent (req: express.Request, res: express.Response) {
// Proxify remote request without cache
const remoteUrl = file.getRemoteTorrentUrl(file.getVideo())
- await pipeline(generateRequestStream(remoteUrl), res)
+ await pipelineToResponse({ streams: [ generateRequestStream(remoteUrl) ], res, logLabel: `torrent download of ${remoteUrl}` })
}
diff --git a/server/core/helpers/express-utils.ts b/server/core/helpers/express-utils.ts
index b890c2824c..6c02bf619a 100644
--- a/server/core/helpers/express-utils.ts
+++ b/server/core/helpers/express-utils.ts
@@ -3,6 +3,8 @@ import { NSFWFlag, VideosCommonQuery } from '@peertube/peertube-models'
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
import express, { RequestHandler } from 'express'
import multer, { diskStorage } from 'multer'
+import { Duplex, Readable } from 'stream'
+import { pipeline } from 'stream/promises'
import { CONFIG } from '../initializers/config.js'
import { REMOTE_SCHEME } from '../initializers/constants.js'
import { isArray } from './custom-validators/misc.js'
@@ -209,6 +211,27 @@ export function parseRangeHeader (rangeHeader: string | undefined, size: number)
return { start, end }
}
+// Pipe streams to the HTTP response, ignoring the errors emitted when the client aborts the request
+export async function pipelineToResponse (options: {
+ streams: (Readable | Duplex)[]
+ res: express.Response
+ logLabel: string
+}) {
+ const { streams, res, logLabel } = options
+
+ try {
+ await pipeline([ ...streams, res ])
+ } catch (err) {
+ // The client can close the connection at any time: this is not a server error
+ if ([ 'ERR_STREAM_PREMATURE_CLOSE', 'ECONNRESET', 'EPIPE' ].includes(err.code)) {
+ logger.debug(`Client aborted ${logLabel}`, { err })
+ return
+ }
+
+ throw err
+ }
+}
+
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------