diff options
author | Chocobozzz <me@florianbigard.com> | 2020-01-03 13:47:45 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-01-03 13:47:45 +0100 |
commit | 35f28e94c763370616d25d5820f4b9ef70cedca9 (patch) | |
tree | 7ade51d5e60a5856d74c5776dd62fcaaa3ae0858 | |
parent | 562724a1ed13fe93aba08ca1a0f8e029819e9f32 (diff) | |
download | PeerTube-35f28e94c763370616d25d5820f4b9ef70cedca9.tar.gz PeerTube-35f28e94c763370616d25d5820f4b9ef70cedca9.tar.zst PeerTube-35f28e94c763370616d25d5820f4b9ef70cedca9.zip |
Add infohash cache
-rw-r--r-- | server/controllers/tracker.ts | 2 | ||||
-rw-r--r-- | server/helpers/utils.ts | 2 | ||||
-rw-r--r-- | server/initializers/constants.ts | 8 | ||||
-rw-r--r-- | server/models/video/video-file.ts | 9 | ||||
-rw-r--r-- | server/models/video/video-streaming-playlist.ts | 16 |
5 files changed, 32 insertions, 5 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 912f82b86..2ae1cf86c 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts | |||
@@ -48,7 +48,7 @@ const trackerServer = new TrackerServer({ | |||
48 | try { | 48 | try { |
49 | if (CONFIG.TRACKER.PRIVATE === false) return cb() | 49 | if (CONFIG.TRACKER.PRIVATE === false) return cb() |
50 | 50 | ||
51 | const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash) | 51 | const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash) |
52 | if (videoFileExists === true) return cb() | 52 | if (videoFileExists === true) return cb() |
53 | 53 | ||
54 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) | 54 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) |
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index ba07eaaf3..4c6f200f8 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts | |||
@@ -40,7 +40,7 @@ const getServerActor = memoizee(async function () { | |||
40 | actor.Account = application.Account | 40 | actor.Account = application.Account |
41 | 41 | ||
42 | return actor | 42 | return actor |
43 | }) | 43 | }, { promise: true }) |
44 | 44 | ||
45 | function generateVideoImportTmpPath (target: string | ParseTorrent) { | 45 | function generateVideoImportTmpPath (target: string | ParseTorrent) { |
46 | const id = typeof target === 'string' ? target : target.infoHash | 46 | const id = typeof target === 'string' ? target : target.infoHash |
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 1b7b94d74..f4a2b358b 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts | |||
@@ -572,7 +572,12 @@ const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS | |||
572 | const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls') | 572 | const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls') |
573 | 573 | ||
574 | const MEMOIZE_TTL = { | 574 | const MEMOIZE_TTL = { |
575 | OVERVIEWS_SAMPLE: 1000 * 3600 * 4 // 4 hours | 575 | OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours |
576 | INFO_HASH_EXISTS: 1000 * 3600 * 12 // 12 hours | ||
577 | } | ||
578 | |||
579 | const MEMOIZE_LENGTH = { | ||
580 | INFO_HASH_EXISTS: 200 | ||
576 | } | 581 | } |
577 | 582 | ||
578 | const QUEUE_CONCURRENCY = { | 583 | const QUEUE_CONCURRENCY = { |
@@ -728,6 +733,7 @@ export { | |||
728 | ACTIVITY_PUB_ACTOR_TYPES, | 733 | ACTIVITY_PUB_ACTOR_TYPES, |
729 | THUMBNAILS_SIZE, | 734 | THUMBNAILS_SIZE, |
730 | VIDEO_CATEGORIES, | 735 | VIDEO_CATEGORIES, |
736 | MEMOIZE_LENGTH, | ||
731 | VIDEO_LANGUAGES, | 737 | VIDEO_LANGUAGES, |
732 | VIDEO_PRIVACIES, | 738 | VIDEO_PRIVACIES, |
733 | VIDEO_LICENCES, | 739 | VIDEO_LICENCES, |
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index fa5a6e76d..e08999385 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts | |||
@@ -24,9 +24,10 @@ import { VideoModel } from './video' | |||
24 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' | 24 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' |
25 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' | 25 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' |
26 | import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' | 26 | import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' |
27 | import { MIMETYPES } from '../../initializers/constants' | 27 | import { MIMETYPES, MEMOIZE_LENGTH, MEMOIZE_TTL } from '../../initializers/constants' |
28 | import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file' | 28 | import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file' |
29 | import { MStreamingPlaylistVideo, MVideo } from '@server/typings/models' | 29 | import { MStreamingPlaylistVideo, MVideo } from '@server/typings/models' |
30 | import * as memoizee from 'memoizee' | ||
30 | 31 | ||
31 | @Table({ | 32 | @Table({ |
32 | tableName: 'videoFile', | 33 | tableName: 'videoFile', |
@@ -138,6 +139,12 @@ export class VideoFileModel extends Model<VideoFileModel> { | |||
138 | }) | 139 | }) |
139 | RedundancyVideos: VideoRedundancyModel[] | 140 | RedundancyVideos: VideoRedundancyModel[] |
140 | 141 | ||
142 | static doesInfohashExistCached = memoizee(VideoFileModel.doesInfohashExist, { | ||
143 | promise: true, | ||
144 | max: MEMOIZE_LENGTH.INFO_HASH_EXISTS, | ||
145 | maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS | ||
146 | }) | ||
147 | |||
141 | static doesInfohashExist (infoHash: string) { | 148 | static doesInfohashExist (infoHash: string) { |
142 | const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1' | 149 | const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1' |
143 | const options = { | 150 | const options = { |
diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index faad4cc2d..0099add4e 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts | |||
@@ -5,7 +5,14 @@ import { VideoModel } from './video' | |||
5 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' | 5 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' |
6 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | 6 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' |
7 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 7 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
8 | import { CONSTRAINTS_FIELDS, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_DOWNLOAD_PATHS, STATIC_PATHS } from '../../initializers/constants' | 8 | import { |
9 | CONSTRAINTS_FIELDS, | ||
10 | MEMOIZE_LENGTH, | ||
11 | MEMOIZE_TTL, | ||
12 | P2P_MEDIA_LOADER_PEER_VERSION, | ||
13 | STATIC_DOWNLOAD_PATHS, | ||
14 | STATIC_PATHS | ||
15 | } from '../../initializers/constants' | ||
9 | import { join } from 'path' | 16 | import { join } from 'path' |
10 | import { sha1 } from '../../helpers/core-utils' | 17 | import { sha1 } from '../../helpers/core-utils' |
11 | import { isArrayOf } from '../../helpers/custom-validators/misc' | 18 | import { isArrayOf } from '../../helpers/custom-validators/misc' |
@@ -13,6 +20,7 @@ import { Op, QueryTypes } from 'sequelize' | |||
13 | import { MStreamingPlaylist, MVideoFile } from '@server/typings/models' | 20 | import { MStreamingPlaylist, MVideoFile } from '@server/typings/models' |
14 | import { VideoFileModel } from '@server/models/video/video-file' | 21 | import { VideoFileModel } from '@server/models/video/video-file' |
15 | import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths' | 22 | import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths' |
23 | import * as memoizee from 'memoizee' | ||
16 | 24 | ||
17 | @Table({ | 25 | @Table({ |
18 | tableName: 'videoStreamingPlaylist', | 26 | tableName: 'videoStreamingPlaylist', |
@@ -89,6 +97,12 @@ export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistMod | |||
89 | }) | 97 | }) |
90 | RedundancyVideos: VideoRedundancyModel[] | 98 | RedundancyVideos: VideoRedundancyModel[] |
91 | 99 | ||
100 | static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, { | ||
101 | promise: true, | ||
102 | max: MEMOIZE_LENGTH.INFO_HASH_EXISTS, | ||
103 | maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS | ||
104 | }) | ||
105 | |||
92 | static doesInfohashExist (infoHash: string) { | 106 | static doesInfohashExist (infoHash: string) { |
93 | const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1' | 107 | const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1' |
94 | const options = { | 108 | const options = { |