Skip to content

Commit

Permalink
Remove internal retry logic (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 authored Oct 23, 2024
1 parent 8f8f205 commit 08c2060
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 54 deletions.
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

0 comments on commit 08c2060

Please sign in to comment.