From 557b13ae24019d9ab214bbea7eaa0f892c8f4b05 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Aug 2019 11:32:40 +0200 Subject: Lazy load avatars --- server/initializers/constants.ts | 22 ++++++-- server/initializers/migrations/0420-avatar-lazy.ts | 60 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 server/initializers/migrations/0420-avatar-lazy.ts (limited to 'server/initializers') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index b9d90b2bd..3dc178b11 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -14,7 +14,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 415 +const LAST_MIGRATION_VERSION = 420 // --------------------------------------------------------------------------- @@ -498,6 +498,11 @@ const STATIC_DOWNLOAD_PATHS = { TORRENTS: '/download/torrents/', VIDEOS: '/download/videos/' } +const LAZY_STATIC_PATHS = { + AVATARS: '/lazy-static/avatars/', + PREVIEWS: '/static/previews/', + VIDEO_CAPTIONS: '/static/video-captions/' +} // Cache control let STATIC_MAX_AGE = { @@ -536,9 +541,12 @@ const FILES_CACHE = { } } -const CACHE = { +const LRU_CACHE = { USER_TOKENS: { - MAX_SIZE: 10000 + MAX_SIZE: 1000 + }, + AVATAR_STATIC: { + MAX_SIZE: 500 } } @@ -549,6 +557,10 @@ const MEMOIZE_TTL = { OVERVIEWS_SAMPLE: 1000 * 3600 * 4 // 4 hours } +const QUEUE_CONCURRENCY = { + AVATAR_PROCESS_IMAGE: 3 +} + const REDUNDANCY = { VIDEOS: { RANDOMIZED_FACTOR: 5 @@ -649,6 +661,7 @@ export { WEBSERVER, API_VERSION, PEERTUBE_VERSION, + LAZY_STATIC_PATHS, HLS_REDUNDANCY_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, AVATARS_SIZE, @@ -695,11 +708,12 @@ export { VIDEO_PRIVACIES, VIDEO_LICENCES, VIDEO_STATES, + QUEUE_CONCURRENCY, VIDEO_RATE_TYPES, VIDEO_TRANSCODING_FPS, FFMPEG_NICE, VIDEO_ABUSE_STATES, - CACHE, + LRU_CACHE, JOB_REQUEST_TIMEOUT, USER_PASSWORD_RESET_LIFETIME, MEMOIZE_TTL, diff --git a/server/initializers/migrations/0420-avatar-lazy.ts b/server/initializers/migrations/0420-avatar-lazy.ts new file mode 100644 index 000000000..5fc57aac2 --- /dev/null +++ b/server/initializers/migrations/0420-avatar-lazy.ts @@ -0,0 +1,60 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize, + db: any +}): Promise { + { + // We'll add a unique index on filename, so delete duplicates or PeerTube won't start + const query = 'DELETE FROM "avatar" s1 ' + + 'USING (SELECT MIN(id) as id, filename FROM "avatar" GROUP BY "filename" HAVING COUNT(*) > 1) s2 ' + + 'WHERE s1."filename" = s2."filename" AND s1.id <> s2.id' + await utils.sequelize.query(query) + } + + { + const data = { + type: Sequelize.STRING, + allowNull: true, + defaultValue: null + } + + await utils.queryInterface.addColumn('avatar', 'fileUrl', data) + } + + { + const data = { + type: Sequelize.BOOLEAN, + allowNull: true, + defaultValue: null + } + + await utils.queryInterface.addColumn('avatar', 'onDisk', data) + } + + { + const query = 'UPDATE "avatar" SET "onDisk" = true;' + await utils.sequelize.query(query) + } + + { + const data = { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: null + } + + await utils.queryInterface.changeColumn('avatar', 'onDisk', data) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3