npm package: @neosjs/exponential-backoff
Description: 一个用于对失败操作进行重试的库，它采用指数递增的方式来控制重试间隔。当达到设定的最大重试次数或者未命中重试条件时将停止重试。
Author: 听着情歌流泪
Latest version: 1.1.0

<div align="center">

# @neosjs/exponential-backoff

一个用于对失败操作进行重试的库，它采用指数递增的方式来控制重试间隔。

当达到设定的最大重试次数或者未命中重试条件时将停止重试。

![Npm Version][npm-version-image] ![Npm Week Downloads][npm-downloads-image-week] ![Npm Month Downloads][npm-downloads-image-month] ![License][license-image] ![Exponential Backoff][exponential-backoff]

</div>

## 安装

```bash
pnpm add @neosjs/exponential-backoff

npm install @neosjs/exponential-backoff

yarn add @neosjs/exponential-backoff
```

## 使用

### 全局配置

```ts
import Backoff from '@neosjs/exponential-backoff'

// 全局配置
const backoff = new Backoff({
  initialDelay: 2000,
  maxDelay: 60000,
  factor: 2,
  jitter: true,
  maxRetries: 5,
  retryableErrors: ['ECONNRESET', 'ECONNREFUSED'],
  logger: true
})

const fetchData = async () => {
  // ....
}

backoff.execute(fetchData).then((res: any) => {
  console.log(res)
}).catch((err: any) => {
  console.log(err)
})
```

### 局部配置

仅对当前的请求有效

```ts
import Backoff from '@neosjs/exponential-backoff'

// 全局配置
const backoff = new Backoff()

const fetchData = async () => {
  // ....
}

backoff.execute(fetchData, {
  initialDelay: 2000,
  maxDelay: 60000,
  factor: 2,
  jitter: true,
  maxRetries: 5,
  retryableErrors: ['ECONNRESET', 'ECONNREFUSED']
}).then((res: any) => {
  console.log(res)
}).catch((err: any) => {
  console.log(err)
})
```

### 自定义日志回调

```ts
backoff.execute(fetchData, {
  initialDelay: 2000,
  maxDelay: 60000,
  factor: 2,
  jitter: true,
  maxRetries: 5,
  logger: (msg) => {
    // ...
  }
}).then((res: any) => {
  console.log(res)
}).catch((err: any) => {
  console.log(err)
})
```

## 参数

| 参数            | 描述             | 类型              | 默认值          |
| --------------- | ---------------- | ----------------- | --------------- |
| initialDelay    | 初始延迟时间     | Number            | 2000            |
| maxDelay        | 最大延迟时长     | Number            | 60000           |
| factor          | 指数因子         | Number            | 2               |
| jitter          | 是否应用随机抖动 | Boolean           | true            |
| maxRetries      | 最大重试次数     | Number            | 10              |
| retryableErrors | 可重试的错误码   | any[]             | retryableErrors |
| logger          | 是否打印重试日志 | Boolean、Function | true            |

#### retryableErrors(默认)

```ts
['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED', 'EPIPE', 'CONNECTION_CLOSED', 'ERR_NETWORK', 429, 503, 504]
```

## License

[MIT](./LICENSE) License © 2021-PRESENT [NeosJS](https://docs.neosjs.com)

<!-- Badges -->

[npm-version-image]: https://img.shields.io/npm/v/@neosjs/exponential-backoff?style=flat&colorA=2d333b&colorB=1fa669
[npm-downloads-image-week]: https://img.shields.io/npm/dw/@neosjs/exponential-backoff?style=flatt&colorA=2d333b&colorB=1fa669
[npm-downloads-image-month]: https://img.shields.io/npm/dm/@neosjs/exponential-backoff?style=flat&colorA=2d333b&colorB=1fa669
[license-image]: https://img.shields.io/npm/l/@neosjs/exponential-backoff?style=flat&colorA=2d333b&colorB=1fa669
[exponential-backoff]: https://img.shields.io/badge/NeosJS-exponential--backoff-1fa669?style=flat&colorA=2d333b&colorB=1fa669