throttle-url
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

throttle-url

Install

yarn add throttle-url

Example

为某个接口实现最大并发量

import { throttleUrl } from "throttle-url";

async function ping() {
  const removeThrottle = throttleUrl({
    // 接口名称,用来计数标记
    url: "/ping",
    // 并发最大值任务数
    max: 100,
    // 每个任务最大时间,超时自动移除当前任务
    timeout: 5000,
    // 当达到并发最大值时执行
    onMax: (i) => {
      throw Error(`请稍后再试,当前并发: ${i}`);
    },
  });

  try {
    await new Promise((res, rej) => {
      setTimeout(() => {
        if (Math.random() > 0.8) {
          rej("异步错误");
        } else {
          res(void 0);
        }
      }, 2000);
    });
  } catch (err) {
    removeThrottle();
    throw err;
  }

  removeThrottle();
  return "pong";
}

Readme

Keywords

none

Package Sidebar

Install

npm i throttle-url

Weekly Downloads

1

Version

0.0.3

License

none

Unpacked Size

4.24 kB

Total Files

6

Last publish

Collaborators

  • ymblender