mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 08:06:56 +00:00
청크 업로드 성능 개선 및 네트워크 속도를 더 정확하게 측정하도록 개선
This commit is contained in:
44
src/lib/utils/concurrency/BoundedQueue.ts
Normal file
44
src/lib/utils/concurrency/BoundedQueue.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
export class BoundedQueue<T> {
|
||||
private isClosed = false;
|
||||
private reservedCount = 0;
|
||||
private items: T[] = [];
|
||||
|
||||
private waitersNotFull: (() => void)[] = [];
|
||||
private waitersNotEmpty: (() => void)[] = [];
|
||||
|
||||
constructor(private readonly maxSize: number) {}
|
||||
|
||||
async push(item: T) {
|
||||
if (this.isClosed) {
|
||||
throw new Error("Queue closed");
|
||||
}
|
||||
|
||||
while (this.reservedCount >= this.maxSize) {
|
||||
await new Promise<void>((resolve) => this.waitersNotFull.push(resolve));
|
||||
if (this.isClosed) throw new Error("Queue closed");
|
||||
}
|
||||
|
||||
this.reservedCount++;
|
||||
this.items.push(item);
|
||||
this.waitersNotEmpty.shift()?.();
|
||||
}
|
||||
|
||||
async pop() {
|
||||
while (this.items.length === 0) {
|
||||
if (this.isClosed) return null;
|
||||
await new Promise<void>((resolve) => this.waitersNotEmpty.push(resolve));
|
||||
}
|
||||
|
||||
const item = this.items.shift()!;
|
||||
this.reservedCount--;
|
||||
this.waitersNotFull.shift()?.();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
close() {
|
||||
this.isClosed = true;
|
||||
while (this.waitersNotEmpty.length > 0) this.waitersNotEmpty.shift()!();
|
||||
while (this.waitersNotFull.length > 0) this.waitersNotFull.shift()!();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user