antifreeze2
v0.0.2
Published
Tiny helper to prevent the event loop from getting stuck in asynchronous routines
Downloads
17
Maintainers
Readme
antifreeze2
:star: Antifreeze for eventloop- let it always work :star:
Why
If you have a heavy synchronous task, you may use workers or just split the task to several async micro tasks/chunks to keep event loop always running, its pretty easy to do that with async function. But async function doesn't guarantee that all of its task scheduled by await will be executed really asynchronously and will not block the IO stage of the EventLoop. This simple package consists some helpers to ensure that the event loop is running, measuring the duration of the current event tick and allowing it go to the next tick if the maximum tick duration is exceeded.
Installation
Install for node.js using npm/yarn:
$ npm install antifreeze2 --save
$ yarn add antifreeze2
const { antifreeze, isNeeded, watchTick }= require('antifreeze2');
Usage examples
Example 1 - Fibonacci
For example, we need to calculate Fibonacci for 1,000,000 value. It's a task with heavy computation since it can take around 10s to complete. If we write the function as synchronous or just use an ECMA asynchronous function, the event loop will be blocked for that period. We won't be able to perform other tasks like accepting new connections, I/O events, timers, etc. because we only have one thread. To avoid this, we must ensure that the event loop tick duration does not exceed the allowed range of 15-20ms in order for the application to remain responsive.
By default, the desired event loop tick is set to 10ms. You can change it using watchTick(maxTick: number)
function.
See online demo
import {antifreeze, isNeeded} from "antifreeze2";
// A function with heavy computations
const fibAsync = async(n) => {
let a = 1n, b = 1n, sum, i = n - 2;
while (i-- > 0) {
sum = a + b;
a = b;
b = sum;
if (isNeeded()) { // If more than 10ms have passed since the last run of the eventloop cycle
await antifreeze(); // let the event loop get polled
}
}
return b;
};
// Test it - calculate Fibonacci for n= 1,000,000
(async (n) => {
let ts = Date.now();
let ticks = 0;
const timer = setInterval(() => {
const now = Date.now();
console.log(`Timer tick [${now - ts}ms]`);
ts = now;
ticks++;
}, 100);
const result = await fibAsync(n);
console.warn(`\nTimer ticks: ${ticks}\nFibonacci(${n}) = ${result}`)
clearTimeout(timer);
})(500000);
Optionally, to get the maximum performance, you can throttle the isNeeded()
call by using some counter:
const fibAsync = async(n) => {
let a = 1n, b = 1n, sum, i = n - 2;
while (i-- > 0) {
sum = a + b;
a = b;
b = sum;
// check only every 1000th cycle
if (!(i % 1000) && isNeeded()) { // If more than 10ms have passed since the last run of the eventloop cycle
await antifreeze(); // let the event loop get polled
}
}
return b;
};
Example 2 - koa server with heavy computation
The application has two endpoints:
Time request - light query with 20ms latency
Fibonacci request - heavy query that takes 10s to complete
Note that while a heavy request is being executed, the server continues to process light requests even though it is only running in one thread.
API
antifreeze2
- antifreeze2
- .watchTick(tick)
- .antifreeze() ⇒ Promise.<any> | null
- .isNeeded([maxTick]) ⇒ boolean
antifreeze2.watchTick(tick)
set interval for EventLoop delay checking
Kind: static method of antifreeze2
| Param | Type | Description | | --- | --- | --- | | tick | Number | checking interval. Set to 0 to disable the watcher. By default this value is set to 15(ms) |
antifreeze2.antifreeze() ⇒ Promise.<any> | null
Antifreeze promise injector
Kind: static method of antifreeze2
antifreeze2.isNeeded([maxTick]) ⇒ boolean
returns true if current event loop tick is delayed
Kind: static method of antifreeze2
| Param | Description | | --- | --- | | [maxTick] | max tick duration allowed |
Contribution
Feel free to fork, open issues, enhance or create pull requests.
License
The MIT License Copyright (c) 2019 Dmitriy Mozgovoy [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.