-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bottleneck
function
#145
Draft
aleclarson
wants to merge
2
commits into
main
Choose a base branch
from
feat/bottleneck-fn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as _ from 'radashi' | ||
import { bench } from 'vitest' | ||
|
||
describe('bottleneck', () => { | ||
bench('with no arguments', () => { | ||
_.bottleneck() | ||
}) | ||
}) | ||
|
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,14 @@ | ||
--- | ||
title: bottleneck | ||
description: Restrict a function's call frequency to a specified rate | ||
--- | ||
|
||
### Usage | ||
|
||
Does a thing. Returns a value. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
_.bottleneck() | ||
``` |
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,149 @@ | ||
declare const setTimeout: (callback: () => void, delay: number) => unknown | ||
|
||
/** | ||
* The options for the `bottleneck` function. | ||
* | ||
* @see https://radashi-org.github.io/reference/async/bottleneck | ||
*/ | ||
export interface BottleneckOptions { | ||
/** | ||
* The maximum number of calls to allow per interval. | ||
* | ||
* @default 1 | ||
*/ | ||
max?: number | ||
/** | ||
* The interval at which to allow the maximum number of calls. | ||
*/ | ||
interval: number | ||
/** | ||
* The maximum number of calls to allow at once. | ||
* | ||
* @default Infinity | ||
*/ | ||
concurrency?: number | ||
} | ||
|
||
/** | ||
* The return type of the `bottleneck` function. | ||
* | ||
* @see https://radashi-org.github.io/reference/async/bottleneck | ||
*/ | ||
export interface BottledFunction<TArgs extends any[], TReturn> { | ||
(...args: TArgs): Promise<TReturn> | ||
/** | ||
* Prevent any throttled calls from ever running. | ||
* | ||
* Currently executing calls are not affected. | ||
*/ | ||
cancel(): void | ||
} | ||
|
||
/** | ||
* Limit the rate at which a function is called. | ||
* | ||
* A maximum of `max` calls are allowed per `interval` milliseconds. | ||
* | ||
* Use the `concurrency` option for limiting the number of concurrent | ||
* calls. | ||
* | ||
* @see https://radashi-org.github.io/reference/async/bottleneck | ||
* @example | ||
* ```ts | ||
* const double = bottleneck( | ||
* { max: 1, interval: 1000 }, | ||
* async (x: number) => x * 2 | ||
* ) | ||
* double(1) // <- Runs immediately | ||
* double(2) // <- Will wait 1 second | ||
* double(3) // <- Will wait 2 seconds | ||
* ``` | ||
* @example Limited concurrency | ||
* ```ts | ||
* const double = bottleneck( | ||
* { max: 5, interval: 1000, concurrency: 1 }, | ||
* async (x: number) => x * 2 | ||
* ) | ||
* double(1) // <- Runs immediately | ||
* double(2) // <- Will wait for 1 to finish | ||
* double(3) // <- Will wait for 2 to finish | ||
* ``` | ||
*/ | ||
export function bottleneck<TArgs extends any[], TReturn>( | ||
{ | ||
max = 1, | ||
interval, | ||
concurrency = Number.POSITIVE_INFINITY, | ||
}: BottleneckOptions, | ||
fn: (...args: TArgs) => TReturn, | ||
): BottledFunction<TArgs, TReturn> { | ||
let numCalls = 0 | ||
let numRunning = 0 | ||
let startTime: number | undefined | ||
|
||
type QueueItem = { | ||
args: TArgs | ||
resolve: (value: TReturn | PromiseLike<TReturn>) => void | ||
reject: (error: any) => void | ||
} | ||
|
||
const queue: QueueItem[] = [] | ||
|
||
async function run(input: TArgs | QueueItem) { | ||
const now = Date.now() | ||
startTime ??= now | ||
|
||
if (now - startTime >= interval) { | ||
startTime = now | ||
numCalls = 0 | ||
} | ||
|
||
if (numCalls < max && numRunning < concurrency) { | ||
// If this is the first call, schedule the flush. | ||
if (!numCalls && Number.isFinite(interval)) { | ||
setTimeout(next, interval) | ||
} | ||
|
||
let result: any | ||
|
||
numCalls++ | ||
numRunning++ | ||
try { | ||
const args = Array.isArray(input) ? input : input.args | ||
result = await fn(...args) | ||
} catch (error) { | ||
if (Array.isArray(input)) { | ||
throw error | ||
} | ||
return input.reject(error) | ||
} finally { | ||
numRunning-- | ||
next() | ||
} | ||
|
||
return Array.isArray(input) ? result : input.resolve(result) | ||
} | ||
|
||
if (Array.isArray(input)) { | ||
// Return a queue promise for the throttled call. | ||
return new Promise<TReturn>((resolve, reject) => { | ||
queue.push({ args: input, resolve, reject }) | ||
}) | ||
} | ||
|
||
// Return the unused queue item to the queue. | ||
queue.unshift(input) | ||
} | ||
|
||
// This function is called when the interval has elapsed and after | ||
// every finished call. | ||
const next = () => queue.length && run(queue.shift()!) | ||
|
||
const bottled: BottledFunction<TArgs, any> = (...args) => run(args) | ||
|
||
bottled.cancel = () => { | ||
queue.length = 0 | ||
} | ||
|
||
return bottled | ||
} |
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,156 @@ | ||
import { flushMicroTasks } from 'flush-microtasks' | ||
import * as _ from 'radashi' | ||
|
||
const interval = 1000 | ||
const smidge = 10 | ||
|
||
describe('bottleneck', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
test('limit calls within interval', async () => { | ||
const fn = vi.fn((x: number) => x) | ||
const limitedFn = _.bottleneck({ max: 2, interval }, fn) | ||
|
||
limitedFn(1) | ||
limitedFn(2) | ||
limitedFn(3) | ||
|
||
expect(fn).toHaveBeenCalledTimes(2) | ||
expect(fn).toHaveBeenNthCalledWith(1, 1) | ||
expect(fn).toHaveBeenNthCalledWith(2, 2) | ||
|
||
vi.advanceTimersByTime(interval - smidge) | ||
expect(fn).toHaveBeenCalledTimes(2) | ||
|
||
vi.advanceTimersByTime(smidge) | ||
expect(fn).toHaveBeenCalledTimes(3) | ||
expect(fn).toHaveBeenNthCalledWith(3, 3) | ||
}) | ||
|
||
test('respect concurrency limit', async () => { | ||
const fn = vi.fn(async (x: number) => { | ||
await _.sleep(smidge) | ||
return x | ||
}) | ||
const limitedFn = _.bottleneck( | ||
{ max: Number.POSITIVE_INFINITY, interval, concurrency: 1 }, | ||
fn, | ||
) | ||
|
||
const promise1 = limitedFn(1) | ||
const promise2 = limitedFn(2) | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
vi.advanceTimersByTime(smidge) | ||
await flushMicroTasks() | ||
|
||
expect(fn).toHaveBeenCalledTimes(2) | ||
vi.advanceTimersByTime(interval) | ||
|
||
const [result1, result2] = await Promise.all([promise1, promise2]) | ||
|
||
expect(result1).toBe(1) | ||
expect(result2).toBe(2) | ||
}) | ||
|
||
test('queue calls beyond max limit', async () => { | ||
const fn = vi.fn((x: number) => x) | ||
const limitedFn = _.bottleneck({ max: 1, interval }, fn) | ||
|
||
const promise1 = limitedFn(1) | ||
const promise2 = limitedFn(2) | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
vi.advanceTimersByTime(smidge) | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
vi.advanceTimersByTime(interval) | ||
|
||
expect(fn).toHaveBeenCalledTimes(2) | ||
vi.advanceTimersByTime(interval) | ||
|
||
expect(fn).toHaveBeenCalledTimes(2) | ||
limitedFn(3) // <- Should run immediately. | ||
expect(fn).toHaveBeenCalledTimes(3) | ||
|
||
const result1 = await promise1 | ||
expect(result1).toBe(1) | ||
|
||
const result2 = await promise2 | ||
expect(result2).toBe(2) | ||
}) | ||
|
||
test('error thrown by first call does not affect queued calls', async () => { | ||
const fn = vi.fn(async (x: number) => { | ||
await _.sleep(smidge) | ||
if (x === 1) { | ||
throw new Error('test') | ||
} | ||
return x | ||
}) | ||
const limitedFn = _.bottleneck({ max: 1, interval }, fn) | ||
|
||
const promise1 = limitedFn(1) | ||
const promise2 = limitedFn(2) | ||
|
||
vi.advanceTimersByTime(smidge) | ||
await expect(promise1).rejects.toThrow('test') | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
|
||
vi.advanceTimersByTime(interval) | ||
await flushMicroTasks() | ||
|
||
expect(fn).toHaveBeenCalledTimes(2) | ||
|
||
await expect(promise2).resolves.toBe(2) | ||
}) | ||
|
||
test('error thrown by queued call does not affect other queued calls', async () => { | ||
const fn = vi.fn(async (x: number) => { | ||
await _.sleep(smidge) | ||
if (x === 2) { | ||
throw new Error('test') | ||
} | ||
return x | ||
}) | ||
const limitedFn = _.bottleneck({ max: 1, interval }, fn) | ||
|
||
const promise1 = limitedFn(1) | ||
const promise2 = limitedFn(2) | ||
const promise3 = limitedFn(3) | ||
|
||
vi.advanceTimersByTime(smidge) | ||
await expect(promise1).resolves.toBe(1) | ||
|
||
vi.advanceTimersByTime(interval) | ||
await expect(promise2).rejects.toThrow('test') | ||
|
||
vi.advanceTimersByTime(interval) | ||
await expect(promise3).resolves.toBe(3) | ||
}) | ||
|
||
describe('cancel method', () => { | ||
test('cancels all queued calls', async () => { | ||
const fn = vi.fn((x: number) => x) | ||
const limitedFn = _.bottleneck({ max: 1, interval }, fn) | ||
|
||
limitedFn(1) | ||
limitedFn(2) | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
limitedFn.cancel() | ||
|
||
vi.advanceTimersByTime(interval) | ||
await flushMicroTasks() | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can create a
QueueItem
here, push it in the queue and callnext()
. That should help us to simplifyrun
function torun(input: QueueItem)
.Something like this:
Probably we need to create our internal
withResolvers
util since that API is very new.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be interested in contributing that?
You can do this in terminal to create the files:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on the simplification, even if it means a little more GC work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a PR #148
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW @aleclarson
add-function
doesn't put an export intosrc/mod.ts
. (also, we can use https://github.com/google/zx instead of sh, but maybe it's overengineering)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I hope to fix that eventually 😅
The
add-function
script should have warned you though. Did it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, no 🤔