Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
auto-reset-event
Advanced tools
Changelog
[1.0.0] - 2018-10-03
Readme
This package mimic the behavior of AutoResetEvent
of C#, an acquire-release one semantic.
Although JavaScript is single-threaded, one might want to limit number of concurrent asynchronous operations. For example, a task that requires multiple asynchronous steps to complete can use this package to limit its concurrency.
Run npm install auto-reset-event
.
import createAutoResetEvent from 'auto-reset-event';
// Create an acquire-release queue
const acquire = createAutoResetEvent();
// Acquire a single lock, this function will not be resolved until the lock is acquired
const release = await acquire();
// Do the non-parallelized asynchronous work
// Release the lock
release();
import createAutoResetEvent from 'auto-reset-event';
import delay from 'delay';
async function main() {
const acquire = createAutoResetEvent();
await Promise.all([
(async () => {
const release = await acquire();
console.log('You will see this first.');
await delay(1000);
release();
})(),
(async () => {
const release = await acquire();
console.log('You will see this after a second.');
await delay(1000);
release();
})(),
]);
console.log('You will see this last, after two seconds.');
}
main().catch(err => console.error(err));
auto-reset-event
can be used to limit concurrency for asynchronous operations.
We can use auto-reset-event
to easily slow down concurrent requests by limiting its concurrency and serving rate. In this sample, we slowed down serving static content by serving one request every second.
import { createServer } from 'restify';
import createAutoResetEvent from 'auto-reset-event';
import delay from 'delay';
import serveHandler from 'serve-handler';
const server = createServer();
const acquireSlowLock = createAutoResetEvent();
server.get('/public/*', async (req, res) => {
// If ?slow is added to the URL, we will slow it down by serving one request every second
if ('slow' in req.query) {
// Make sure it is not cached, so we can replay the slow behavior easily
res.noCache();
const release = await acquireSlowLock();
await delay(1000);
release();
}
await serveHandler(req, res, { path: join(__dirname, './public') });
});
server.listen(process.env.PORT || 80);
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.
FAQs
An acquire-release one semantic implemented in Promise.
The npm package auto-reset-event receives a total of 11 weekly downloads. As such, auto-reset-event popularity was classified as not popular.
We found that auto-reset-event demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.