Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry option #54

Merged
merged 7 commits into from
May 8, 2023
Merged

Add retry option #54

merged 7 commits into from
May 8, 2023

Conversation

Sinhalite
Copy link
Member

@Sinhalite Sinhalite commented Apr 15, 2023

概要

APIのレスポンスがエラーで返却された際に、最大2回までリトライを行うオプションを追加しました。
(最初のリクエストと合わせて計3回)

Issue #38 に対応するPRとなります。
提案では、リクエスト時のオプションの案もありましたが、Clientの生成時にオプション指定する形としています。

const client = createClient({
  serviceDomain: "YOUR_DOMAIN",
  apiKey: "YOUR_API_KEY",
  retry: true 
});

リトライにあたっては、リクエストの分散のため、ExponentialBackoffが採用されています。
(async-retryライブラリにて、標準的に実装されています。)

実際の挙動

Next.jsでビルドを行った際の挙動です。
(リトライ再現の都合上、404の際にもリトライするように変更して、実施してます。)

Screen.Recording.2023-04-15.at.7.16.58.PM.mov

Comment on lines +137 to +144
{
retries: retryOption ? MAX_RETRY_COUNT : 0,
onRetry: (err, num) => {
console.log(err);
console.log(`Waiting for retry (${num}/${MAX_RETRY_COUNT})`);
},
minTimeout: MIN_TIMEOUT_MS,
}
Copy link
Member Author

@Sinhalite Sinhalite Apr 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

リトライはデフォルトの設定ではOFFとしています、

Exponential Backoffのアルゴリズムについては、async-retryのReadmeに記載があります。
https://github.com/vercel/async-retry#api

こちらの設定の場合、
1回目のリトライ: 1回目のリクエスト失敗から、5秒〜10秒のディレイ
2回目のリトライ: 2回目のリクエスト失敗から、10秒〜20秒のディレイ
の間に実行されます。

@Sinhalite Sinhalite marked this pull request as ready for review April 15, 2023 10:25
Copy link
Member

@dc7290 dc7290 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体的に良さそうです!

1点だけ、get, createなどのメソッドを呼び出すときに上書きできる方がいいかなと思いました。
SDKの用途として、サーバー側、クライアント側の2つがありますが、このうちクライアント側はSWRなどのライブラリを使用することも多く、その場合はRetryオプションをオフにするのではないかと思いました。
そのため、デフォルトの設定をcreateClientで設定し、メソッド側でそれを上書きできるような設計はどうでしょうか?

@Sinhalite
Copy link
Member Author

@dc7290
レビューどうもです!

1点だけ、get, createなどのメソッドを呼び出すときに上書きできる方がいいかなと思いました。 SDKの用途として、サーバー側、クライアント側の2つがありますが、このうちクライアント側はSWRなどのライブラリを使用することも多く、その場合はRetryオプションをオフにするのではないかと思いました。 そのため、デフォルトの設定をcreateClientで設定し、メソッド側でそれを上書きできるような設計はどうでしょうか?

クライアントサイドだと、別ライブラリでの制御としたいケースがありそうというのは、確かにですね。
こちらに関しては、ReadmeのTipsにあるようにClientを作り分ける方法でも良いかなと思ったんですが、どうでしょう?
https://github.com/microcmsio/microcms-js-sdk#tips

@himorishige
Copy link
Contributor

対応ありがとうございます!すばらしい!
自分もSSGなどビルド時には必要だと思いますが、デプロイされたサイト上では利用すること少ないかなと思います。
デフォルトではOFFでビルド時だけONに上書きできるようにするとかできると運用しやすいかなと。
もしくは上記の通りClientを使い分けてもらうというのも良さそうですね。

@Sinhalite
Copy link
Member Author

レビューありがとうございました!

リトライ設定のスコープをより柔軟に制限できても良さそうという件は、

  • Clientの使い分けによって一定の対処ができそう
  • オプショナルな仕様追加とはなりそうなので、破壊的な変更等はなく今後実装できそう

という観点があるかとおもったので、こちらについては、マージしてリリースする流れで進めたいと思います。

Copy link
Member

@dc7290 dc7290 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

Copy link
Contributor

@himorishige himorishige left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記方針で良いと思います。
対応ありがとうございます!

@Sinhalite Sinhalite merged commit 7077435 into microcmsio:main May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants