-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: optional rate limiter on concurrent sitemap functions execut…
…ed (#62)
- Loading branch information
1 parent
f0a6e9a
commit aacec50
Showing
5 changed files
with
190 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { RateLimiter } from '../rate-limiter'; | ||
|
||
describe('RateLimiter', () => { | ||
it('should not allow more than the rate limit to run concurrently', async () => { | ||
const limit = new RateLimiter(3); | ||
let activeTasks = 0; | ||
|
||
const increaseActive = () => { | ||
activeTasks++; | ||
}; | ||
|
||
const decreaseActive = () => { | ||
activeTasks--; | ||
limit.free(); | ||
}; | ||
|
||
const tasks = Array(5) | ||
.fill(null) | ||
.map(async () => { | ||
await limit.allocate(); | ||
|
||
increaseActive(); | ||
expect(activeTasks).toBeLessThanOrEqual(3); | ||
|
||
// Mock task duration (randomness to affect completion order) | ||
await new Promise(resolve => | ||
setTimeout(resolve, 500 + 500 * Math.random()) | ||
); | ||
decreaseActive(); | ||
}); | ||
await Promise.all(tasks); | ||
}); | ||
|
||
it('should queue tasks and execute them in order', async () => { | ||
const limit = new RateLimiter(1); | ||
const taskOrder: number[] = []; | ||
const mockTask = async (id: number) => { | ||
await limit.allocate(); | ||
|
||
// Make each task successively longer to ensure execution completion order | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
taskOrder.push(id); | ||
limit.free(); | ||
}; | ||
|
||
const tasks = [1, 2, 3, 4].map(id => mockTask(id)); | ||
await Promise.all(tasks); | ||
expect(taskOrder).toEqual([1, 2, 3, 4]); // Ensure tasks are executed in the expected order | ||
expect(limit.getProcessing()).toEqual(0); | ||
expect(limit.getWaiting()).toEqual(0); | ||
}); | ||
|
||
it('ensure too many tasks never run at once', async () => { | ||
const maxConcurrent = 20; | ||
const limit = new RateLimiter(maxConcurrent); | ||
const mockTask = async () => { | ||
await limit.allocate(); | ||
|
||
expect(limit.getProcessing()).toBeLessThanOrEqual(maxConcurrent); | ||
|
||
// Mock task duration (randomness to affect completion order) | ||
await new Promise(resolve => setTimeout(resolve, 500 * Math.random())); | ||
|
||
expect(limit.getProcessing()).toBeLessThanOrEqual(maxConcurrent); | ||
|
||
limit.free(); | ||
}; | ||
await Promise.all( | ||
Array(100) | ||
.fill(0) | ||
.map(() => mockTask()) | ||
); | ||
|
||
expect(limit.getProcessing()).toEqual(0); | ||
expect(limit.getWaiting()).toEqual(0); | ||
}); | ||
|
||
it('should handle rapid calls to free correctly', async () => { | ||
const limit = new RateLimiter(1); | ||
const mockTask = async () => { | ||
await limit.allocate(); | ||
limit.free(); | ||
limit.free(); // Intentional rapid call to free | ||
}; | ||
|
||
await Promise.all([mockTask(), mockTask()]); | ||
expect(limit.getProcessing()).toEqual(0); | ||
expect(limit.getWaiting()).toEqual(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Usage: | ||
* const limit = new RateLimiter(3); | ||
* Promise.all(arr.map(async x => { | ||
* await limit.allocate(); // waits until there are less than three active | ||
* await longTask(x); | ||
* limit.free(); // mark the task as done, allowing another to start | ||
* })) | ||
*/ | ||
|
||
export class RateLimiter { | ||
rate: number; | ||
queue: Array<() => void>; | ||
active: number; | ||
|
||
constructor(rate: number) { | ||
this.rate = rate; | ||
this.queue = []; | ||
this.active = 0; | ||
} | ||
|
||
allocate(): Promise<void> { | ||
return new Promise(res => { | ||
if (this.active < this.rate) { | ||
this.active++; | ||
res(); | ||
} else { | ||
this.queue.push(res); | ||
} | ||
}); | ||
} | ||
|
||
free(): void { | ||
const first = this.queue.shift(); | ||
if (first) { | ||
// Don't change activity count | ||
// just substitute the finished work with new work | ||
first(); | ||
} else { | ||
this.active = Math.max(0, this.active - 1); | ||
} | ||
} | ||
|
||
getProcessing(): number { | ||
return this.active; | ||
} | ||
getWaiting(): number { | ||
return this.queue.length; | ||
} | ||
} |