-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into remove-bluebird
- Loading branch information
Showing
8 changed files
with
157 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export const MAX_BUFFER_SIZE = 100 * 1024 * 1024; // 100 MiB | ||
const THRESHOLD = 0.15; | ||
|
||
export class CircularBuffer { | ||
private _buf: Buffer[]; | ||
private _size: number; | ||
private _maxSize: number; | ||
|
||
constructor(maxSize = MAX_BUFFER_SIZE) { | ||
this._maxSize = maxSize; | ||
this._buf = []; | ||
this._size = 0; | ||
} | ||
|
||
get size(): number { | ||
return this._size; | ||
} | ||
|
||
get count(): number { | ||
return this._buf.length; | ||
} | ||
|
||
public add(item: Buffer): this { | ||
this._buf.push(item); | ||
this._size += item.length; | ||
this._align(); | ||
return this; | ||
} | ||
|
||
public value(): Buffer { | ||
return Buffer.concat(this._buf); | ||
} | ||
|
||
private _align(): void { | ||
if (this._size <= this._maxSize) { | ||
return; | ||
} | ||
|
||
let numberOfItemsToShift = 0; | ||
// We add the threshold to avoid shifting the array for each `add` call, | ||
// which reduces the CPU usage | ||
const expectedSizeToShift = this._size - this._maxSize + Math.trunc(this._maxSize * THRESHOLD); | ||
let actualShiftedSize = 0; | ||
while (numberOfItemsToShift < this._buf.length - 1 && actualShiftedSize <= expectedSizeToShift) { | ||
actualShiftedSize += this._buf[numberOfItemsToShift].length; | ||
numberOfItemsToShift++; | ||
} | ||
if (numberOfItemsToShift > 0) { | ||
this._buf.splice(0, numberOfItemsToShift); | ||
this._size -= actualShiftedSize; | ||
} | ||
if (actualShiftedSize < expectedSizeToShift) { | ||
// We have already deleted all buffer items, but one, | ||
// although the recent item is still too big to fit into the allowed size limit | ||
const remainderToShift = expectedSizeToShift - actualShiftedSize; | ||
this._buf[0] = this._buf[0].subarray(remainderToShift); | ||
this._size -= remainderToShift; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { CircularBuffer } from '../lib/circular-buffer'; | ||
|
||
|
||
describe('CircularBuffer', function () { | ||
let chai; | ||
|
||
before(async function() { | ||
chai = await import('chai'); | ||
const chaiAsPromised = await import('chai-as-promised'); | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised.default); | ||
}); | ||
|
||
it('should properly rotate', function () { | ||
const maxSize = 100; | ||
const buffer = new CircularBuffer(maxSize); | ||
buffer.count.should.equal(0); | ||
buffer.size.should.equal(0); | ||
buffer.add(Buffer.from('x'.repeat(maxSize))); | ||
buffer.count.should.equal(1); | ||
buffer.size.should.equal(maxSize); | ||
buffer.value().should.eql(Buffer.from('x'.repeat(maxSize))); | ||
buffer.add(Buffer.from('y'.repeat(maxSize))); | ||
buffer.count.should.equal(1); | ||
buffer.size.should.equal(85); | ||
buffer.value().should.eql(Buffer.from('y'.repeat(85))); | ||
}); | ||
|
||
it('should properly rotate if the incoming value is too large', function () { | ||
const maxSize = 100; | ||
const buffer = new CircularBuffer(maxSize); | ||
buffer.count.should.equal(0); | ||
buffer.size.should.equal(0); | ||
buffer.add(Buffer.from('x'.repeat(maxSize))); | ||
buffer.count.should.equal(1); | ||
buffer.size.should.equal(maxSize); | ||
buffer.value().should.eql(Buffer.from('x'.repeat(maxSize))); | ||
buffer.add(Buffer.from('y'.repeat(maxSize + 10))); | ||
buffer.count.should.equal(1); | ||
buffer.size.should.equal(85); | ||
buffer.value().should.eql(Buffer.from('y'.repeat(85))); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters