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

cotool

v1.0.6

Published

coto provide some useful tool for web server

Downloads

24

Readme

coto

该库提供了一系列 nodejs web 后端常用的工具,包括自适应降载器,时间滑动窗口,时间轮定时器,lru,布隆过滤器,bitmap 和一些限速工具等。

usage

npm i cotool

adapter shedder

自适应降载器,会计算当前进程 cpu 消耗情况,时间窗口内最大并发数,和当前平均并发数等数据,自适应拒绝请求,降低服务器负载。

const shedder = new AdaptiveShedder({
  window:5, // 时间窗口5秒
  buckets:50, // 5秒窗口桶的数量,每个桶5 * 1000/50 = 100ms
  beta:0.95, // 滑动平均权重,0.95相当于20次平均值
  cpu:{
	  cpuThreshold:700000, //cpu负载,大约相当于60%
	  cpuBeta:0,9, // cpu负载统计,滑动平均权重,相当于10次平均
	  duration:250 // cpu统计周期,250ms统计一次
  }
});
// 注册一个中间件,调用allow()方法,返回一个promise和error,如果error有值,说明负载太高,直接拒绝请求,promise.fail()记录请求失败,promise.pass()记录请求成功
export default class AdaptiveShedderMiddleware implements KoaMiddleware {
  constructor() {}

  use = async (ctx: Context, next: NextFunction) => {
    const { promise, error } = shedderClient.allow()
    if (error) {
      ctx.status = 500
      throw new Error('负载太高')
    }
    try {
      await next()
    } finally {
      if (![200].includes(ctx.status)) {
        promise.fail()
      } else {
        promise.pass()
      }
    }
  }
}

布隆过滤器

布隆过滤器常用来判断不存在,可以用来解决缓存穿透等场景问题。(注意,布隆过滤器不能处理删除)

import RedisClient, { RedisOptions, Redis } from "ioredis";
const redisOption = { host: "127.0.0.1", port: 6379 };
const store = new RedisClient(options);

const bitSet = new RedisBitSet(store, "test_key", 64); // 包含64个bit位
const filter = new Filter(64, bitSet); // 64个比特位
// set a value
await filter.add("hello");
await filter.add("world");

await filter.exists("hello"); // true
await filter.exists("world"); // true
await filter.exists("hello world"); // false

bitmap

基于 redis 的 bitmap,可以用来做一些判断存在性问题的场景,比如一篇文章,用户 a 是否已读。

import RedisClient, { RedisOptions, Redis } from "ioredis";
const redisOption = { host: "127.0.0.1", port: 6379 };
const store = new RedisClient(options);

await bitMapSet(store, key, [1, 2, 3]); // 索引位 1  2 3 设为[1]
await bitMapTest(store, key, [1, 2, 3, 4, 5]); // 返回值为1的索引,[1,2,3]
await bitMapUnSet(store, key, [1, 2]); // 将索引位 1,2的值设为0
await bitMapTest(store, key, [1, 2, 3, 4, 5]); // 返回值为1的索引,[3]

时间滑动窗口

时间滑动窗口可以用来收集一些时序相关的信息,可以用在限速,时序相关热度榜等场景

const size = 300; // 300个桶
const interval = 50; // 每个桶50ms
const ignoreCurrent = true; // reduce时是否忽略当前正在写入的桶,未写完的桶可能会在统计时提供不完整的信息
const r = new RollingWindow({ size, interval, ignoreCurrent });

r.add(1); // 在当前桶添加值,比如在做时间窗口限速时,每个请求进来都add(1)

// 做计数统计,只统计当前有效窗口
const result = [];
r.reduce((bucket) => {
  result.push(bucket.sum);
});

lru, 最近最少使用淘汰策略算法

const capacity = 30;
const lru = new LRUCache(capacity);

lru.put("a", "a");
lru.get("a");

lru-redis-zet,用 redis 的 zset 结构实现 lru 算法

const storeKey = "test_zset_lru";
const capacity = 3;
const store = newRedis(redisOption);
const lru = new ZsetLru(store, storeKey, capacity);

lru.put("a", "a");
lru.get("a");