This commit is contained in:
2023-09-22 21:13:33 +09:00
parent 295d39327b
commit 438ee85ed2
24 changed files with 539 additions and 40 deletions
+23
View File
@@ -0,0 +1,23 @@
{
"name": "@sammo/server_util",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build"
},
"type": "module",
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@sammo/api_def": "workspace:^",
"@sammo/util": "workspace:^"
},
"devDependencies": {
"lodash-es": "^4.17.21"
},
"peerDependencies": {
"lodash-es": "^4.17.21"
}
}
@@ -0,0 +1,198 @@
export interface StateIncrementer {
(increase: number): Promise<number>;
}
export type NumberGroup = {
start: number;
remain: number;
}
const AllocatorGroupBucketSize = 100;
const AllocatorPreserveThreshold = 50;
export function* numberGenerator(numberGroups: NumberGroup[]): Generator<number, null> {
for (const numberGroup of numberGroups) {
for (let i = 0; i < numberGroup.remain; i++) {
yield numberGroup.start + i;
}
}
return null;
}
export class UniqueNumberAllocator {
private preservedNumbers: NumberGroup[];
private incrementer: StateIncrementer;
private totalRemain: number;
private promisedCnt: number;
private promise: Promise<void>;
public constructor(incrementer: StateIncrementer, initialState?: NumberGroup) {
this.incrementer = incrementer;
if (!initialState) {
this.preservedNumbers = [];
this.totalRemain = 0;
}
else {
if (initialState.remain < 1) {
throw new Error('initialState.remain must be greater than 0');
}
this.preservedNumbers = [initialState];
this.totalRemain = initialState.remain;
}
this.promisedCnt = 0;
this.promise = Promise.resolve();
this.preserveNext();
}
public static async generate(incrementer: StateIncrementer): Promise<UniqueNumberAllocator> {
const initialStateEnd = await incrementer(AllocatorGroupBucketSize);
const initialState: NumberGroup = {
start: initialStateEnd - AllocatorGroupBucketSize + 1,
remain: AllocatorGroupBucketSize,
}
return new UniqueNumberAllocator(incrementer, initialState);
}
private preserveNext(): void {
if (this.promisedCnt + this.totalRemain >= AllocatorGroupBucketSize) {
return;
}
if (this.totalRemain >= AllocatorPreserveThreshold) {
return;
}
const waiter = this.promise;
this.promise = (async () => {
this.promisedCnt += AllocatorGroupBucketSize;
const nextP = this.incrementer(AllocatorGroupBucketSize);
await waiter;
const next = await nextP;
if (this.preservedNumbers.length > 0) {
const last = this.preservedNumbers[this.preservedNumbers.length - 1];
if (last.start + last.remain === next - AllocatorGroupBucketSize + 1) {
// 마지막 그룹과 연속됨
last.remain += AllocatorGroupBucketSize;
this.totalRemain += AllocatorGroupBucketSize;
this.promisedCnt -= AllocatorGroupBucketSize;
return;
}
}
const nextGroup: NumberGroup = {
start: next - AllocatorGroupBucketSize + 1,
remain: AllocatorGroupBucketSize,
}
this.preservedNumbers.push(nextGroup);
this.totalRemain += AllocatorGroupBucketSize;
this.promisedCnt -= AllocatorGroupBucketSize;
})();
}
public async allocateOne(): Promise<number> {
if (this.totalRemain > 0) {
const head = this.preservedNumbers[0];
if (head.remain > 1) {
const next = head.start;
head.start++;
head.remain--;
this.totalRemain--;
this.preserveNext();
return next;
}
else {
this.preservedNumbers.shift();
this.totalRemain--;
this.preserveNext();
return head.start;
}
}
const next = (await this.allocate(1)).next().value;
if (next === null) {
throw new Error('incrementer failed to allocate enough numbers');
}
return next;
}
public async allocate(n: number): Promise<Generator<number, null>> {
n = Math.ceil(n);
if (n < 1) {
throw new Error('n must be greater than 0');
}
if (n > this.totalRemain + this.promisedCnt) {
const endP = this.incrementer(n);
this.preserveNext();
const end = await endP;
return numberGenerator([{
start: end - n + 1,
remain: n,
}]);
}
const result: NumberGroup[] = [];
if (this.totalRemain > 0) {
const head = this.preservedNumbers[0];
const headRemain = head.remain;
if (n < headRemain) {
result.push({
start: head.start,
remain: n,
});
head.start += n;
head.remain -= n;
this.totalRemain -= n;
this.preserveNext();
return numberGenerator(result);
}
if (n === headRemain) {
this.preservedNumbers.shift();
result.push(head);
this.totalRemain -= n;
this.preserveNext();
return numberGenerator(result);
}
}
//assert n <= this.totalRemain + this.promisedRemain
if (n > this.totalRemain) {
await this.promise;
if (n > this.totalRemain) {
throw new Error('incrementer failed to allocate enough numbers');
}
}
this.totalRemain -= n;
this.preserveNext();
let remain = n;
let idx = 0;
while (remain > 0) {
const head = this.preservedNumbers[idx];
if (head.remain <= remain) {
remain -= head.remain;
result.push(head);
idx++;
continue;
}
result.push({
start: head.start,
remain: remain,
});
head.start += remain;
head.remain -= remain;
remain = 0;
break;
}
this.preservedNumbers = this.preservedNumbers.slice(idx);
return numberGenerator(result);
}
}
+1
View File
@@ -0,0 +1 @@
export { UniqueNumberAllocator, type StateIncrementer } from './UniqueNumberAllocator.js';
+33
View File
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "node16",
"moduleResolution": "node16",
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"importHelpers": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"skipLibCheck": true,
"incremental": true,
"rootDir": "./src",
"outDir": "./dist",
"composite": true
},
"references": [
{
"path": "../util"
},
{
"path": "../crypto"
},
]
}