Skip to content

Commit

Permalink
Fix Sempahore
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Apr 7, 2024
1 parent 9f227b1 commit 25159a8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "floatplane-plex-downloader",
"version": "5.12.2",
"version": "5.13.0",
"private": true,
"type": "module",
"scripts": {
Expand Down Expand Up @@ -53,4 +53,4 @@
"pkg": "^5.8.1",
"typescript": "^5.4.3"
}
}
}
2 changes: 1 addition & 1 deletion src/lib/VideoBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class VideoBase extends Attachment {
} = await fApi.cdn.delivery("download", this.attachmentId);

// Release the semaphore after DeliveryTimeout
setTimeout(VideoBase.DeliverySemaphore.release, VideoBase.DeliveryTimeout);
setTimeout(() => VideoBase.DeliverySemaphore.release(), VideoBase.DeliveryTimeout);

return delivery;
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib/helpers/Semaphore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
export class Semaphore {
private avalibleSlots: number;
private readonly _queue: (() => void)[];
private readonly queued: (() => void)[] = [];

constructor(slots: number) {
this.avalibleSlots = slots;
this._queue = [];
}

public async obtain() {
// If there is an available request slot, proceed immediately
if (this.avalibleSlots > 0) return this.avalibleSlots--;

// Otherwise, wait for a request slot to become available
return new Promise((r) => this._queue.push(() => r(this.avalibleSlots--)));
return new Promise((r) => this.queued.push(() => r(this.avalibleSlots--)));
}

public release(): void {
this.avalibleSlots++;
// If there are queued requests, resolve the first one in the queue
this._queue.shift()?.();
this.queued.shift()?.();
}
}
2 changes: 1 addition & 1 deletion src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "dotenv/config";
import json5 from "json5";
const { parse } = json5;

export const DownloaderVersion = "5.12.2";
export const DownloaderVersion = "5.13.0";

import type { PartialArgs, Settings } from "../types.js";

Expand Down

0 comments on commit 25159a8

Please sign in to comment.