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

Refactor/ Add fetchWithTimeout and use it in portfolio's batcher #1170

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
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
29 changes: 14 additions & 15 deletions src/libs/portfolio/batcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Fetch } from '../../interfaces/fetch'
import { fetchWithTimeout } from '../../utils/fetch'

export interface QueueElement {
resolve: Function
Expand Down Expand Up @@ -37,11 +38,16 @@ export default function batcher(
// useful also if the API is limited to a certain # and we want to paginate
requestGenerator(queueCopy).map(async ({ url, queueSegment }) => {
try {
const fetchPromise = fetch(url).then(async (resp) => {
const fetchPromise = fetchWithTimeout(
fetch,
url,
{},
timeoutSettings?.timeoutAfter || 20000
).then(async (resp) => {
const body = await resp.json()
if (resp.status !== 200) throw body
if (body.hasOwnProperty('message')) throw body
if (body.hasOwnProperty('error')) throw body
if (Object.prototype.hasOwnProperty.call(body, 'message')) throw body
if (Object.prototype.hasOwnProperty.call(body, 'error')) throw body
if (Array.isArray(body)) {
if (body.length !== queueSegment.length)
throw new Error('internal error: queue length and response length mismatch')
Expand All @@ -53,19 +59,12 @@ export default function batcher(
} else throw body
})

if (timeoutSettings) {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Request timed out'))
}, timeoutSettings.timeoutAfter)
})
await Promise.race([fetchPromise, timeoutPromise])
} else {
await fetchPromise
}
await fetchPromise
} catch (e: any) {
if (e.message === 'Request timed out' && timeoutSettings) {
console.error(timeoutSettings.timeoutErrorMessage)
if (e.message === 'request-timeout' && timeoutSettings) {
console.error('Batcher error: ', timeoutSettings.timeoutErrorMessage)
} else {
console.log('Batcher error:', e)
}
queueSegment.forEach(({ reject }) => reject(e))
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fetchWithTimeout = async (
fetch: Function,
url: string,
options: RequestInit,
timeout: number
): Promise<any> => {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('request-timeout'))
}, timeout)
})

return Promise.race([fetch(url, options), timeoutPromise])
}

export { fetchWithTimeout }
Loading