
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
basic-cluster
Advanced tools
A library for performing multiple tasks in parallel with control over resource usage.
npm install basic-cluster
The cluster controls the execution of multiple async tasks. It has a size which will control how many instances it can manage.
The cluster also accepts options for how it retries obtaining an instance to run submitted tasks. Retries are managed via exponential-backoff so check the docs over there for options.
An instance is simply an object managed by the cluster and acts as a context for
tasks to run in. While running a task, an instance is considered busy
and it
cannot accept a new task until the current one completes.
Task is what you submit to the cluster for it to run when possible.
Here's what you do:
The BasicCluster
class makes it easy to run a bunch of parallel tasks which
don't depend on a managed instance object.
import { BasicCluster } from './src/cluster/BasicCluster';
const clusterSize = 3;
const cluster: BasicCluster = new BasicCluster(clusterSize);
const result = cluster.submit(async () => {
// Do something
});
If you no longer require the cluster, you can shut it down. There are two options for this:
Cluster#shutdown()
will attempt to wait for any running task to complete
before shutting down.
Cluster#shutdownNow()
will forcefully shutdown the cluster, calling
shutdown()
on all its instances immediately.
You can create clusters that use (potentially complex) instance objects, reusing them for new tasks. The example that inspired this package was a cluster of Puppeteer browser instances, which take some time to initiate and, as such, are a prime candidate for pooling.
To do so, you can implement the Instance
interface and use the Cluster
class
directly with your instance.
The SimpleInstance
is a utility for when you need an instance with some state
and the shutdown is a no-op. Here's an example:
let i = 0;
const cluster: Cluster<number> = new Cluster(3, () => new SimpleInstance(++i));
cluster.submit((instance) => {
console.log(`Running task on instance ${instance.getValue()}`);
// do something with the instance
});
FAQs
Run multiple async tasks in parallel
The npm package basic-cluster receives a total of 10 weekly downloads. As such, basic-cluster popularity was classified as not popular.
We found that basic-cluster demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.