Skip to content

Commit

Permalink
docs: ✏️ Add an introduction to the Exponential-backoff algorith
Browse files Browse the repository at this point in the history
  • Loading branch information
haozi committed Mar 12, 2024
1 parent 1a888aa commit e8c4f3b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.vscode
.npmrc
coverage
public
**/demo
**/mock
**/src
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Assuming an interface has a 10% failure rate, the probability of still failing a

Using `idmp` to wrap the interface, it will automatically retry on timeouts or failures, which greatly reduces the occurrence of abnormal situations. Before each retry, you can monitor exceptions through the `onBeforeRetry` hook function (note that it will not capture the last error)

`idmp` internally implements an algorithm similar to [Exponential backoff](https://en.m.wikipedia.org/wiki/Exponential_backoff), which dynamically changes the retry time to avoid DDoS to the server.

```typescript
const getUserData = idmp(
'using a deduplicated string as a key',
Expand Down
10 changes: 6 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ export const getInfoByIdIdmp = (id: string) =>

## 插件

`idmp` 有一个强大的插件系统。 以下插件是官方维护的,您也可以参考源码创建自己的插件:
`idmp` 有一个强大的插件系统。 以下是官方维护的插件列表,您也可以参考源码创建自己的插件:

与高阶函数的类比优雅地传达了插件可以以非侵入性方式扩展 idmp 的核心功能,类似于数学函数 $g(f)(x)$。 这为插件系统提供了极大的灵活性和可扩展性
插件可以以非侵入性方式扩展 `idmp` 的核心功能,类似于数学函数 $g(f)(x)$。 这种优雅的设计为插件系统提供了极大的灵活性和可扩展性

- [使用node-fs进行数据持久化](plugins/node-fs/README.md)(将数据持久化到文件系统)
- [使用 node-fs 进行数据持久化](plugins/node-fs/README.md)(将数据持久化到文件系统)
- [使用 localStorage 进行数据持久化](https://github.com/ha0z1/idmp/blob/main/plugins/browser-storage/README.md)
- [使用 sessionStorage 进行数据持久化](https://github.com/ha0z1/idmp/blob/main/plugins/browser-storage/README.md)
- 使用 indexedDB 进行数据持久化 // TODO
Expand Down Expand Up @@ -212,7 +212,7 @@ $$

当哪天页面不需要直接消费 dataA 的数据时,直接删除请求 dataA 的代码就好了,没有任何心智负担。

## 健壮性
## 鲁棒性

假设一个接口的请求失败率为 10%, 那么通过 3 次尝试后,请求仍失败的可能性将降到 0.1%

Expand All @@ -233,6 +233,8 @@ const getUserData = idmp(
)
```

idmp 内部实现了类似 [指数退避](https://en.m.wikipedia.org/wiki/Exponential_backoff)的算法,会动态改变重试时间,避免对服务器造成 DDoS。

## 优化大计算

虽然 `idmp` 的第二个参数必须是一个 Promise 函数,但由于同步函数都可以方便地包装成 Promise 对象。故 `idmp` 除了可以缓存网络请求外,原则上可以缓存任何函数调用。
Expand Down
3 changes: 2 additions & 1 deletion demo/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ interface IProps {
id: string
}
export default (props: IProps) => {
const [data, setData] = useState({})
const [data, setData] = useState<any>(null)
useEffect(() => {
getUserData(props.id).then((res) => {
setData(res)
})
}, [])
if (!data) return <>waiting server's data...</>
return <>{JSON.stringify(data)}</>
}
4 changes: 3 additions & 1 deletion demo/ItemIdmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ interface IProps {
id: string
}
export default (props: IProps) => {
const [data, setData] = useState({})
const [data, setData] = useState<any>(null)
useEffect(() => {
getUserDataIdmp(props.id).then((res) => {
setData(res)
})
}, [])

if (!data) return <>waiting server's data...</>
return <>{JSON.stringify(data)}</>
}
4 changes: 4 additions & 0 deletions demo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import idmp, { g } from '../src'
// return res
// }

const sleep = (delay: number) =>
new Promise((resolve) => setTimeout(resolve, delay))

export const getUserData = async (userId: string) => {
const API = `https://haozi.me/?id=${userId}&t=${Math.random()}`
await fetch(API).then((d) => d.text())

await sleep(2000)
const res = {
id: userId,
val: Math.random(),
Expand Down
4 changes: 3 additions & 1 deletion demo/pages/Storage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default () => {
setData(data)
})
}, [])
if (!data) return

if (!data) return <>waiting server's data...</>

return (
<div>
Refresh the browser and still use the same data in 5 seconds. No network
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dts from 'vite-plugin-dts'

export default defineConfig(({ mode }) => {
return {
publicDir: false,
plugins: [react(), dts()],
build: {
minify: !false,
Expand Down

0 comments on commit e8c4f3b

Please sign in to comment.