refactor: move locks into StreamController

This commit is contained in:
WebStreamr 2025-09-01 10:58:05 +00:00
parent 4d05ac859f
commit e109637e48
No known key found for this signature in database

View file

@ -5,8 +5,6 @@ import winston from 'winston';
import { Source } from '../source';
import { contextFromRequestAndResponse, envIsProd, Id, ImdbId, StreamResolver, TmdbId } from '../utils';
const locks = new Map<string, Mutex>();
export class StreamController {
public readonly router: Router;
@ -14,6 +12,8 @@ export class StreamController {
private readonly sources: Source[];
private readonly streamResolver: StreamResolver;
private readonly locks = new Map<string, Mutex>();
public constructor(logger: winston.Logger, sources: Source[], streams: StreamResolver) {
this.router = Router();
@ -44,10 +44,10 @@ export class StreamController {
const sources = this.sources.filter(source => source.countryCodes.filter(countryCode => countryCode in ctx.config).length);
let mutex = locks.get(rawId);
let mutex = this.locks.get(rawId);
if (!mutex) {
mutex = new Mutex();
locks.set(rawId, mutex);
this.locks.set(rawId, mutex);
}
await mutex.runExclusive(async () => {
@ -62,7 +62,7 @@ export class StreamController {
});
if (!mutex.isLocked()) {
locks.delete(rawId);
this.locks.delete(rawId);
}
};
}