-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(limits): Concise implementation for workers/global/limits.ts (…
…#7140) Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
- Loading branch information
Showing
7 changed files
with
81 additions
and
56 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
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 |
---|---|---|
@@ -1,21 +1,43 @@ | ||
import { getLimitRemaining, incrementLimit, init } from './limits'; | ||
import { | ||
Limit, | ||
incLimitedValue, | ||
isLimitReached, | ||
resetAllLimits, | ||
setMaxLimit, | ||
} from './limits'; | ||
|
||
describe('lib/workers/global/limits', () => { | ||
describe('init()', () => { | ||
it('check defined variables have a value set to zero', () => { | ||
const config = { prCommitsPerRunLimit: 3 }; | ||
init(config); | ||
const result = getLimitRemaining('prCommitsPerRunLimit'); | ||
expect(result).toEqual(3); | ||
}); | ||
beforeEach(() => { | ||
resetAllLimits(); | ||
}); | ||
describe('incrementLimit()', () => { | ||
it('check increment works as expected', () => { | ||
const config = { prCommitsPerRunLimit: 3 }; | ||
init(config); | ||
incrementLimit('prCommitsPerRunLimit', 2); | ||
const result = getLimitRemaining('prCommitsPerRunLimit'); | ||
expect(result).toEqual(1); | ||
}); | ||
|
||
beforeEach(() => { | ||
resetAllLimits(); | ||
}); | ||
|
||
it('increments limited value', () => { | ||
setMaxLimit(Limit.Commits, 3); | ||
|
||
expect(isLimitReached(Limit.Commits)).toBe(false); | ||
|
||
incLimitedValue(Limit.Commits, 2); | ||
expect(isLimitReached(Limit.Commits)).toBe(false); | ||
|
||
incLimitedValue(Limit.Commits); | ||
expect(isLimitReached(Limit.Commits)).toBe(true); | ||
|
||
incLimitedValue(Limit.Commits); | ||
expect(isLimitReached(Limit.Commits)).toBe(true); | ||
}); | ||
|
||
it('defaults to unlimited', () => { | ||
expect(isLimitReached(Limit.Commits)).toBe(false); | ||
}); | ||
|
||
it('increments undefined', () => { | ||
incLimitedValue(Limit.Commits); | ||
expect(isLimitReached(Limit.Commits)).toBe(false); | ||
setMaxLimit(Limit.Commits, 1); | ||
expect(isLimitReached(Limit.Commits)).toBe(true); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,41 +1,44 @@ | ||
import { logger } from '../../logger'; | ||
|
||
const limitsToInit = ['prCommitsPerRunLimit']; | ||
const l: Record<string, number> = {}; | ||
const v: Record<string, number> = {}; | ||
export enum Limit { | ||
Commits = 'Commits', | ||
} | ||
|
||
export function setLimit(name: string, value: number): void { | ||
logger.debug(`Limits.setLimit l[${name}] = ${value}`); | ||
l[name] = value; | ||
interface LimitValue { | ||
max: number | null; | ||
current: number; | ||
} | ||
|
||
export function init(config: Record<string, any>): void { | ||
logger.debug(`Limits.init enter method`); | ||
for (const limit of limitsToInit) { | ||
logger.debug(`Limits.init ${limit} processing`); | ||
if (config[limit]) { | ||
setLimit(limit, config[limit]); | ||
v[limit] = 0; | ||
} else { | ||
logger.debug( | ||
`Limits.init ${limit} variable is not set. Ignoring ${limit}` | ||
); | ||
} | ||
} | ||
const limits = new Map<Limit, LimitValue>(); | ||
|
||
export function resetAllLimits(): void { | ||
limits.clear(); | ||
} | ||
|
||
export function getLimitRemaining(name: string): number { | ||
let result; | ||
if (typeof v[name] !== 'undefined') { | ||
result = l[name] - v[name]; | ||
} else { | ||
result = undefined; | ||
} | ||
return result; | ||
export function setMaxLimit(key: Limit, max: unknown): void { | ||
const maxVal = typeof max === 'number' && max > 0 ? max : null; | ||
logger.debug(`${key} limit = ${maxVal}`); | ||
const limit = limits.get(key); | ||
limits.set(key, { | ||
current: 0, | ||
...limit, | ||
max: maxVal, | ||
}); | ||
} | ||
|
||
export function incLimitedValue(key: Limit, incBy = 1): void { | ||
const limit = limits.get(key) || { max: null, current: 0 }; | ||
limits.set(key, { | ||
...limit, | ||
current: limit.current + incBy, | ||
}); | ||
} | ||
|
||
export function incrementLimit(name: string, value = 1): void { | ||
if (typeof v[name] !== 'undefined') { | ||
v[name] += value; | ||
export function isLimitReached(key: Limit): boolean { | ||
const limit = limits.get(key); | ||
if (!limit || limit.max === null) { | ||
return false; | ||
} | ||
const { max, current } = limit; | ||
return max - current <= 0; | ||
} |
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