npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ema-timeout

v1.0.3

Published

通过ema算法动态计算延时时间

Downloads

1

Readme

ema超时算法

NPM version npm download

EMA算法本身是多用于金融行业的,是一种基于增加期望的模型。算法的主要思想是综合利用历史上积累到的数据,预测下一个周期内的期望。我们参考了标准的EMA算法,得到了符合互联网业务特点的用于超时控制的EMA算法。具体的思想主要是引入“平均效应”的概念,用平均响应时间代替固定超时时间,只要平均响应时间没有超时即可,而不是要求每次都不能超时。主要几点:总体情况不能超标;平均情况表现越好,弹性越大;平均情况表现越差,弹性越小。这里的弹性指的是允许毛刺情况的最大响应时间。

Install

$ npm i ema-timeout --save

Usage

const ema = require('ema-timeout');

// 因为每个cgi有可能不一样,根据自身业务需求考虑是否要为每个接口create不同的ema计算
const options = {
        Tavg: 55,   //  最低响应时间, 一般用平均响应时间替代
        Thwm: 300,  // 超时时间限制, 确保最坏的时候,所有请求能处理。正常时正确处理的成功率满足需求。
        Tmax: 500,  // 最大弹性时间
        N: 90   // 平滑指数,越小表示平无值越受最近值的影响,太大则对异常响应较慢
    };
const emaObj = ema.create(options);
const start = Date.now();
let tdto = emaObj.get(); // 获取当前超时设置时间

// 请求接口指定超时时间
// 这里是你业务请求逻辑
try {
    const result = await request({
        timeout: tdto
    });// 请求接口 

    emaObj.update(Date.now() - start); // 这里是更新当前接口延时时间,以备计算后续超时
}
catch(e) {
    // 把异常指定为最大超时,这样更好的反应接口健康情况
    emaObj.update(Math.max(Date.now() - start, options.Tmax)); 
}    

适用条件

固定业务逻辑,循环执行,如FastCGI/S++。

程序大部分时间在等待响应,而不是CPU计算或处理I/O中断。

服务是串行处理模式,容易受异常、慢请求阻塞。

响应时间不宜波动过大。

可以接受有损服务。

License

MIT