From 3f0ceab06e5320f62f593c49daa30d963dbc36f9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 22 May 2023 15:52:59 +0200 Subject: More robust quota check Avoid concurrency issues with permanent lives --- server/lib/live/live-manager.ts | 4 ++-- server/lib/live/live-quota-store.ts | 14 +++++++------- server/lib/live/shared/muxing-session.ts | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'server/lib') diff --git a/server/lib/live/live-manager.ts b/server/lib/live/live-manager.ts index f062e2fd3..5c6e69806 100644 --- a/server/lib/live/live-manager.ts +++ b/server/lib/live/live-manager.ts @@ -313,7 +313,7 @@ class LiveManager { const liveSession = await this.saveStartingSession(videoLive) const user = await UserModel.loadByLiveId(videoLive.id) - LiveQuotaStore.Instance.addNewLive(user.id, videoLive.id) + LiveQuotaStore.Instance.addNewLive(user.id, sessionId) const muxingSession = new MuxingSession({ context: this.getContext(), @@ -359,7 +359,7 @@ class LiveManager { muxingSession.on('after-cleanup', ({ videoUUID }) => { this.muxingSessions.delete(sessionId) - LiveQuotaStore.Instance.removeLive(user.id, videoLive.id) + LiveQuotaStore.Instance.removeLive(user.id, sessionId) muxingSession.destroy() diff --git a/server/lib/live/live-quota-store.ts b/server/lib/live/live-quota-store.ts index 8ceccde98..44539faaa 100644 --- a/server/lib/live/live-quota-store.ts +++ b/server/lib/live/live-quota-store.ts @@ -2,31 +2,31 @@ class LiveQuotaStore { private static instance: LiveQuotaStore - private readonly livesPerUser = new Map() + private readonly livesPerUser = new Map() private constructor () { } - addNewLive (userId: number, liveId: number) { + addNewLive (userId: number, sessionId: string) { if (!this.livesPerUser.has(userId)) { this.livesPerUser.set(userId, []) } - const currentUserLive = { liveId, size: 0 } + const currentUserLive = { sessionId, size: 0 } const livesOfUser = this.livesPerUser.get(userId) livesOfUser.push(currentUserLive) } - removeLive (userId: number, liveId: number) { + removeLive (userId: number, sessionId: string) { const newLivesPerUser = this.livesPerUser.get(userId) - .filter(o => o.liveId !== liveId) + .filter(o => o.sessionId !== sessionId) this.livesPerUser.set(userId, newLivesPerUser) } - addQuotaTo (userId: number, liveId: number, size: number) { + addQuotaTo (userId: number, sessionId: string, size: number) { const lives = this.livesPerUser.get(userId) - const live = lives.find(l => l.liveId === liveId) + const live = lives.find(l => l.sessionId === sessionId) live.size += size } diff --git a/server/lib/live/shared/muxing-session.ts b/server/lib/live/shared/muxing-session.ts index c672ec4d6..434b308e6 100644 --- a/server/lib/live/shared/muxing-session.ts +++ b/server/lib/live/shared/muxing-session.ts @@ -271,7 +271,7 @@ class MuxingSession extends EventEmitter { try { const segmentStat = await stat(segmentPath) - LiveQuotaStore.Instance.addQuotaTo(this.user.id, this.videoLive.id, segmentStat.size) + LiveQuotaStore.Instance.addQuotaTo(this.user.id, this.sessionId, segmentStat.size) const canUpload = await this.isAbleToUploadVideoWithCache(this.user.id) -- cgit v1.2.3