https://github.com/lbfalvy/buffered-dispatch
Buffer function calls and dispatch them on servers as they become available
https://github.com/lbfalvy/buffered-dispatch
command-pattern load-balancing
Last synced: about 1 month ago
JSON representation
Buffer function calls and dispatch them on servers as they become available
- Host: GitHub
- URL: https://github.com/lbfalvy/buffered-dispatch
- Owner: lbfalvy
- License: mit
- Created: 2022-03-07T00:08:26.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-02-08T18:40:55.000Z (over 3 years ago)
- Last Synced: 2024-04-28T06:28:28.983Z (about 2 years ago)
- Topics: command-pattern, load-balancing
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/buffered-dispatch
- Size: 106 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Buffered Dispatch
Buffer function calls and dispatch them on servers as they become available
Think of a load balancer balancing a cluster of HTTP servers. It accepts
requests, dispatches them on the first available server, then
forward the response back to the client. This utility is a lot like that,
except it doesn't keep track of responses and servers signal when they're
ready to accept the next request explicitly
## Usage
Here we have constructed a buffered dispatch with two number arguments
and a number return value, representing a division operation.
```ts
import { bufferedDispatch } from 'buffered-dispatch'
const [requestDiv, serveDiv] = bufferedDispatch<[number, number], number>()
```
This would go in something like a HTTP request handler, an asynchronous
function that represents a client. Any number of clients can call
`request` concurrently
```ts
const result = await requestDiv(3, 5)
```
This is a grossly oversimplified server. Any number of servers can call
`serve` concurrently, they will be resolved as clients arrive. Once a
server and a client have been matched the dispatcher forgets about both
of them
```ts
while (true) {
const { resolve, reject, args: [num, denom] } = await serveDiv()
if (denom == 0) reject(new Error('Division by zero'))
else resolve(num / denom)
}
```