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

Remove internal fetch retry support #248

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 1 addition & 41 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export const defaultEndpoint =
export const currentConfig: Config = {
japaneseAddressesApi: defaultEndpoint,
cacheSize: 1_000,
backendTimeout: 1_500,
backendTries: 3,
}

export type FetchOptions = {
Expand All @@ -27,44 +25,6 @@ export type FetchLike = (
options?: FetchOptions,
) => Promise<FetchResponseLike>

const timeoutableFetch = async (
fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>,
input: RequestInfo,
init: RequestInit | undefined,
timeout: number,
) => {
const response = await fetch(input, {
...init,
signal: AbortSignal.timeout(timeout),
})
return response
}

export async function fetchWithTimeoutRetry(
fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>,
input: RequestInfo,
init?: RequestInit,
) {
let tries = 0
while (true) {
try {
// await needs to be in this try block, otherwise it won't be caught
const resp = await timeoutableFetch(
fetch,
input,
init,
currentConfig.backendTimeout,
)
return resp
} catch (error) {
tries++
if (tries >= currentConfig.backendTries) {
throw error
}
}
}
}

/**
* @internal
*/
Expand All @@ -90,7 +50,7 @@ export const __internals: { fetch: FetchLike } = {
} else {
throw new Error('fetch is not available in this environment')
}
return fetchWithTimeoutRetry(globalFetch, url, {
return globalFetch(url, {
headers,
})
},
Expand Down
5 changes: 1 addition & 4 deletions src/main-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Normalize from './normalize'
import {
__internals,
fetchWithTimeoutRetry,
FetchOptions,
FetchResponseLike,
} from './config'
Expand Down Expand Up @@ -48,9 +47,7 @@ export const requestHandlers = {
if (typeof o.length !== 'undefined' && typeof o.offset !== 'undefined') {
headers['Range'] = `bytes=${o.offset}-${o.offset + o.length - 1}`
}
return fetchWithTimeoutRetry(
// 私達が使う場所の undici fetch インタフェースはDOMのfetchと同等なので、型キャストしても問題ない
fetch as unknown as (typeof Window.prototype)['fetch'],
return fetch(
fileURL.toString(),
{
headers,
Expand Down
9 changes: 0 additions & 9 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ export interface Config {
/** 内部キャッシュの最大サイズ。デフォルトでは 1,000 件 */
cacheSize: number

/**
* バックエンドへのリクエストのタイムアウト。
* デフォルト 600ms
* タイムアウトした場合、数回リトライを行います。
* リトライ回数は {@link backendTries} で設定します。
*/
backendTimeout: number
backendTries: number

geoloniaApiKey?: string
}
export const config: Config = currentConfig
Expand Down