buffered-dispatch
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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.

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

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

while (true) {
	const { resolve, reject, args: [num, denom] } = await serveDiv()
	if (denom == 0) reject(new Error('Division by zero'))
	else resolve(num / denom)
}

Package Sidebar

Install

npm i buffered-dispatch

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

13.9 kB

Total Files

6

Last publish

Collaborators

  • lbfalvy