-
Notifications
You must be signed in to change notification settings - Fork 27
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 withSemaphore
& withMutex
function
#331
Open
hugo082
wants to merge
9
commits into
radashi-org:main
Choose a base branch
from
hugo082:main
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7fa9fe8
chore: init with-semaphore
hugo082 1cf7335
feat: withSemaphore implementation
hugo082 d781c8c
doc: withSemaphore usage
hugo082 c09a320
chore: prefix Permit type to avoid conflicts with other functions
hugo082 f47b346
feat: withMutex implementation
hugo082 acfa4c9
doc: withMutex usage
hugo082 f662c28
chore: fix coverage
hugo082 66bd2e2
chore: format
hugo082 157e5e7
chore: doc
hugo082 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
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,52 @@ | ||
--- | ||
title: withSemaphore | ||
description: A synchronization primitive for limiting concurrent usage to one | ||
since: 12.3.0 | ||
--- | ||
|
||
### Usage | ||
|
||
Creates a mutex-protected async function that limits concurrent execution to a single use at a time. Additional calls will wait for the mutex to be released before executing. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const exclusiveFn = _.withMutex(async () => { | ||
// Do stuff | ||
}) | ||
|
||
exclusiveFn() // run immediatly | ||
exclusiveFn() // wait until mutex is released | ||
``` | ||
|
||
#### Using with task based functions | ||
|
||
Execution function can be passed as a task parameter and ignored at the mutex creation. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const exclusiveFn = _.withMutex() | ||
|
||
exclusiveFn(async permit => { | ||
// Do stuff | ||
}) | ||
``` | ||
|
||
### Manual lock management | ||
|
||
The mutex can be manually acquired and released for fine-grained control over locking behavior. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const mutex = _.withMutex() | ||
|
||
const permit = await mutex.acquire() | ||
mutex.isLocked() // true | ||
|
||
// Do stuff | ||
|
||
permit.release() | ||
mutex.isLocked() // false | ||
``` |
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,66 @@ | ||
--- | ||
title: withSemaphore | ||
description: A synchronization primitive for limiting concurrent usage | ||
since: 12.3.0 | ||
--- | ||
|
||
### Usage | ||
|
||
Creates a [semaphore-protected](<https://en.wikipedia.org/wiki/Semaphore_(programming)>) async function that limits concurrent execution to a specified number of active uses. | ||
Additional calls will wait for previous executions to complete. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const exclusiveFn = _.withSemaphore(2, async () => { | ||
// Do stuff | ||
}) | ||
|
||
exclusiveFn() // run immediatly | ||
exclusiveFn() // run immediatly | ||
exclusiveFn() // wait until semaphore is released | ||
``` | ||
|
||
#### Using with task based functions | ||
|
||
Execution function can be passed as a task parameter and ignored at the semaphore creation. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const exclusiveFn = _.withSemaphore({ capacity: 2 }) | ||
|
||
exclusiveFn(async permit => { | ||
// Do stuff | ||
}) | ||
``` | ||
|
||
#### Weighted tasks | ||
|
||
Each task can require a specific weight from a semaphore. In this example two tasks each weighted with 2 from | ||
a semaphore with a capacity of 2. As a result they are mutually exclusive. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const exclusiveFn = _.withSemaphore({ capacity: 2 }) | ||
|
||
exclusiveFn({ weight: 2 }, async permit => {}) // run immediatly | ||
exclusiveFn({ weight: 2 }, async permit => {}) // wait until semaphore is released | ||
``` | ||
|
||
### Manual lock management | ||
|
||
The semaphore can be manually acquired and released for fine-grained control over locking behavior. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
const semaphore = _.withSemaphore({ capacity: 2 }) | ||
|
||
const permit = await semaphore.acquire() | ||
|
||
// Do stuff | ||
|
||
permit.release() | ||
``` |
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,58 @@ | ||
import { withSemaphore, type SemaphorePermit } from 'radashi' | ||
|
||
type AnyFn<T = unknown> = (permit: SemaphorePermit) => Promise<T> | ||
|
||
export interface Mutex { | ||
isLocked(): boolean | ||
acquire(): Promise<SemaphorePermit> | ||
release(): void | ||
} | ||
|
||
/** | ||
* Creates a mutex-protected instance with supplied function that limits concurrent execution to a single active use. | ||
* | ||
* @see https://radashi.js.org/reference/async/withSemaphore | ||
* @example | ||
* ```ts | ||
* const limitedFn = withMutex() | ||
* limitedFn(() => ...) | ||
* ``` | ||
*/ | ||
export function withMutex(): ExclusiveFn | ||
|
||
/** | ||
* Creates a mutex-protected instance with supplied function that limits concurrent execution to a single active use. | ||
* Supports direct invocation and dynamic function passing. | ||
* | ||
* @see https://radashi.js.org/reference/async/withMutex | ||
* @example | ||
* ```ts | ||
* const limitedFn = withMutex(() => ...) | ||
* limitedFn() | ||
* limitedFn(() => ...) | ||
* ``` | ||
*/ | ||
export function withMutex<T>(fn: AnyFn<T>): PrebuiltExclusiveFn<T> | ||
export function withMutex(baseFn?: AnyFn): PrebuiltExclusiveFn<unknown> { | ||
// @ts-expect-error because baseFn is not optional | ||
const semaphore = withSemaphore({ capacity: 1 }, baseFn) | ||
|
||
async function runExclusive(innerFn?: AnyFn): Promise<unknown> { | ||
// @ts-expect-error because innerFn is not optional | ||
return semaphore({ weight: 1 }, innerFn) | ||
} | ||
|
||
runExclusive.isLocked = () => semaphore.getRunning() > 0 | ||
runExclusive.acquire = () => semaphore.acquire(1) | ||
runExclusive.release = () => semaphore.release(1) | ||
|
||
return runExclusive | ||
} | ||
|
||
interface ExclusiveFn extends Mutex { | ||
<T>(fn: AnyFn<T>): Promise<T> | ||
} | ||
|
||
interface PrebuiltExclusiveFn<T> extends ExclusiveFn { | ||
(): Promise<T> | ||
} |
Oops, something went wrong.
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.
This usage is not supported by the current type definitions. I assume we'll want to update this example, rather than allow overriding the base function?